Skip to content

Commit fa1fb07

Browse files
committed
Added files via upload
1 parent 48ea1ee commit fa1fb07

27 files changed

+13039
-0
lines changed

Diff for: 1.jpg

7.86 KB
Loading

Diff for: 2.jpg

29.4 KB
Loading

Diff for: 3.jpg

4.97 KB
Loading

Diff for: 4.jpg

8.45 KB
Loading

Diff for: 5.jpg

6.08 KB
Loading

Diff for: 6.jpg

34 KB
Loading

Diff for: 7.jpg

21.2 KB
Loading

Diff for: 8.jpg

37.4 KB
Loading

Diff for: 9.jpg

17.2 KB
Loading

Diff for: Detect_Faces.m

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
%{
2+
This is the implementation of “Matlab tutorial, Face Detection using SVM-Light” uploaded [link]
3+
This is a very basic Face Detector, just to show you how to use SVM-Light for object recognition problems.
4+
%}
5+
6+
clear; close all; clc;
7+
8+
% Add Path to svm library
9+
addpath './svm_mex601/matlab/';
10+
addpath './svm_mex601/bin/';
11+
12+
%pathPos = './dataset/faces/'; % positive example
13+
%pathNeg = './dataset/non-faces/'; % negative example
14+
15+
%% if you want to use your own dataset then following functions could be
16+
% used for cropping the faces, non-face or positive and negative examples
17+
% respectively
18+
% pathN = './INRIA/';
19+
% grab_neg(pathN,pathNeg); % cut image into 4 equal sub images
20+
% flip_all( pathPos, pathNeg ); % Flip and write images to their corresponding directories
21+
22+
%% Learning part
23+
%if exist('model.mat','file')
24+
load model;
25+
%else
26+
% [fpos, fneg] = features(pathPos, pathNeg); % extract features
27+
% [ model ] = trainSVM( fpos,fneg ); % train SVM
28+
% save model model;
29+
%end
30+
31+
%% Detection
32+
tSize = [24, 32];
33+
%testImPath = './test images/';
34+
%imlist = dir([testImPath '*.jpg']);
35+
%for j = 1:length(imlist)
36+
%img = imread([testImPath imlist(j).name]);
37+
% axis equal; axis tight; axis off;
38+
%disp(['Processing Image no.',num2str(j)]);
39+
[filename, pathname] = uigetfile('*.jpg',' Select an Image With A Face ');
40+
img = imread([pathname,filename]);
41+
imshow(img); hold on;
42+
detect(img,model,tSize);
43+
% saveas(gcf, ['./results/' imlist(j).name], 'jpg');
44+
% close all;
45+

Diff for: HOG.m

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
function HOGv = HOG(I)
2+
3+
orientation_bins = 9;
4+
cell_size = 8;
5+
block_size = 2;
6+
oriented_gradients = 1; % 0 otherwise
7+
clipping_L2norm = 0.2;
8+
9+
HOGv = HoG(I,[orientation_bins, cell_size, block_size, oriented_gradients, clipping_L2norm]);
10+
11+
end

Diff for: HoG.cpp

+232
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,232 @@
1+
#include <stdlib.h>
2+
#include <math.h>
3+
#include <mex.h>
4+
#include <vector>
5+
6+
using namespace std;
7+
8+
void HoG(double *pixels, double *params, int *img_size, double *dth_des, unsigned int grayscale){
9+
10+
const float pi = 3.1415926536;
11+
12+
int nb_bins = (int) params[0];
13+
double cwidth = params[1];
14+
int block_size = (int) params[2];
15+
int orient = (int) params[3];
16+
double clip_val = params[4];
17+
18+
int img_width = img_size[1];
19+
int img_height = img_size[0];
20+
21+
int hist1= 2+ceil(-0.5 + img_height/cwidth);
22+
int hist2= 2+ceil(-0.5 + img_width/cwidth);
23+
24+
double bin_size = (1+(orient==1))*pi/nb_bins;
25+
26+
float dx[3], dy[3], grad_or, grad_mag, temp_mag;
27+
float Xc, Yc, Oc, block_norm;
28+
int x1, x2, y1, y2, bin1, bin2;
29+
int des_indx = 0;
30+
31+
vector<vector<vector<double> > > h(hist1, vector<vector<double> > (hist2, vector<double> (nb_bins, 0.0) ) );
32+
vector<vector<vector<double> > > block(block_size, vector<vector<double> > (block_size, vector<double> (nb_bins, 0.0) ) );
33+
34+
//Calculate gradients (zero padding)
35+
36+
for(unsigned int y=0; y<img_height; y++) {
37+
for(unsigned int x=0; x<img_width; x++) {
38+
if (grayscale == 1){
39+
if(x==0) dx[0] = pixels[y +(x+1)*img_height];
40+
else{
41+
if (x==img_width-1) dx[0] = -pixels[y + (x-1)*img_height];
42+
else dx[0] = pixels[y+(x+1)*img_height] - pixels[y + (x-1)*img_height];
43+
}
44+
if(y==0) dy[0] = -pixels[y+1+x*img_height];
45+
else{
46+
if (y==img_height-1) dy[0] = pixels[y-1+x*img_height];
47+
else dy[0] = -pixels[y+1+x*img_height] + pixels[y-1+x*img_height];
48+
}
49+
}
50+
else{
51+
if(x==0){
52+
dx[0] = pixels[y +(x+1)*img_height];
53+
dx[1] = pixels[y +(x+1)*img_height + img_height*img_width];
54+
dx[2] = pixels[y +(x+1)*img_height + 2*img_height*img_width];
55+
}
56+
else{
57+
if (x==img_width-1){
58+
dx[0] = -pixels[y + (x-1)*img_height];
59+
dx[1] = -pixels[y + (x-1)*img_height + img_height*img_width];
60+
dx[2] = -pixels[y + (x-1)*img_height + 2*img_height*img_width];
61+
}
62+
else{
63+
dx[0] = pixels[y+(x+1)*img_height] - pixels[y + (x-1)*img_height];
64+
dx[1] = pixels[y+(x+1)*img_height + img_height*img_width] - pixels[y + (x-1)*img_height + img_height*img_width];
65+
dx[2] = pixels[y+(x+1)*img_height + 2*img_height*img_width] - pixels[y + (x-1)*img_height + 2*img_height*img_width];
66+
67+
}
68+
}
69+
if(y==0){
70+
dy[0] = -pixels[y+1+x*img_height];
71+
dy[1] = -pixels[y+1+x*img_height + img_height*img_width];
72+
dy[2] = -pixels[y+1+x*img_height + 2*img_height*img_width];
73+
}
74+
else{
75+
if (y==img_height-1){
76+
dy[0] = pixels[y-1+x*img_height];
77+
dy[1] = pixels[y-1+x*img_height + img_height*img_width];
78+
dy[2] = pixels[y-1+x*img_height + 2*img_height*img_width];
79+
}
80+
else{
81+
dy[0] = -pixels[y+1+x*img_height] + pixels[y-1+x*img_height];
82+
dy[1] = -pixels[y+1+x*img_height + img_height*img_width] + pixels[y-1+x*img_height + img_height*img_width];
83+
dy[2] = -pixels[y+1+x*img_height + 2*img_height*img_width] + pixels[y-1+x*img_height + 2*img_height*img_width];
84+
}
85+
}
86+
}
87+
88+
grad_mag = sqrt(dx[0]*dx[0] + dy[0]*dy[0]);
89+
grad_or= atan2(dy[0], dx[0]);
90+
91+
if (grayscale == 0){
92+
temp_mag = grad_mag;
93+
for (unsigned int cli=1;cli<3;++cli){
94+
temp_mag= sqrt(dx[cli]*dx[cli] + dy[cli]*dy[cli]);
95+
if (temp_mag>grad_mag){
96+
grad_mag=temp_mag;
97+
grad_or= atan2(dy[cli], dx[cli]);
98+
}
99+
}
100+
}
101+
102+
if (grad_or<0) grad_or+=pi + (orient==1) * pi;
103+
104+
// trilinear interpolation
105+
106+
bin1 = (int)floor(0.5 + grad_or/bin_size) - 1;
107+
bin2 = bin1 + 1;
108+
x1 = (int)floor(0.5+ x/cwidth);
109+
x2 = x1+1;
110+
y1 = (int)floor(0.5+ y/cwidth);
111+
y2 = y1 + 1;
112+
113+
Xc = (x1+1-1.5)*cwidth + 0.5;
114+
Yc = (y1+1-1.5)*cwidth + 0.5;
115+
116+
Oc = (bin1+1+1-1.5)*bin_size;
117+
118+
if (bin2==nb_bins){
119+
bin2=0;
120+
}
121+
if (bin1<0){
122+
bin1=nb_bins-1;
123+
}
124+
125+
h[y1][x1][bin1]= h[y1][x1][bin1]+grad_mag*(1-((x+1-Xc)/cwidth))*(1-((y+1-Yc)/cwidth))*(1-((grad_or-Oc)/bin_size));
126+
h[y1][x1][bin2]= h[y1][x1][bin2]+grad_mag*(1-((x+1-Xc)/cwidth))*(1-((y+1-Yc)/cwidth))*(((grad_or-Oc)/bin_size));
127+
h[y2][x1][bin1]= h[y2][x1][bin1]+grad_mag*(1-((x+1-Xc)/cwidth))*(((y+1-Yc)/cwidth))*(1-((grad_or-Oc)/bin_size));
128+
h[y2][x1][bin2]= h[y2][x1][bin2]+grad_mag*(1-((x+1-Xc)/cwidth))*(((y+1-Yc)/cwidth))*(((grad_or-Oc)/bin_size));
129+
h[y1][x2][bin1]= h[y1][x2][bin1]+grad_mag*(((x+1-Xc)/cwidth))*(1-((y+1-Yc)/cwidth))*(1-((grad_or-Oc)/bin_size));
130+
h[y1][x2][bin2]= h[y1][x2][bin2]+grad_mag*(((x+1-Xc)/cwidth))*(1-((y+1-Yc)/cwidth))*(((grad_or-Oc)/bin_size));
131+
h[y2][x2][bin1]= h[y2][x2][bin1]+grad_mag*(((x+1-Xc)/cwidth))*(((y+1-Yc)/cwidth))*(1-((grad_or-Oc)/bin_size));
132+
h[y2][x2][bin2]= h[y2][x2][bin2]+grad_mag*(((x+1-Xc)/cwidth))*(((y+1-Yc)/cwidth))*(((grad_or-Oc)/bin_size));
133+
}
134+
}
135+
136+
137+
138+
//Block normalization
139+
140+
for(unsigned int x=1; x<hist2-block_size; x++){
141+
for (unsigned int y=1; y<hist1-block_size; y++){
142+
143+
block_norm=0;
144+
for (unsigned int i=0; i<block_size; i++){
145+
for(unsigned int j=0; j<block_size; j++){
146+
for(unsigned int k=0; k<nb_bins; k++){
147+
block_norm+=h[y+i][x+j][k]*h[y+i][x+j][k];
148+
}
149+
}
150+
}
151+
152+
block_norm=sqrt(block_norm);
153+
for (unsigned int i1=0; i1<block_size; i1++){
154+
for(unsigned int j1=0; j1<block_size; j1++){
155+
for(unsigned int k1=0; k1<nb_bins; k1++){
156+
if (block_norm>0){
157+
block[i1][j1][k1]=h[y+i1][x+j1][k1]/block_norm;
158+
if (block[i1][j1][k1]>clip_val) block[i1][j1][k1]=clip_val;
159+
}
160+
}
161+
}
162+
}
163+
164+
block_norm=0;
165+
for (unsigned int i2=0; i2<block_size; i2++){
166+
for(unsigned int j2=0; j2<block_size; j2++){
167+
for(unsigned int k2=0; k2<nb_bins; k2++){
168+
block_norm+=block[i2][j2][k2]*block[i2][j2][k2];
169+
}
170+
}
171+
}
172+
173+
block_norm=sqrt(block_norm);
174+
for (unsigned int i3=0; i3<block_size; i3++){
175+
for(unsigned int j3=0; j3<block_size; j3++){
176+
for(unsigned int k3=0; k3<nb_bins; k3++){
177+
if (block_norm>0) dth_des[des_indx]=block[i3][j3][k3]/block_norm;
178+
else dth_des[des_indx]=0.0;
179+
des_indx++;
180+
}
181+
}
182+
}
183+
}
184+
}
185+
}
186+
187+
188+
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]) {
189+
190+
double *pixels, *dth_des, *params;
191+
int nb_bins, block_size;
192+
int img_size[2];
193+
unsigned int grayscale = 1;
194+
195+
if (nlhs>1) mexErrMsgTxt("Too many output arguments");
196+
if (nrhs==0) mexErrMsgTxt("No Image -> No HoG");
197+
198+
pixels = (double*) mxGetPr(prhs[0]);
199+
img_size[0] = mxGetM(prhs[0]);
200+
img_size[1] = mxGetN(prhs[0]);
201+
if (mxGetNumberOfDimensions(prhs[0])==3){
202+
img_size[1] /= 3;
203+
grayscale = 0;
204+
}
205+
206+
if (nrhs>1){
207+
params = mxGetPr(prhs[1]);
208+
if (params[0]<=0) mexErrMsgTxt("Number of orientation bins must be positive");
209+
if (params[1]<=0) mexErrMsgTxt("Cell size must be positive");
210+
if (params[2]<=0) mexErrMsgTxt("Block size must be positive");
211+
}
212+
else {
213+
params = new double[5];
214+
params[0]=9;
215+
params[1]=8;
216+
params[2]=2;
217+
params[3]=0;
218+
params[4]=0.2;
219+
}
220+
221+
nb_bins = (int) params[0];
222+
block_size = (int) params[2];
223+
224+
int hist1= 2+ceil(-0.5 + img_size[0]/params[1]);
225+
int hist2= 2+ceil(-0.5 + img_size[1]/params[1]);
226+
227+
plhs[0] = mxCreateDoubleMatrix((hist1-2-(block_size-1))*(hist2-2-(block_size-1))*nb_bins*block_size*block_size, 1, mxREAL);
228+
dth_des = mxGetPr(plhs[0]);
229+
230+
HoG(pixels, params, img_size, dth_des, grayscale);
231+
if (nrhs==1) delete[] params;
232+
}

Diff for: HoG.mexw32

28 KB
Binary file not shown.

Diff for: HoG.mexw64

19.5 KB
Binary file not shown.

Diff for: How To Run.txt

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
1. Place the folder in the Matlab Path
2+
2. have Windows SDK with .NET framework 4 installed
3+
3. Type 'mex -setup' in command window
4+
4. Enter y
5+
5. It will display a list of compilers available
6+
6. Choose 1 or 2 and type the same
7+
7. Type mex HoG.cpp in command window
8+
8. Run Detect_Faces.m and select an image from the 'Test Images'
9+
9. Observe results

Diff for: detect.m

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
function detect(im,model,wSize)
2+
%{
3+
this function will take three parameters
4+
1. im --> Test Image
5+
2. model --> trained model
6+
3. wStize --> Size of the window, i.e. [24,32]
7+
and draw rectangle on best estimated window
8+
%}
9+
10+
topLeftRow = 1;
11+
topLeftCol = 1;
12+
[bottomRightCol bottomRightRow d] = size(im);
13+
14+
fcount = 1;
15+
16+
% this for loop scan the entire image and extract features for each sliding window
17+
for y = topLeftCol:bottomRightCol-wSize(2)
18+
for x = topLeftRow:bottomRightRow-wSize(1)
19+
p1 = [x,y];
20+
p2 = [x+(wSize(1)-1), y+(wSize(2)-1)];
21+
po = [p1; p2];
22+
img = imcut(po,im);
23+
featureVector{fcount} = HOG(double(img));
24+
boxPoint{fcount} = [x,y];
25+
fcount = fcount+1;
26+
x = x+1;
27+
end
28+
end
29+
30+
lebel = ones(length(featureVector),1);
31+
P = cell2mat(featureVector);
32+
% each row of P' correspond to a window
33+
[~, predictions] = svmclassify(P',lebel,model); % classifying each window
34+
35+
[a, indx]= max(predictions);
36+
bBox = cell2mat(boxPoint(indx));
37+
rectangle('Position',[bBox(1),bBox(2),24,32],'LineWidth',1, 'EdgeColor','r');
38+
end
39+

Diff for: features.m

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
function [fpos, fneg] = features(pathPos,pathNeg)
2+
% extract features for positive examples
3+
imlist = dir([pathPos '*.png']);
4+
for i = 1:length(imlist)
5+
im = imread([pathPos imlist(i).name]);
6+
fpos{i} = HOG(double(im));
7+
end
8+
% extract features for negative examples
9+
imlist = dir([pathNeg '*.png']);
10+
for i = 1:length(imlist)
11+
im = imread([pathNeg imlist(i).name]);
12+
fneg{i} = HOG(double(im));
13+
end
14+
15+
end

Diff for: flip_all.m

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
function flip_all(pathPos, pathNeg)
2+
3+
4+
% positive images
5+
imlist = dir([pathPos '*.png']);
6+
for i = 1:length(imlist)
7+
im = imread([pathPos imlist(i).name]);
8+
I = flipdim(im ,2);
9+
imwrite(I,[pathPos 'fliped_' imlist(i).name]);
10+
end
11+
12+
% neg images
13+
imlist = dir([pathNeg '*.png']);
14+
for i = 1:length(imlist)
15+
im = imread([pathNeg imlist(i).name]);
16+
I = flipdim(im ,2);
17+
imwrite(I,[pathNeg 'fliped_' imlist(i).name]);
18+
end
19+
20+
end

0 commit comments

Comments
 (0)