-
Notifications
You must be signed in to change notification settings - Fork 26
/
morph.cpp
427 lines (398 loc) · 16.7 KB
/
morph.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
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
#include <opencv2/opencv.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/nonfree/features2d.hpp>
#include <iostream>
#include <vector>
#include <cmath>
#include <ctype.h>
#include "morph.h"
#include "features.h"
using namespace cv;
using namespace std;
/*height and width of the rectangular window*/
#define heightrect 64
#define widthrect 96
Rect frame1(0,0,640,300);
Rect frame2(0,360,640,300);
Rect frame3(640,0,640,300);
Rect frame4(640,360,640,300);
Rect rects1(45, 50, 125, 220);
Rect rects2(175, 50, 300, 220);
Rect rects3(480, 50, 125, 220);
Rect rects4(685, 10, 125, 220);
Rect rects5(815, 10, 300, 260);
Rect rects6(1120, 10, 125, 240);
Rect rects7(45, 410, 120, 200);
Rect rects8(175, 410, 300, 210);
Rect rects9(480, 410, 125, 200);
Rect rects10(685, 370, 120, 260);
Rect rects11(815, 370, 300, 250);
Rect rects12(1120, 370, 125, 220);
int rectstat[12];//Will use this to denote if it has a moving object or not
void imgerode(Mat& img,
Mat& out,
int erosion_size)
{
Mat element = getStructuringElement( MORPH_ELLIPSE,
Size( 2*erosion_size + 1, 2*erosion_size+1 ),
Point( erosion_size, erosion_size ) );
erode(img,out,element);
}
void imgdilate(Mat& img,
Mat& out,
int erosion_size)
{
Mat element = getStructuringElement( MORPH_ELLIPSE,
Size( 2*erosion_size + 1, 2*erosion_size+1 ),
Point( erosion_size, erosion_size ) );
dilate(img,out,element);
}
void extractwindows(Mat& img,Mat& out, vector<Point2f> *points)
{
out=img.clone();
vector<float> pts;//The HOG descriptors
Mat refout;
int included=0;
cout<<"Refining the rectangles"<<endl;
for(int i=0;i<points[0].size();i++)
{
const Point2f& p1=points[0][i];
Rect windows(p1.x, p1.y, widthrect, heightrect);
//Mat roi=img(windows).clone();
//showhog(roi,refout,pts);
//if(comparehog(pts))
rectangle(out, windows, Scalar(255), 1, 8, 0);
}
}
void extractwindowsrefined(Mat& img,Mat &blob, Mat& out, vector<Point2f> *points, vector < vector<Point2i> > &blobs)
{
int seen[blobs.size()+1]={0};//1 if that component is already covered
out=img.clone();
vector<float> pts;//The HOG descriptors
Mat refout;
int classes=0;
int found=0,k=0;
int hogvalue=0;
int count=0;
Rect r;
for(int i=0;i<points[0].size();i++)
{
//cout<<"Inside the first loop"<< i<< points[0].size()<< endl;
count=count+1;
const Point2f& p1=points[0][i];
//cout<<round(p1.x)<<" "<<round(p1.y)<<" "<<blob.rows<<blob.cols<< " Count "<< count << endl;
classes = blob.at<int>(round(p1.y),round(p1.x));
//cout<<"class value "<<classes<<endl;
if(classes>1000)
continue;
//cout<< classes << "value" <<seen[classes] << endl;
//cout<<" Classes "<<classes<<endl;
Rect windows(p1.x, p1.y, widthrect, heightrect);
//cout<<" Classes "<<classes<<endl;
if(classes>=1 && seen[classes]==0)
{
//cout<<"class value "<<classes<<endl;
for(k=0;k<blobs[classes-1].size();k++)
{
int x = blobs[classes-1][k].x;
int y = blobs[classes-1][k].y;
//cout<<k<<blobs[classes].size()<<endl;
if(seen[classes]==0)
{
Rect windows2(x, y, widthrect,heightrect);
if(windows2.x + windows2.width<=img.cols && windows2.y + windows2.height<=img.rows)
windows = windows | windows2;
}
}
if(windows.x + windows.width<=img.cols && windows.y + windows.height<=img.rows)
{
Mat roi=img(windows).clone();
showhog(roi,refout,pts);
hogvalue=comparehog(pts);
//hogvalue=1;
//cout << "Computing HOG "<< hogvalue << endl;
if(hogvalue)
{
r = frame1 & windows;
if(r.area()==windows.area())
rectangle(out, windows, Scalar(255), 1, 8, 0);
r = frame2 & windows;
if(r.area()==windows.area())
rectangle(out, windows, Scalar(255), 1, 8, 0);
r = frame3 & windows;
if(r.area()==windows.area())
rectangle(out, windows, Scalar(255), 1, 8, 0);
r = frame4 & windows;
if(r.area()==windows.area())
rectangle(out, windows, Scalar(255), 1, 8, 0);
}
}
//rectangle(out, windows, Scalar(255), 1, 8, 0);
seen[classes]=1;
}
//cout<<"Drawing Rectangle "<< blobs.size()<< endl;
//rectangle(out, windows, Scalar(255), 1, 8, 0);
//cout<<"Drew Rectangle "<< blobs.size()<< endl;
}
cout<<"Exited the function of refining the windows and extracting"<< endl;
}
/*
We will call this function only to extract static windows from the images
*/
void extractstaticwindowsrefined(Mat& img,Mat& diff,Mat &blob, Mat& out, vector<Point2f> *points, vector < vector<Point2i> > &blobs)
{
int seen[blobs.size()+1]={0};//1 if that component is already covered
//out=img.clone();
cvtColor(img, img, CV_BGR2GRAY);
vector<float> pts;//The HOG descriptors
Mat refout;
int classes=0;
int found=0,k=0;
int hogvalue=0;
int count=0;
Rect r1,r2,r3,r4,r;
for(int i=0;i<points[0].size();i++)
{
//cout<<"Inside the first loop"<< i<< points[0].size()<< endl;
count=count+1;
const Point2f& p1=points[0][i];
//cout<<round(p1.x)<<" "<<round(p1.y)<<" "<<blob.rows<<blob.cols<< " Count "<< count << endl;
classes = blob.at<int>(round(p1.y),round(p1.x));
//cout<<"class value "<<classes<<endl;
if(classes>1000)
continue;
//cout<< classes << "value" <<seen[classes] << endl;
//cout<<" Classes "<<classes<<endl;
Rect windows(p1.x, p1.y, widthrect, heightrect);
//cout<<" Classes "<<classes<<endl;
if(classes>=1 && seen[classes]==0)
{
//cout<<"class value "<<classes<<endl;
for(k=0;k<blobs[classes-1].size();k++)
{
int x = blobs[classes-1][k].x;
int y = blobs[classes-1][k].y;
//cout<<k<<blobs[classes].size()<<endl;
if(seen[classes]==0)
{
Rect windows2(x, y, widthrect,heightrect);
if(windows2.x + windows2.width<=img.cols && windows2.y + windows2.height<=img.rows)
windows = windows | windows2;
}
}
if(windows.x + windows.width<=img.cols && windows.y + windows.height<=img.rows)
{
Mat roi=img(windows).clone();
showhog(roi,refout,pts);
hogvalue=comparehog(pts);
Mat roidiff=diff(windows).clone();
double motion = cv::sum( roidiff )[0];
//hogvalue=1;
//cout << "Computing HOG "<< hogvalue<<"Motion Value "<<motion << endl;
if(hogvalue && motion <25000)
{
r1 = frame1 & windows;
/*if(r1.area()==windows.area())
{
rectangle(out, windows, Scalar(255,255,0), 1, 8, 0);
}*/
r2 = frame2 & windows;
/*if(r2.area()==windows.area())
{
rectangle(out, windows, Scalar(255,255,0), 1, 8, 0);
}*/
r3 = frame3 & windows;
/*if(r3.area()==windows.area())
{
rectangle(out, windows, Scalar(255,255,0), 1, 8, 0);
}*/
r4 = frame4 & windows;
/*if(r4.area()==windows.area())
{
rectangle(out, windows, Scalar(255,255,0), 1, 8, 0);
}*/
r = rects1 & windows;
if(r.area()> 0 && r1.area()==windows.area())
{
if(rectstat[1])
rectangle(out, rects1, Scalar(255,255,0), 1, 8, 0);//Static plus Moving cases
else
rectangle(out, rects1, Scalar(0,255,255), 1, 8, 0);
rectstat[1]=0;
}
r = rects2 & windows;
if(r.area()> 0 && r1.area()==windows.area())
{
if(rectstat[2])
rectangle(out, rects2, Scalar(255,255,0), 1, 8, 0);//Static plus Moving cases
else
rectangle(out, rects2, Scalar(0,255,255), 1, 8, 0);
rectstat[2]=0;
}
r = rects3 & windows;
if(r.area()> 0 && r1.area()==windows.area())
{
if(rectstat[3])
rectangle(out, rects3, Scalar(255,255,0), 1, 8, 0);//Static plus Moving cases
else
rectangle(out, rects3, Scalar(0,255,255), 1, 8, 0);
rectstat[3]=0;
}
r = rects4 & windows;
if(r.area()> 0 && r2.area()==windows.area())
{
if(rectstat[4])
rectangle(out, rects4, Scalar(255,255,0), 1, 8, 0);//Static plus Moving cases
else
rectangle(out, rects4, Scalar(0,255,255), 1, 8, 0);
rectstat[4]=0;
}
r = rects5 & windows;
if(r.area()> 0 && r2.area()==windows.area())
{
if(rectstat[5])
rectangle(out, rects5, Scalar(255,255,0), 1, 8, 0);//Static plus Moving cases
else
rectangle(out, rects5, Scalar(0,255,255), 1, 8, 0);
rectstat[5]=0;
}
r = rects6 & windows;
if(r.area()> 0 && r2.area()==windows.area())
{
if(rectstat[6])
rectangle(out, rects6, Scalar(255,255,0), 1, 8, 0);//Static plus Moving cases
else
rectangle(out, rects6, Scalar(0,255,255), 1, 8, 0);
rectstat[6]=0;
}
r = rects7 & windows;
if(r.area()> 0 && r3.area()==windows.area())
{
if(rectstat[7])
rectangle(out, rects7, Scalar(255,255,0), 1, 8, 0);//Static plus Moving cases
else
rectangle(out, rects7, Scalar(0,255,255), 1, 8, 0);
rectstat[7]=0;
}
r = rects8 & windows;
if(r.area()> 0 && r3.area()==windows.area())
{
if(rectstat[8])
rectangle(out, rects8, Scalar(255,255,0), 1, 8, 0);//Static plus Moving cases
else
rectangle(out, rects8, Scalar(0,255,255), 1, 8, 0);
rectstat[8]=0;
}
r = rects9 & windows;
if(r.area()> 0 && r3.area()==windows.area())
{
if(rectstat[9])
rectangle(out, rects9, Scalar(255,255,0), 1, 8, 0);//Static plus Moving cases
else
rectangle(out, rects9, Scalar(0,255,255), 1, 8, 0);
rectstat[9]=0;
}
r = rects10 & windows;
if(r.area()> 0 && r4.area()==windows.area())
{
if(rectstat[10])
rectangle(out, rects10, Scalar(255,255,0), 1, 8, 0);//Static plus Moving cases
else
rectangle(out, rects10, Scalar(0,255,255), 1, 8, 0);
rectstat[10]=0;
}
r = rects11 & windows;
if(r.area()> 0 && r4.area()==windows.area())
{
if(rectstat[11])
rectangle(out, rects11, Scalar(255,255,0), 1, 8, 0);//Static plus Moving cases
else
rectangle(out, rects11, Scalar(0,255,255), 1, 8, 0);
rectstat[11]=0;
}
r = rects12 & windows;
if(r.area()> 0 && r4.area()==windows.area())
{
if(rectstat[12])
rectangle(out, rects12, Scalar(255,255,0), 1, 8, 0);//Static plus Moving cases
else
rectangle(out, rects12, Scalar(0,255,255), 1, 8, 0);
rectstat[12]=0;
}
}
}
//rectangle(out, windows, Scalar(255), 1, 8, 0);
seen[classes]=1;
}
//cout<<"Drawing Rectangle "<< blobs.size()<< endl;
//rectangle(out, windows, Scalar(255), 1, 8, 0);
//cout<<"Drew Rectangle "<< blobs.size()<< endl;
}
//cout<<"Exited the function of refining the windows and extracting"<< endl;
}
void extractwindowsclose(Mat& img,Mat &blob, Mat& out, vector<Point2f> *points, vector < vector<Point2i> > &blobs)
{
out=img.clone();
const Point2f& p1=points[0][0];
Rect windows(p1.x, p1.y, widthrect, heightrect);
Mat refout;
int hogvalue=0;
vector<float> pts;//The HOG descriptors
for(int i=1;i<points[0].size();i++)
{
const Point2f& p1=points[0][i];
Rect windows2(p1.x, p1.y, widthrect, heightrect);
if(windows2.x + windows2.width<=img.cols && windows2.y + windows2.height<=img.rows)
{
windows = windows | windows2;
Mat roi=img(windows).clone();
showhog(roi,refout,pts);
hogvalue=comparehog(pts);
if(hogvalue)
rectangle(out, windows2, Scalar(255), 1, 8, 0);
}
}
//rectangle(out, windows, Scalar(255), 1, 8, 0);
}
/*Connected Components algorithm implementation taken from net*/
void FindBlobs(const Mat &binary,Mat &labels, vector < vector<Point2i> > &blobs)
{
blobs.clear();
// Fill the label_image with the blobs
// 0 - background
// 1 - unlabelled foreground
// 2+ - labelled foreground
labels=binary.clone();
Mat label_image;
binary.convertTo(label_image, CV_32SC1);
int label_count = 2; // starts at 2 because 0,1 are used already
for(int y=0; y < label_image.rows; y++) {
int *row = (int*)label_image.ptr(y);
for(int x=0; x < label_image.cols; x++) {
if(row[x] != 1) {
//cout<<"continuing!!"<<row[x]<<endl;
continue;
}
Rect rect;
//cout<<"Flood Filled"<<endl;
floodFill(label_image, Point(x,y), label_count, &rect, 0, 0, 4);
//cout<<"Flood Filled done"<<endl;
vector <Point2i> blob;
for(int i=rect.y; i < (rect.y+rect.height); i++) {
int *row2 = (int*)label_image.ptr(i);
for(int j=rect.x; j < (rect.x+rect.width); j++) {
if(row2[j] != label_count) {
continue;
}
//cout<<"assigning"<<endl;
labels.at<int>(i,j)=label_count;
blob.push_back(Point2i(j,i));
}
}
//cout<<"Pushing the blobs now "<<label_count<<endl;
blobs.push_back(blob);
label_count++;
}
}
//cout<<"Returning after finding all the blobs "<<endl;
}