-
Notifications
You must be signed in to change notification settings - Fork 0
/
Latent_img.cpp
215 lines (162 loc) · 4.99 KB
/
Latent_img.cpp
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/objdetect/objdetect.hpp"
#include "opencv2/ml/ml.hpp"
#include <iostream>
#include <stdio.h>
using namespace std;
using namespace cv;
// Function Headers
Mat MatchTemplate(Mat , Mat);
vector<float> computeHOG(Mat);
Mat deskew(Mat& );
int main( int argc, char** argv )
{
Mat in,src_img,templ_img, Cropped_ROI,result;
int positive_count,negative_count;
positive_count = 40;
negative_count = 20;
vector<float> HOGvec;
vector<vector<float>> trainVector;
//Load the template
const string& templ_file = "Latent_template.jpg" ;
templ_img = imread(templ_file,1);
for(int i=1;i<=positive_count;i++)
{
//Load the image
const String& img_file = "real"+ to_string(i) +".jpg" ;
in = imread(img_file,1);
if (in.empty())
{
cout<<"Image cannot be loaded..!!"<<endl;
return -1;
}
//Deskew the image if required
src_img = deskew(in);
Cropped_ROI = MatchTemplate(src_img,templ_img);
//Compute HOG of ROI
HOGvec = computeHOG(Cropped_ROI);
trainVector.push_back(HOGvec);
}
for(int i=1;i<=negative_count;i++)
{
const String& img_file = "fake"+ to_string(i) +".jpg" ;
in = imread(img_file,1);
if (in.empty())
{
cout<<"Image cannot be loaded..!!"<<endl;
return -1;
}
//Deskew the image if required
src_img = deskew(in);
Cropped_ROI = MatchTemplate(src_img,templ_img);
HOGvec = computeHOG(Cropped_ROI);
trainVector.push_back(HOGvec);
}
// vector to matrix
// trainVector.size() equals positive_count + negative_count
int descriptor_size = trainVector[0].size();
Mat HOGfeatMatrix(positive_count + negative_count, descriptor_size , CV_32FC1);
Mat labels( positive_count + negative_count, 1, CV_32FC1, Scalar(-1.0) );
labels.rowRange( 0, positive_count ) = Scalar( 1.0 );
for (int i=0;i<labels.rows;++i)
cout<<labels.at<float>(i,0)<<endl;
for (int i = 0; i<trainVector.size(); i++)
{
for (int j = 0; j<descriptor_size; j++)
{
HOGfeatMatrix.at<float>(i, j) = trainVector[i][j];
}
}
// Set SVM parameters
CvSVMParams params;
params.svm_type = SVM::C_SVC;
params.C = 0.1;
params.kernel_type = SVM::RBF;
params.term_crit = TermCriteria(CV_TERMCRIT_ITER, (int)1e7, 1e-6);
CvSVM svm;
//Train the SVM
svm.train_auto(HOGfeatMatrix, labels, Mat(), Mat(), params);
//save the trained model
svm.save("TrainedModel.xml");
return 0;
}
// Function Definition
Mat MatchTemplate(Mat img, Mat templ)
{
Rect roi;
Mat roi_cropped;
// Source image to display
Mat img_display,result;
img.copyTo( img_display );
// Create the result matrix
int result_cols = img.cols - templ.cols + 1;
int result_rows = img.rows - templ.rows + 1;
result.create( result_cols, result_rows, CV_32FC1 );
// Do the Matching and Normalize
matchTemplate( img, templ, result, CV_TM_CCOEFF );
normalize( result, result, 0, 1, NORM_MINMAX, -1, Mat() );
/// Localizing the best match with minMaxLoc
double minVal;
double maxVal;
Point minLoc;
Point maxLoc;
Point matchLoc;
minMaxLoc( result, &minVal, &maxVal, &minLoc, &maxLoc, Mat() );
// the best matches are the higher the better
matchLoc = maxLoc;
//Drawing ROI
rectangle( img_display, matchLoc, Point( matchLoc.x + templ.cols , matchLoc.y + templ.rows ), Scalar::all(0), 2, 8, 0 );
//Cropinng thr ROI
roi.x = matchLoc.x+10;
roi.y = matchLoc.y+10;
roi.width = templ.cols-20;
roi.height = templ.rows-20;
img_display(roi).copyTo(roi_cropped);
return roi_cropped;
}
vector<float> computeHOG(Mat img)
{
Mat src,hsv,channel[3];
img.copyTo(src);
Mat imgLatent;
cvtColor( src, hsv, CV_BGR2HSV );
split(hsv,channel);
channel[1].copyTo(imgLatent);
resize(imgLatent,imgLatent, Size(640,264));
//Set the HOG class parameters
HOGDescriptor hog(
Size(640,264), //winSize
Size(16,16), //blocksize
Size(8,8), //blockStride,
Size(8,8), //cellSize,
9, //nbins,
1, //derivAper,
-1, //winSigma,
0, //histogramNormType,
0.2, //L2HysThresh,
1, //gammal correction,
64);//nlevels=64
vector<float> descriptors;
hog.compute(imgLatent, descriptors );
return descriptors;
}
Mat deskew(Mat& img)
{ Mat img1;
int SZ = img.rows;
float affineFlags = WARP_INVERSE_MAP|INTER_LINEAR;
cvtColor(img,img1,cv::COLOR_BGR2GRAY);
Moments m = moments(img1);
if(abs(m.mu02) < 1e-2)
{
// No deskewing needed.
return img.clone();
}
// Calculate skew based on central momemts.
double skew = m.mu11/m.mu02;
// Calculate affine transform to correct skewness.
Mat warpMat = (Mat_<double>(2,3) << 1, skew, -0.5*SZ*skew, 0, 1 , 0);
Mat imgOut = Mat::zeros(img.rows, img.cols, img.type());
warpAffine(img, imgOut, warpMat, imgOut.size(),affineFlags);
return imgOut;
}