-
Notifications
You must be signed in to change notification settings - Fork 0
/
preprocess_image.m
executable file
·67 lines (54 loc) · 1.9 KB
/
preprocess_image.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
function preprocessed_image = preprocess_image( original_image )
%preprocess_image - Preprocesses an image in order to speed up shape
%detection.
%Preprocesses the image given as an input by clustering the points into
%three clusters and by setting the lightest cluster to 256 and the
%darkest to 0.
%
% Syntax: preprocessed_image = preprocess_image( original_image )
%
% Inputs:
% original_image - RGB image as NxMx3 matrix.
%
% Outputs:
% preprocessed_image - Preprocessed image as as NxMx3 matrix.
%
% Example:
% preprocess_image( original_image )
%
%
% Author: Robert Ciszek
% July 2015; Last revision: 31-May-2017
cform = makecform('srgb2lab');
if size(original_image,3) == 4
original_image = original_image(:,:,1:3);
end
lab_image = applycform(original_image,cform);
ab = double(lab_image(:,:,2:3));
nrows = size(ab,1);
ncols = size(ab,2);
ab = reshape(ab,nrows*ncols,2);
%Check if parallel pool exists. If not, create a new one.
p = gcp('nocreate'); %
if isempty(p)
parpool(maxNumCompThreads);
end
%Cluster pixels in parallel.
[cluster_idx, cluster_center] = kmeans(ab,3,'distance','sqEuclidean', 'Replicates',10,'Options',statset('UseParallel',1));
pixel_labels = reshape(cluster_idx,nrows,ncols);
labels = unique(pixel_labels);
means = zeros(1,length(labels));
%Find the mean of each cluster
for i=1:size(labels,1)
means(i) = mean(original_image( pixel_labels == labels(i)));
end
[B,I] = sort(means);
lightest = labels(I(3));
darkest = labels(I(1));
%Set the pixels in the darkest cluster to 0 and the ones in the lightest to 256.
g_im = rgb2gray(original_image);
g_im = imadjust(g_im);
g_im( pixel_labels == darkest) = 0;
g_im( pixel_labels == lightest) = 256;
preprocessed_image = g_im;
end