-
Notifications
You must be signed in to change notification settings - Fork 0
/
FaceDetectorCascade.asv
124 lines (93 loc) · 4.45 KB
/
FaceDetectorCascade.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
124
clear variables
% load the detector cascade variables
load detectorCascade.mat;
% load image file
[imageFilename, pathName] = uigetfile('*.*', 'Select image file');
[testImage testImageMap] = imread([pathName imageFilename]);
testImage = imcrop(testImage); % ############TAKE OUT IN GENERAL CASE
%figure,imshow(Itest);
% check if image has a colormap
if ~isempty(testImageMap)
testImage = ind2gray(testImage, testImageMap);
end
% convert the image from RGB to rgb
if length(size(testImage)) > 2
testImage = rgb2gray(testImage);
end
% Show the detection image
figure,imshow(testImage);
testImage = double(testImage);
figure,imshow(testImage, []);
[row col]=size(testImage);
mindim=min(row,col);
cropsize=24;
patchi=24;
patchsize=24*24;
patchsizepad=25*25;
detectedFaces = 0;
detectedNonFaces = 0;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Subwindow extraction starting from 24x24 to
% mindim x mindim
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
for psize = 24:6:mindim
% xplace and yplace are the subewindow coordinates
for xplace=1:10:col-psize
for yplace=1:10:row-psize
% extract a subwindow and resize it to [cropsize, cropsize]
[imageSubwindow, subwindowRect] = imcrop(testImage, [xplace yplace psize psize]);
imageSubwindow = imresize(imageSubwindow, [cropsize,cropsize]);
originalSubWindow = imageSubwindow;
% normalize the image intensities
imageSubwindow = normImageF(imageSubwindow); % may slow down processing...
% create a integral image and transform it to one row
imageSubwindow = reshape(imageSubwindow, 1, cropsize*cropsize);
imageSubwindow = padJN(imageSubwindow);
imageSubwindow = cumImageJN(imageSubwindow);
x = imageSubwindow;
disp('subwindow:');
disp(subwindowRect);
% go through detection cascade
for cascadeStepIndex=1:cascadeSize
disp(' cascade:');
disp([' ' cascadeStepIndex]);
% get the number of features used in this cascade step
i = cascadeHaarFeaturesCount{cascadeStepIndex};
% calculate the current cascade step detector threshold
cascadeStepThresh = 2. + sum(cascadeAlphaArray{cascadeStepIndex}(1:i))/2;
% calculate the cascade step sum for the current subwindow
cascadeStepSum=0;
for t=1:i
fout = x * haarFeatures(:, cascadeStepBestFeatures{cascadeStepIndex}(t));
if fout*cascadePBestArray{cascadeStepIndex}(t) < cascadeThetaBestArray{cascadeStepIndex}(t)*cascadePBestArray{cascadeStepIndex}(t)
cascadeStepSum = cascadeStepSum + cascadeAlphaArray{cascadeStepIndex}(t);
end
end
% check the cascade step threshold value
if cascadeStepSum < cascadeStepThresh
detectedNonFaces = detectedNonFaces + 1;
% save the image in the test negatives directory
%saveImage = uint8(originalSubWindow);
%imwrite(imresize(saveImage,[cropsize,cropsize]), ['test_negatives\' sprintf('%d',detectedNonFaces) '.bmp']);
% this is not a face so reject this subwindow
break;
end;
if cascadeStepIndex == cascadeSize
% if this is the last cascade step and it says it is a face
% WE FOUND A "FACE"
detectedFaces = detectedFaces + 1;
% save the image in the test positives directory
saveImage = uint8(originalSubWindow);
imwrite(imresize(saveImage,[cropsize,cropsize]), ['test_positives\' sprintf('%d',detectedFaces) '.bmp']);
% place a rectangle around that face
rectangle('Position', subwindowRect, 'edgecolor', 'red');
drawnow;
else
% place a rectangle around that face
%rectangle('Position', subwindowRect, 'edgecolor', 'blue');
%drawnow;
end
end
end
end
end