-
Notifications
You must be signed in to change notification settings - Fork 2
/
mouthDetector.m
137 lines (102 loc) · 3.5 KB
/
mouthDetector.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
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
clear all; clear clc; close all;
% img = imread('images/smile/negative/2.jpg');
% img = imread('images/smile/positive/s2.jpg');
% img = imread('images/smile/test/smile3.jpg');
cam = webcam;
preview(cam);
pause;
closePreview(cam);
img = snapshot(cam);
img = imgaussfilt(img, 2);
thresholdFace = 1;
thresholdParts = 1;
stdsize = 176;
mins = [15 25];
detectors.stdsize = stdsize;
detectors.detector = cell(4,1);
minSize = int32([stdsize/5 stdsize/5]);
minSize = [max(minSize(1),mins(1)), max(minSize(2),mins(2))];
detectors.detector{1} = vision.CascadeObjectDetector('FrontalFaceCART', 'MergeThreshold', thresholdFace);
detectors.detector{2} = vision.CascadeObjectDetector('Mouth', 'MergeThreshold', thresholdParts, 'MinSize', minSize);
detectors.detector{3} = vision.CascadeObjectDetector('LeftEye', 'MergeThreshold', thresholdParts, 'MinSize', minSize);
detectors.detector{4} = vision.CascadeObjectDetector('RightEye', 'MergeThreshold', thresholdParts, 'MinSize', minSize);
[bbox bbimg faces bbfaces] = detectMouth(detectors,img,2);
figure;imshow(bbimg);
for i=1:size(bbfaces,1)
figure;imshow(bbfaces{i});
end
% Mouth image
mouth = imcrop(img, bbox(5:8));
figure; imshow(mouth);
bw = rgb2gray(mouth);
% bw = mouth;
bw = histeq(bw);
% meanBW = mean2(bw);
% bw = bw + meanBW;
bw = imgaussfilt(bw, 1);
SE = strel('diamond',2.0);
bw2 = imdilate(bw,SE);
% bw2 = bw;
corners = detectHarrisFeatures(bw2);
% corners = detectMinEigenFeatures(bw);
% corners = detectFASTFeatures(bw);
figure;imshow(bw2); hold on;
plot(corners.selectStrongest(50));
coordinates = corners.selectStrongest(50).Location;
left = coordinates(coordinates == min(coordinates(:,1)),:);
right = coordinates(coordinates == max(coordinates(:,1)),:);
bottom = coordinates(coordinates(:,2) == max(coordinates(:,2)), :);
plot(left(:,1), left(:,2), '+r');
plot(right(:,1), right(:,2), '+r');
plot(bottom(:,1), bottom(:,2), '+r');
leftAngle = atand((bottom(2)-left(2))/(bottom(1)-left(1)))
rightAngle = atand((bottom(2)-right(2))/(right(1)-bottom(1)))
meanY = mean(coordinates(:,2));
heightBW = size(bw2, 2);
middleLine = [0, meanY; heightBW, meanY];
plot(middleLine(:,1), middleLine(:,2), 'r');
nearToMean2 = abs(coordinates(:,2)-meanY) <= 5;
bestPoints2 = coordinates(nearToMean2, :);
plot(bestPoints2(:,1), bestPoints2(:,2), '+y');
ymin = meanY-0.1*heightBW;
height = 0.25*heightBW;
rect = [0, ymin, size(bw2, 2), height];
lips = imcrop(bw2,rect);
% lipCorners = detectHarrisFeatures(lips);
lipCorners = detectMinEigenFeatures(lips);
figure;imshow(lips); hold on;
plot(lipCorners.selectStrongest(50));
lipCoord = lipCorners.selectStrongest(50).Location;
meanLipsY = mean(lipCoord(:,2));
heightLipsBW = size(lips, 2);
middleLineLips = [0, meanLipsY; heightLipsBW, meanLipsY];
plot(middleLineLips(:,1), middleLineLips(:,2), 'r');
nearToMean = abs(lipCoord(:,2)-meanLipsY) <= 5;
bestPoints = lipCoord(nearToMean, :);
size(bestPoints)
plot(bestPoints(:,1), bestPoints(:,2), '+y');
binaryImg = local_threshold(lips);
figure;imshow(binaryImg);
W = 4;
% SE = strel('square',W);
SE = strel('line',8,90);
erodedBW = imerode(binaryImg,SE);
% erodedBW = imcomplement(erodedBW);
% dilatedBW = imdilate(binaryImg,SE);
% figure;imshow(dilatedBW);
figure;imshow(erodedBW);
rowSum = sum(erodedBW,2)
min(rowSum)
size(erodedBW, 2)
% rowSum = sum(dilatedBW,2);
% % active contour
% mask = ones(size(binaryImg));
% % mask(5:end-5,5:end-5) = 1;
%
% figure, imshow(mask);
% title('Initial Contour Location');
%
% AC = activecontour(binaryImg,mask);
%
% figure, imshow(AC);
% title('Segmented Image');