-
Notifications
You must be signed in to change notification settings - Fork 2
/
ColorSegmentation.m
59 lines (42 loc) · 1.81 KB
/
ColorSegmentation.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
function [ Mask ] = ColorSegmentation(im, minCutOff, maxCutOff, SampleWidthR, SampleHeightR, SkinWidthR, SkinHeightR )
%COLORSEGMENTATION
he = double(im);
%he = rgb2gray(he);
% figure;
% subplot(1, 3, 1);
% imshow(he);
heHSV = rgb2hsv(((he)));
V = heHSV(:,:,3);
VLocalAvg = imgaussfilt(V, 30);
GlobalAvg = mean(mean(V));
heHSV(:,:,3) = heHSV(:,:,3) + GlobalAvg - VLocalAvg;
% subplot(1, 3, 2);
% imshow(hsv2rgb(heHSV));
lab_he = hsv2rgb(heHSV);
nColors = 2;
ab = (imresize(lab_he, [250, NaN]));
nrows = size(ab,1);
ncols = size(ab,2);
ab = reshape(ab,nrows*ncols,3);
count = 0;
while(count < 5)
[cluster_idx, cluster_center] = kmeans(ab,nColors,'distance','sqEuclidean', 'Replicates',3);
pixel_labels = reshape(cluster_idx,nrows,ncols)-1;
Invert = InversionDecisionRGB( im, cluster_center, SampleWidthR, SampleHeightR, SkinWidthR, SkinHeightR );
if Invert == 1
pixel_labels = not(pixel_labels);
end
%sum(sum(pixel_labels))/(size(pixel_labels,1)*size(pixel_labels,2))
if((sum(sum(pixel_labels))/(size(pixel_labels,1)*size(pixel_labels,2)) < maxCutOff && sum(sum(pixel_labels))/(size(pixel_labels,1)*size(pixel_labels,2)) > minCutOff))
break;
end
count=count+1;
end
ErrorOne = sqrt(((lab_he(:,:,1) - cluster_center(1, 1)).^2) + ((lab_he(:,:,2) - cluster_center(1, 2)).^2) + ((lab_he(:,:,3) - cluster_center(1, 3)).^2));
ErrorTwo = sqrt(((lab_he(:,:,1) - cluster_center(2, 1)).^2) + ((lab_he(:,:,2) - cluster_center(2, 2)).^2) + ((lab_he(:,:,3) - cluster_center(2, 3)).^2));
cluster_center = cluster_center/255;
Mask = ErrorOne > ErrorTwo;
if Invert == 1
Mask = not(Mask);
end
end