-
Notifications
You must be signed in to change notification settings - Fork 19
/
process.cpp
474 lines (423 loc) · 11.7 KB
/
process.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
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
//
// process.cpp
// final_assignment
//
// Created by william wei on 17/1/7.
// Copyright © 2017年 simon. All rights reserved.
//
#include "process.hpp"
#include <unistd.h>
#include <dirent.h>
#include <stdlib.h>
#include <opencv2/core/core.hpp>
#include <opencv2/opencv.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/objdetect/objdetect.hpp>
#include <opencv2/ml/ml.hpp>
#include "helper.hpp"
using namespace std;
using namespace cv;
/**
* 颜色过滤,去除非蓝红以外的颜色
*
*/
Mat filterColor(const Mat &inputImage)
{
Mat hsvImage;
cvtColor(inputImage, hsvImage, CV_BGR2HSV);
Mat resultGray = Mat(hsvImage.rows, hsvImage.cols,CV_8U,cv::Scalar(255));
//Mat resultColor = Mat(hsvImage.rows, hsvImage.cols,CV_8UC3,cv::Scalar(255, 255, 255));
double H=0.0,S=0.0,V=0.0;
for(int i=0;i<hsvImage.rows;i++)
{
for(int j=0;j<hsvImage.cols;j++)
{
H=hsvImage.at<Vec3b>(i,j)[0];
S=hsvImage.at<Vec3b>(i,j)[1];
V=hsvImage.at<Vec3b>(i,j)[2];
if((S >= 20 && S<=255))
{
if((( H>=0 && H <= 10) ||(H<=180&&H>=125) || (H>=100&&H<=124))&& V >= 0 && V <=255)
{
resultGray.at<uchar>(i,j)=0;
}
}
}
}
for (int i=0; i<10; i++) {
resultGray = 255 - resultGray;
overSpread(resultGray,10,10);
resultGray = 255 - resultGray;
overSpread(resultGray,10,10);
}
//resultGray = 255 - resultGray;
overSpread(resultGray,20,50);
return resultGray;
}
/**
* 过滤异常大小的识别矩形
*
*/
vector<Rect> filterRect(vector<Rect> R)
{
vector<Rect>result;
for (int i=0; i<R.size(); i++)
{
if(R[i].width<=RECT_WIDTH_LIMIT)
result.push_back(R[i]);
}
return result;
}
vector<Rect> filterRect(const Mat inputImage,vector<Rect>found)
{
Mat src = inputImage;
vector<Rect> result;
for (size_t i = 0; i < found.size(); i++)
{
if(found[i].width > RECT_WIDTH_LIMIT)
{
continue;
}
int pixel_sum=0;
int x_coord=found[i].x;
int y_coord=found[i].y;
x_coord = x_coord<0?0:x_coord;
y_coord = y_coord<0?0:y_coord;
int width=found[i].width;
int height=found[i].height;
for(int x=x_coord; x<x_coord+width && x<inputImage.cols; x++)
{
for(int y=y_coord; y<y_coord+height && y<inputImage.rows; y++)
{
if(src.at<uchar>(y,x)<255)
{
pixel_sum++;
}
}
if (pixel_sum/(width*height*1.0)>AREA_THRESHOLD) {
result.push_back(found[i]);
}
}
}
return result;
}
/**
* 将过细的部分膨胀铺展开
*
*/
void overSpread(Mat &mask,int width,int height,int threshold)
{
int dilate_ele_size=30;
Mat ele = getStructuringElement(MORPH_RECT, Size(width, height),
Point(width/2, height/2));
erode(mask, mask, ele);
}
/**
* 单峰过滤
*
*/
vector<Rect> filterSinglePeak(vector<Rect> R)
{
if(R.size()==0)
return R;
int WD = 1.5;
int Asize = R.size();
int avew = 0; int aveh = 0; int rx = 0;int dy = 0;
int lx = 9999;int uy = 9999;
for (int i = 0; i < Asize; i++){
avew = avew + R[i].width;
aveh = aveh + R[i].height;
if (lx>R[i].x)
lx = R[i].x;
if (uy > R[i].y)
uy = R[i].y;
if (rx < R[i].x + R[i].width)
rx = R[i].x+R[i].width;
if (dy < R[i].y + R[i].height)
dy = R[i].y + R[i].height;
}
avew = avew / Asize;
aveh = aveh / Asize;
int choose, max = 0;
int in = 0;
for (int i = 0; i < Asize; i++){
int tlx = R[i].x;
for (int j = 0; j < Asize; j++){
if (R[j].x >= tlx && R[j].x < tlx + WD * avew)
in++;
}
if (in>max)
{
max = in;
choose = i;
}
in = 0;
}
int tlx = R[choose].x;
vector < Rect > result;
for (int i = 0; i < Asize; i++){
if (R[i].x >= tlx && R[i].x < tlx + WD * avew)
{
result.push_back(R[i]);
//printf("%d %d %d ", R[i].x, tlx, tlx + 2 * avew);
}
}
return result;
}
/**
* 将各个mask进行叠加,得到最终的mask
*
*/
Mat mergeMasks(vector<Mat> Vmat){
int recl = 3;
int all = recl*recl;
int size = Vmat.size();
int Rows= Vmat[0].rows;
int Cols = Vmat[0].cols;
double percent = 0.75;
int temp = 0;
int count = all*percent;
bool control = true;
Mat result(Rows, Cols,CV_8U,Scalar(255));
for (int i = 0; i < Rows-recl; i = i + recl)
{
for (int j = 0; j < Cols-recl; j = j + recl)
{
for (int p = 0; p < recl; p++)
{
for (int q = 0; q < recl; q++)
{
for (int k = 0; k < size; k++)
{
if (Vmat[k].at<uchar>(i + p, j + q) > 128)
control = false;
}
if (control == true)
temp++;
control = true;
}
}
//printf("%d", temp);
if (temp >= count){
//printf("%d", temp);
for (int t = 0; t < recl; t++)
{
for (int s = 0; s < recl; s++){
//printf("%d", temp);
for (int z = 0; z < size; z++){
if (Vmat[z].at<uchar>(i + t, j + s) < 128)
{
result.at<uchar>(i + t, j + s) = 0;
//printf("%d", temp);
}
}
}
}
}
temp = 0;
}
}
return result;
}
/**
* 基于Canny边缘检测的过滤
*
*/
Mat filterCanny(const Mat inputImage)
{
Mat origin = inputImage;
GaussianBlur(origin, origin, Size(5, 5), 0, 0);
Mat result = Mat(inputImage.rows, inputImage.cols,CV_8U,cv::Scalar(255));
//blur(origin,origin,Size(3,3));
Canny(origin, origin, 50, 100, 3);
origin = 255 - origin;
overSpread(origin,35,70);
return origin;
}
bool CompRectY(Rect A,Rect B)
{
if (A.y > B.y)
return true;
else
return false;
}
bool CompRectX(Rect A, Rect B)
{
if (A.x < B.x)
return true;
else
return false;
}
vector<Rect> adjustOneRect(vector<Rect>vecrec,int ave)
{
int Size = vecrec.size();
vector<Rect>result;
if (Size == 1)
return vecrec;
result.push_back(vecrec[0]);
int iter = 0;
int iter2 = Size - 1;
double mul = 0;
for (int i = 1; i < Size; i++){
mul = (vecrec[iter].y - vecrec[i].y)*1.0 / (ave*1.0);
if (int(mul + 0.5) % 2 == 1 || int(mul + 0.5)==1)
continue;
else
{
//iter = i;
for (int j = 1; j <= (int(mul + 0.5))/2; j++)
result.push_back(Rect((vecrec[iter].x + vecrec[i].x) / 2, vecrec[iter].y - 2*j*ave, ave, ave));
iter = i;
}
}
return result;
}
vector<Rect> adjustTwoRect(vector<Rect>vecrec, int ave, int leftx)
{
int Size = vecrec.size();
vector<Rect>result;
if (Size == 1)
return vecrec;
result.push_back(vecrec[0]);
int iter = 0;
int iter2 = Size - 1;
double mul = 0;
bool left = false;
if (vecrec[0].x > leftx + ave / 2)
left = true;
for (int i = 1; i < Size; i++){
mul = (vecrec[iter].y - vecrec[i].y)*1.0 / (ave*1.0);
if (int(mul + 0.5) == 1)
continue;
else
{
//iter = i;
for (int j = 1; j <= (int(mul + 0.5)); j++)
{
if (left == false)
result.push_back(Rect(leftx+ave, vecrec[iter].y - j*ave, ave, ave));
else
result.push_back(Rect(leftx, vecrec[iter].y - j*ave, ave, ave));
left = !left;
}
iter = i;
}
}
return result;
}
/**
* 对识别结果进行调整补齐
*
*/
int adjustRect(vector<Rect> &vecrec)
{
int mode = 1;
int Size = vecrec.size();
int ave=0;
for (int i = 0; i < Size; i++)
ave = ave + vecrec[i].height;
if(Size==0)
return 0;
ave = ave / Size;
//printf("%d", ave);
sort(vecrec.begin(), vecrec.end(), CompRectX);
//printf("%d \n", vecrec[vecrec.size() - 1].x - vecrec[0].x);
if (vecrec[vecrec.size() - 1].x - vecrec[0].x > ave / 2)
mode = 2;
//printf("%d", mode);
if (mode == 1){
sort(vecrec.begin(), vecrec.end(), CompRectY);
int iter = 0;
int iter2 = Size - 1;
double mul = 0;
for (int i = 0; i < Size - 1; i++){
mul = (vecrec[i].y - vecrec[i + 1].y)*1.0 / (ave*1.0);
if (int(mul + 0.5) <= 3)
{
iter = i;
break;
}
}
for (int i = Size - 1; i <0; i--){
mul = (vecrec[i - 1].y - vecrec[i].y)*1.0 / (ave*1.0);
if (int(mul + 0.5) <= 3)
{
iter2 = i;
break;
}
}
vector<Rect> result;
for (int i = iter; i < iter2; i++)
{
result.push_back(vecrec[i]);
}
result = adjustOneRect(result, ave);
vecrec = result;
return 2*vecrec.size();
}
if (mode == 2)
{
vector<Rect>left;
vector<Rect>right;
int leftx = vecrec[0].x;
sort(vecrec.begin(), vecrec.end(), CompRectY);
int iter = 0;
int iter2 = Size - 1;
double mul = 0;
for (int i =0; i < Size-1; i++){
mul = (vecrec[i].y - vecrec[i+1].y)*1.0 / (ave*1.0);
if (int(mul + 0.5) <= 3)
{
iter = i;
break;
}
}
for (int i = Size-1; i <0; i--){
mul = (vecrec[i-1].y - vecrec[i].y)*1.0 / (ave*1.0);
if (int(mul + 0.5) <= 3)
{
iter2 = i;
break;
}
}
for (int i = iter; i < iter2; i++)
{
if (vecrec[i].x < leftx + ave / 2)
{
left.push_back(vecrec[i]);
//printf("%d %d \n", left.size(),vecrec[i].x);
}
else
right.push_back(vecrec[i]);
}
//printf("%d \n", left.size());
left = adjustOneRect(left,ave);
right = adjustOneRect(right,ave);
//printf("%d %d \n", left.size(),right.size());
//printf("%d \n", left.size());
sort(left.begin(), left.end(), CompRectY);
int sumE = (max(left[0].y, right[0].y) - min(left[left.size() - 1].y, right[right.size() - 1].y)) / ave;
if (abs(left[0].y - right[0].y)> 1.5*ave||abs(left[left.size() - 1].y - right[right.size() - 1].y) > 1.5*ave){
for (int i = 0; i < right.size(); i++)
left.push_back(right[i]);
left=adjustTwoRect(left, ave, leftx);
}
else{
for (int i = 0; i < right.size(); i++)
left.push_back(right[i]);
}
//printf("%d \n", left.size());
vecrec = left;
return sumE;
}
return 1;
};
/**
* 线性拟合调整函数
*/
int fitting(float input)
{
return (int)-0.0003*input*input*input+0.0308*input*input+0.6015*input+6.2104;
//return input;
}