-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathremoveRedEyes.asv
123 lines (85 loc) · 2.87 KB
/
removeRedEyes.asv
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
function [result] = removeRedEyes(fileName,printImages)
iImagesRequired = true;
if nargin == 2
iImagesRequired = printImages;
end
I = imread(fileName);
FDetector = vision.CascadeObjectDetector;
disk = strel('disk',2);
detFaces = step(FDetector,I);
if size(detFaces,1) == 0
detFaces = [0 0 size(I,2) size(I,1)];
end
for i = 1:size(detFaces,1)
%se extrae la cara
if (detFaces(1) == 0)
face = I;
eyesRect = [1 1 size(I,2) size(I,1) ];
eyesRegion = I;
else
face = imcrop(I,detFaces(i,:));
eyesRect = calculateFrecuentEyeRegion(face);
eyesRegion = imcrop(face,eyesRect);
end
%se extrae la sección de la cara que tiene más probabilidad de tener
%los ojos
if iImagesRequired
figure,imshow(eyesRegion),title('RegionOjos');
end
%se crea un mapa de "rojez"
redMap = redness(eyesRegion);
%se crea una máscara donde a 0 están todos los píxeles que pueden ser
%piel
eyesRegionG = imgaussfilt(eyesRegion,1);
mask = removeSkinModal(eyesRegionG);
%se crea una máscara donde a 0 están todos los píxeles que se
%encuentran debajo del valor del quantil 60%
redMask = threshold(redMap,quantile(reshape(redMap,1,[]),0.5));
% lumMap = luminance(eyesRegion);
mask = imdilate(mask,disk);
redMask = imdilate(redMask,disk);
if iImagesRequired
figure, imshow(mask),title('Máscara elimina la piel');
figure, imshow(redMask),title('Máscara roja');
end
%se genera la máscara intersección de ambos
intersectionMask = mask .* redMask;
EyesMask = ~imopen(intersectionMask,disk);
figure, imshow(intersectionMask),title('Máscara intersección');
if iImagesRequired
figure, imshow(intersectionMask),title('Máscara intersección');
end
%
% [lbls,l] = bwlabel(intersectionMask); %imopen para quitar ruido
% if l > 2
% [shapes] = shapeFiltering(lbls);
% else
% shapes = intersectionMask;
% end
%imopen para detectar donde está el pelo
%restar a la máscara intersección la negativa de la obtenida
% mask2 = uint8( im2bw(shapes,0.5));
if iImagesRequired
shapeFiltered = applyMask(double(eyesRegion),double(mask2));
figure,imshow(uint8(shapeFiltered)),title('Máscara filtrada por forma');
end
img = imclose(mask2,disk);
img = refillColor(mask2,eyesRegion);
if iImagesRequired
figure,imshow(uint8(img)),title('Image');
end
face = copyOverImg(face,img,eyesRect);
if iImagesRequired
figure,imshow(uint8(face)),title('Cara');
end
if detFaces(1) > 0
I = copyOverImg(I,face,detFaces(i,:));
else
I = face;
end
if iImagesRequired
figure,imshow(uint8(I)),title('I');
end
end
result = I;
end