-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathImageAnalyzer.cpp
378 lines (310 loc) · 10.1 KB
/
ImageAnalyzer.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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
#include "ImageAnalyzer.h"
#include <opencv2/highgui/highgui.hpp>
using namespace cv;
Rect findRoi(const vector<Point2f>& polygon, Mat& image)
{
float xMax = 0;
float yMax = 0;
float xMin = image.cols;
float yMin = image.rows;
for (unsigned int i = 0; i < polygon.size(); i++) {
xMax = max(xMax, polygon[i].x);
yMax = max(yMax, polygon[i].y);
xMin = min(xMin, polygon[i].x);
yMin = min(yMin, polygon[i].y);
}
float x = max(xMin, 0.0f);
float y = max(yMin, 0.0f);
float X = min(xMax, (float) image.cols);
float Y = min(yMax, (float) image.rows);
float w = max(0.0f, X - x);
float h = max(0.0f, Y - y);
return Rect(x, y, w, h);
}
int findMatches(ObjectFeatures object, ObjectFeatures scene,cv::Mat &H)
{
FlannBasedMatcher matcher;
std::vector< DMatch > matches;
matcher.match( object.descriptors, scene.descriptors, matches );
//std::cerr<<"matches: " <<matches.size()<<std::endl;
double max_dist = 0;
double min_dist = 100;
//-- Quick calculation of max and min distances between keypoints
for( int i = 0; i < object.descriptors.rows; i++ ) {
double dist = matches[i].distance;
if( dist < min_dist ) min_dist = dist;
if( dist > max_dist ) max_dist = dist;
}
//printf("-- Max dist : %f \n", max_dist );
//printf("-- Min dist : %f \n", min_dist );
min_dist = std::max(10.0,min_dist);
//-- Draw only "good" matches (i.e. whose distance is less than 3*min_dist )
std::vector< DMatch > good_matches;
for( int i = 0; i < object.descriptors.rows; i++ ) {
if( matches[i].distance < 3*min_dist ) {
good_matches.push_back( matches[i]);
}
}
//std::cerr <<"good matches" << good_matches.size()<<std::endl;
if(good_matches.size() < 4)
return 0;
//-- Localize the object
std::vector<Point2f> obj;
std::vector<Point2f> scenePoints;
for(unsigned int i = 0; i < good_matches.size(); i++ ) {
//-- Get the keypoints from the good matches
obj.push_back( object.keypoints[ good_matches[i].queryIdx ].pt );
scenePoints.push_back( scene.keypoints[ good_matches[i].trainIdx ].pt );
}
H = findHomography( obj, scenePoints, CV_RANSAC );
return good_matches.size();
}
cv::Rect ImageAnalyzer::searchQwopWindow(cv::Mat scene_object)
{
ObjectFeatures scene_data(scene_object,3000);
Mat H;
if(findMatches(init_screen,scene_data,H) > 90) {
std::vector<Point2f> obj_corners(4),scene_corners;
obj_corners[0] = cvPoint(0,0);
obj_corners[1] = cvPoint( init_screen.trainingImage.cols, 0 );
obj_corners[2] = cvPoint( init_screen.trainingImage.cols, init_screen.trainingImage.rows );
obj_corners[3] = cvPoint( 0, init_screen.trainingImage.rows );
perspectiveTransform( obj_corners, scene_corners, H);
Rect roi = findRoi(scene_corners,scene_object);
return roi;
} else
return Rect(0,0,0,0);
}
bool ImageAnalyzer::isEndScreen(cv::Mat im_color)
{
cv::Mat mask,im;
cvtColor(im_color, im, COLOR_RGB2HSV);
inRange(im, cv::Scalar(0,40,206),Scalar(20,80,226), mask);
Canny( mask,mask, 1, 5, 3 );
vector<vector<Point> > contours;
findContours( mask, contours, noArray(), CV_RETR_EXTERNAL, CV_CHAIN_APPROX_SIMPLE, Point(0, 0) );
if(contours.size() > 3)
return false;
for(unsigned int i = 0; i < contours.size(); i++) {
double area = contourArea(contours[i]);
//std::cerr<<"area: "<<area<<std::endl;
if(area >74000 && area < 76000)
return true;
}
return false;
}
vector<vector<Point> > ImageAnalyzer::extractContours(cv::Mat mask)
{
Mat canny_output;
vector<vector<Point> > contours;
vector<Vec4i> hierarchy;
/// Detect edges using canny
Canny( mask, canny_output, 1, 5, 3 );
/// Find contours
findContours( canny_output, contours, hierarchy, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_SIMPLE, Point(0, 0) );
//std::cerr<<"contorni: "<<contours.size()<<std::endl;
return contours;
}
Mat ImageAnalyzer::filterBody(Mat im)
{
cv::Mat mask;
inRange(im, cv::Scalar(120,10,0),Scalar(140,255,255), mask);
Mat element = getStructuringElement( MORPH_ELLIPSE,Size( 8, 8 ) );
erode(mask,mask,element);
dilate(mask,mask,element);
return mask;
}
Mat ImageAnalyzer::filterShoesAndSign(Mat im)
{
Mat mask;
inRange(im, cv::Scalar(000,10,100),Scalar(250,80,255), mask);
Mat element1 = getStructuringElement( MORPH_ELLIPSE,
Size( 6, 6) );
erode(mask,mask,element1);
Mat element = getStructuringElement( MORPH_ELLIPSE,
Size( 20, 20) );
dilate(mask,mask,element);
erode(mask,mask,element);
dilate(mask,mask,element1);
return mask;
}
Mat ImageAnalyzer::filterHair(Mat im)
{
cv::Mat mask;
inRange(im, cv::Scalar(0,100,0),Scalar(180,255,55), mask);
Mat element = getStructuringElement( MORPH_ELLIPSE,Size( 3, 3 ) );
erode(mask,mask,element);
element = getStructuringElement( MORPH_ELLIPSE,Size( 10, 10 ) );
dilate(mask,mask,element);
return mask;
}
void ImageAnalyzer::drawContours(cv::Mat &image,std::vector<std::vector<cv::Point> >& contours)
{
//static RNG rng(12345);
for(unsigned int i = 0; i< contours.size(); i++ ) {
Scalar color = Scalar( 10,128,128 );
cv::drawContours( image, contours, i, color, 2, 8 );
}
}
bool ImageAnalyzer::keepBiggestContours(std::vector<std::vector<cv::Point> > & contours,unsigned int num)
{
if(contours.size() < num)
return false;
if(contours.size() == num)
return true;
std::vector<double> areas;
areas.resize(contours.size());
for(unsigned int i = 0; i < contours.size(); i++)
areas[i] = contourArea(contours[i]);
while(areas.size() > num) {
double minArea= 1000000.0;
unsigned int index = 0;
for(unsigned int i = 0; i < areas.size(); i++) {
if(areas[i]< minArea) {
minArea = areas[i];
index = i;
}
}
areas.erase(areas.begin() + index);
contours.erase(contours.begin()+ index);
}
return true;
}
void ImageAnalyzer::fillResult(std::vector<std::pair<char,cv::Point> > & result,char type,std::vector<std::vector<cv::Point> > &contours)
{
for(unsigned int i = 0; i<contours.size(); i++) {
Moments m = moments( contours[i], false );
Point com( m.m10/m.m00 , m.m01/m.m00 );
result.push_back(std::pair<char,Point>(type,com));
}
}
cv::Mat ImageAnalyzer::filter(cv::Mat scene)
{
cv::Mat im;
Mat im_color = scene.rowRange(30,380).colRange(100,500);
cvtColor(im_color, im, COLOR_RGB2HSV);
cv::Mat mask;
cv::Mat mask2;
inRange(im, cv::Scalar(00,00,200),Scalar(50,5,255), mask);
inRange(im, cv::Scalar(0,00,0),Scalar(50,200,105), mask2);
bitwise_or(mask,mask2,mask);
inRange(im, cv::Scalar(00,100,50),Scalar(90,205,200), mask2);
bitwise_or(mask,mask2,mask);
inRange(im, cv::Scalar(0,200,100),Scalar(50,255,255), mask2);
bitwise_or(mask,mask2,mask);
inRange(im, cv::Scalar(110,100,200),Scalar(120,255,220), mask2);
bitwise_or(mask,mask2,mask);
inRange(im, cv::Scalar(110,200,190),Scalar(120,210,200), mask2);
bitwise_or(mask,mask2,mask);
//inRange(im, cv::Scalar(90,150,0),Scalar(180,250,150), mask);//da tenere in or, l'omino
inRange(im, cv::Scalar(00,150,0),Scalar(90,250,150), mask2);
bitwise_or(mask,mask2,mask);
inRange(im, cv::Scalar(70,80,155),Scalar(120,150,215), mask2);
bitwise_or(mask,mask2,mask);
Mat element = getStructuringElement( MORPH_ELLIPSE,Size( 6, 6 ) );
dilate(mask,mask,element);
element = getStructuringElement( MORPH_ELLIPSE,Size( 6, 6 ) );
erode(mask,mask,element);
Mat sub(mask,Rect(0,0,30,40));
sub.setTo(Scalar::all(255));
im.setTo(Scalar::all(0),mask);
//inRange(im, cv::Scalar(90,150,0),Scalar(180,250,150), mask);//da tenere in or, l'omino
return im;
}
void ImageAnalyzer::saveBackground(cv::Mat immagine)
{
Mat im;
cvtColor(immagine,im, COLOR_RGB2HSV);
background = im.colRange(20,21).clone();
//imshow("QWOP",background);
//waitKey(0);
}
void ImageAnalyzer::removeBackground(cv::Mat immagine)
{
if(immagine.rows != background.rows)
return;
int channels = immagine.channels();
int nRows = immagine.rows;
int nCols = immagine.cols * channels;
uchar* p;
for(int i = 0; i < nRows; ++i) {
p = immagine.ptr<uchar>(i);
Vec3b intensity = background.at<Vec3b>(i,0);
for (int j = 0; j < nCols; j +=channels) {
if(p[j] == intensity[0] && p[j+1] == intensity[1]&&p[j+2] == intensity[2]) {
p[j] = p[j+1] = p[j+2] = 0;
}
}
}
Mat mask;
inRange(immagine, cv::Scalar(000,00,000),Scalar(180,2,255), mask);
immagine.setTo(0,mask);
Mat angolosx = immagine.rowRange(0,100).colRange(0,150);
angolosx.setTo(0);
Mat angolodx = immagine.rowRange(0,100).colRange(immagine.cols-150,immagine.cols);
angolodx.setTo(0);
Mat terreno = immagine.rowRange(immagine.rows-20,immagine.rows);
terreno.setTo(0);
{
Mat mask;
Mat im2 = immagine.rowRange(immagine.rows-100,immagine.rows);
inRange(im2, cv::Scalar(110,00,200),Scalar(120,155,255), mask);
im2.setTo(0,mask);
}
inRange(immagine, cv::Scalar(000,00,000),Scalar(1,1,1), mask);
Mat element = getStructuringElement( MORPH_ELLIPSE,Size( 2, 2 ) );
dilate(mask,mask,element);
immagine.setTo(0,mask);
}
ImageAnalyzer::ImageAnalyzer()
{
Mat img_object;
std::string path = "references/qwopscreen.png";
for(int i = 0; i<3; i++) {
img_object= imread( path, CV_LOAD_IMAGE_GRAYSCALE );
path = std::string("../") + path;
if(img_object.data)
break;
}
if(!img_object.data) {
std::cerr <<"references/qwopscreen.png not found. exiting"<<std::endl;
exit(1);
}
imshow("QWOP",img_object);
waitKey(1);
init_screen.loadImage(img_object,2000);
}
std::vector<std::pair<char,cv::Point> > ImageAnalyzer::analyzeFrame(cv::Mat &im)
{
//cv::Mat im ;
cvtColor(im,im, COLOR_RGB2HSV);
removeBackground(im);
std::vector<std::pair<char,cv::Point> > result;
//Mat im_color =Mat::zeros( im.size(), CV_8UC3 );
Mat im_color = im;
{
Mat mask = filterHair(im);
vector<vector<Point> > contours = extractContours(mask);
keepBiggestContours(contours,1);
drawContours(im_color,contours);
fillResult(result,'H',contours);
}
{
Mat mask = filterShoesAndSign(im);
vector<vector<Point> > contours = extractContours(mask);
keepBiggestContours(contours,4);
drawContours(im_color,contours);
fillResult(result,'S',contours);
}
{
Mat mask = filterBody(im);
vector<vector<Point> > contours = extractContours(mask);
keepBiggestContours(contours,1);
drawContours(im_color,contours);
fillResult(result,'B',contours);
}
cvtColor(im,im, COLOR_HSV2RGB);
//imshow("QWOP2",im_color);
//waitKey(1);
return result;
}