-
Notifications
You must be signed in to change notification settings - Fork 0
/
testLatentImg.cpp
181 lines (126 loc) · 3.97 KB
/
testLatentImg.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
#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>
#include <ctime>
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;
vector<float> HOGvec;
vector<vector<float>> trainVector;
int start_s=clock(); ///time start
// Set SVM parameters
CvSVM svm ;//= new CvSVM;
svm.load("TrainedModel.xml");
//Load the template
const string& templ_file = "Latent_template.jpg" ;
templ_img = imread(templ_file,1);
//Testing results
Mat testResult;
in = imread(argv[1],CV_LOAD_IMAGE_COLOR);
src_img = deskew(in);
Cropped_ROI = MatchTemplate(src_img,templ_img);
HOGvec = computeHOG(Cropped_ROI);
Mat testMat(1,HOGvec.size(),CV_32FC1);
for (int i=0;i<HOGvec.size();++i)
testMat.at<float>(0,i) = HOGvec[i];
svm.predict(testMat,testResult);
for(int i=0;i<testResult.rows;i++)
{
cout << "result: "<<testResult.at<float>(i,0)<<endl;
}
int stop_s=clock(); ///time end
cout << "time: " << (stop_s-start_s)/double(CLOCKS_PER_SEC) << endl;
//namedWindow( "Original Image" , WINDOW_NORMAL );
//imshow( "Original Image", src_img );
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));
namedWindow( "Output Image" , WINDOW_NORMAL );
imshow( "Output Image", imgLatent );
//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;
}