forked from phg1024/FaceShapeFromShading-MATLAB
-
Notifications
You must be signed in to change notification settings - Fork 0
/
find_hair_pixels.m
51 lines (41 loc) · 1.32 KB
/
find_hair_pixels.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
function [hair_pixels, hair_pixels2, confidence] = find_hair_pixels(I, valid_pixels_indices)
% using HSV space
I_hsv = rgb2hsv(I);
Ih = I_hsv(:,:,1); Is = I_hsv(:,:,2); Iv = I_hsv(:,:,3);
idx = kmeans([Ih(valid_pixels_indices), Is(valid_pixels_indices), Iv(valid_pixels_indices)], 2);
size_c1 = length(idx(idx==1));
size_c2 = length(idx(idx==2));
if size_c1 < size_c2
hair_cluster = 1;
else
hair_cluster = 2;
end
hair_pixels = valid_pixels_indices(idx==hair_cluster);
% using Lab space
I_lab = rgb2lab(I);
Ia = I_lab(:,:,2);
Ib = I_lab(:,:,3);
idx = kmeans([Ia(valid_pixels_indices), Ib(valid_pixels_indices)], 2);
size_c1 = length(idx(idx==1));
size_c2 = length(idx(idx==2));
if size_c1 < size_c2
hair_cluster = 1;
else
hair_cluster = 2;
end
hair_pixels2 = valid_pixels_indices(idx==hair_cluster);
length(valid_pixels_indices)
L1 = length(hair_pixels)
L2 = length(hair_pixels2)
common_pixels = intersect(hair_pixels, hair_pixels2);
confidence = length(common_pixels) / L1
% [h, w, ~] = size(I);
% mask = zeros(h, w);
% mask(valid_pixels_indices) = 1;
% figure;imagesc(Ih .* mask);axis equal;title('Ih');
% figure;imagesc(Is .* mask);axis equal;title('Is');
% figure;imagesc(Iv .* mask);axis equal;title('Iv');
% figure;imagesc(Ia .* mask);axis equal;title('Ia');
% figure;imagesc(Ib .* mask);axis equal;title('Ib');
% pause;
end