-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Expand file tree
/
Copy pathcore_func.cpp
More file actions
2569 lines (2068 loc) · 81 KB
/
core_func.cpp
File metadata and controls
2569 lines (2068 loc) · 81 KB
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
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "easypr/core/core_func.h"
#include "easypr/core/plate.hpp"
#include "easypr/core/chars_identify.h"
#include "easypr/config.h"
#include "easypr/core/params.h"
#include "thirdparty/mser/mser2.hpp"
#include <ctime>
namespace easypr {
Mat colorMatch(const Mat &src, Mat &match, const Color r,
const bool adaptive_minsv) {
// if use adaptive_minsv
// min value of s and v is adaptive to h
const float max_sv = 255;
const float minref_sv = 64;
const float minabs_sv = 95; //95;
// H range of blue
const int min_blue = 100; // 100
const int max_blue = 140; // 140
// H range of yellow
const int min_yellow = 15; // 15
const int max_yellow = 40; // 40
// H range of white
const int min_white = 0; // 15
const int max_white = 30; // 40
Mat src_hsv;
// convert to HSV space
cvtColor(src, src_hsv, CV_BGR2HSV);
std::vector<cv::Mat> hsvSplit;
split(src_hsv, hsvSplit);
equalizeHist(hsvSplit[2], hsvSplit[2]);
merge(hsvSplit, src_hsv);
// match to find the color
int min_h = 0;
int max_h = 0;
switch (r) {
case BLUE:
min_h = min_blue;
max_h = max_blue;
break;
case YELLOW:
min_h = min_yellow;
max_h = max_yellow;
break;
case WHITE:
min_h = min_white;
max_h = max_white;
break;
default:
// Color::UNKNOWN
break;
}
float diff_h = float((max_h - min_h) / 2);
float avg_h = min_h + diff_h;
int channels = src_hsv.channels();
int nRows = src_hsv.rows;
// consider multi channel image
int nCols = src_hsv.cols * channels;
if (src_hsv.isContinuous()) {
nCols *= nRows;
nRows = 1;
}
int i, j;
uchar* p;
float s_all = 0;
float v_all = 0;
float count = 0;
for (i = 0; i < nRows; ++i) {
p = src_hsv.ptr<uchar>(i);
for (j = 0; j < nCols; j += 3) {
int H = int(p[j]); // 0-180
int S = int(p[j + 1]); // 0-255
int V = int(p[j + 2]); // 0-255
s_all += S;
v_all += V;
count++;
bool colorMatched = false;
if (H > min_h && H < max_h) {
float Hdiff = 0;
if (H > avg_h)
Hdiff = H - avg_h;
else
Hdiff = avg_h - H;
float Hdiff_p = float(Hdiff) / diff_h;
float min_sv = 0;
if (true == adaptive_minsv)
min_sv =
minref_sv -
minref_sv / 2 *
(1
- Hdiff_p); // inref_sv - minref_sv / 2 * (1 - Hdiff_p)
else
min_sv = minabs_sv; // add
if ((S > min_sv && S < max_sv) && (V > min_sv && V < max_sv))
colorMatched = true;
}
if (colorMatched == true) {
p[j] = 0;
p[j + 1] = 0;
p[j + 2] = 255;
}
else {
p[j] = 0;
p[j + 1] = 0;
p[j + 2] = 0;
}
}
}
// cout << "avg_s:" << s_all / count << endl;
// cout << "avg_v:" << v_all / count << endl;
// get the final binary
Mat src_grey;
std::vector<cv::Mat> hsvSplit_done;
split(src_hsv, hsvSplit_done);
src_grey = hsvSplit_done[2];
match = src_grey;
return src_grey;
}
bool bFindLeftRightBound1(Mat &bound_threshold, int &posLeft, int &posRight) {
float span = bound_threshold.rows * 0.2f;
for (int i = 0; i < bound_threshold.cols - span - 1; i += 3) {
int whiteCount = 0;
for (int k = 0; k < bound_threshold.rows; k++) {
for (int l = i; l < i + span; l++) {
if (bound_threshold.data[k * bound_threshold.step[0] + l] == 255) {
whiteCount++;
}
}
}
if (whiteCount * 1.0 / (span * bound_threshold.rows) > 0.15) {
posLeft = i;
break;
}
}
span = bound_threshold.rows * 0.2f;
for (int i = bound_threshold.cols - 1; i > span; i -= 2) {
int whiteCount = 0;
for (int k = 0; k < bound_threshold.rows; k++) {
for (int l = i; l > i - span; l--) {
if (bound_threshold.data[k * bound_threshold.step[0] + l] == 255) {
whiteCount++;
}
}
}
if (whiteCount * 1.0 / (span * bound_threshold.rows) > 0.06) {
posRight = i;
if (posRight + 5 < bound_threshold.cols) {
posRight = posRight + 5;
} else {
posRight = bound_threshold.cols - 1;
}
break;
}
}
if (posLeft < posRight) {
return true;
}
return false;
}
bool bFindLeftRightBound(Mat &bound_threshold, int &posLeft, int &posRight) {
float span = bound_threshold.rows * 0.2f;
for (int i = 0; i < bound_threshold.cols - span - 1; i += 2) {
int whiteCount = 0;
for (int k = 0; k < bound_threshold.rows; k++) {
for (int l = i; l < i + span; l++) {
if (bound_threshold.data[k * bound_threshold.step[0] + l] == 255) {
whiteCount++;
}
}
}
if (whiteCount * 1.0 / (span * bound_threshold.rows) > 0.36) {
posLeft = i;
break;
}
}
span = bound_threshold.rows * 0.2f;
for (int i = bound_threshold.cols - 1; i > span; i -= 2) {
int whiteCount = 0;
for (int k = 0; k < bound_threshold.rows; k++) {
for (int l = i; l > i - span; l--) {
if (bound_threshold.data[k * bound_threshold.step[0] + l] == 255) {
whiteCount++;
}
}
}
if (whiteCount * 1.0 / (span * bound_threshold.rows) > 0.26) {
posRight = i;
break;
}
}
if (posLeft < posRight) {
return true;
}
return false;
}
bool bFindLeftRightBound2(Mat &bound_threshold, int &posLeft, int &posRight) {
float span = bound_threshold.rows * 0.2f;
for (int i = 0; i < bound_threshold.cols - span - 1; i += 3) {
int whiteCount = 0;
for (int k = 0; k < bound_threshold.rows; k++) {
for (int l = i; l < i + span; l++) {
if (bound_threshold.data[k * bound_threshold.step[0] + l] == 255) {
whiteCount++;
}
}
}
if (whiteCount * 1.0 / (span * bound_threshold.rows) > 0.32) {
posLeft = i;
break;
}
}
span = bound_threshold.rows * 0.2f;
for (int i = bound_threshold.cols - 1; i > span; i -= 3) {
int whiteCount = 0;
for (int k = 0; k < bound_threshold.rows; k++) {
for (int l = i; l > i - span; l--) {
if (bound_threshold.data[k * bound_threshold.step[0] + l] == 255) {
whiteCount++;
}
}
}
if (whiteCount * 1.0 / (span * bound_threshold.rows) > 0.22) {
posRight = i;
break;
}
}
if (posLeft < posRight) {
return true;
}
return false;
}
bool plateColorJudge(const Mat &src, const Color r, const bool adaptive_minsv,
float &percent) {
const float thresh = 0.45f;
Mat src_gray;
colorMatch(src, src_gray, r, adaptive_minsv);
percent =
float(countNonZero(src_gray)) / float(src_gray.rows * src_gray.cols);
// cout << "percent:" << percent << endl;
if (percent > thresh)
return true;
else
return false;
}
Color getPlateType(const Mat &src, const bool adaptive_minsv) {
float max_percent = 0;
Color max_color = UNKNOWN;
float blue_percent = 0;
float yellow_percent = 0;
float white_percent = 0;
if (plateColorJudge(src, BLUE, adaptive_minsv, blue_percent) == true) {
// cout << "BLUE" << endl;
return BLUE;
} else if (plateColorJudge(src, YELLOW, adaptive_minsv, yellow_percent) ==
true) {
// cout << "YELLOW" << endl;
return YELLOW;
} else if (plateColorJudge(src, WHITE, adaptive_minsv, white_percent) ==
true) {
// cout << "WHITE" << endl;
return WHITE;
} else {
//std::cout << "OTHER" << std::endl;
/*max_percent = blue_percent > yellow_percent ? blue_percent : yellow_percent;
max_color = blue_percent > yellow_percent ? BLUE : YELLOW;
max_color = max_percent > white_percent ? max_color : WHITE;*/
// always return blue
return BLUE;
}
}
void clearLiuDingOnly(Mat &img) {
const int x = 7;
Mat jump = Mat::zeros(1, img.rows, CV_32F);
for (int i = 0; i < img.rows; i++) {
int jumpCount = 0;
int whiteCount = 0;
for (int j = 0; j < img.cols - 1; j++) {
if (img.at<char>(i, j) != img.at<char>(i, j + 1)) jumpCount++;
if (img.at<uchar>(i, j) == 255) {
whiteCount++;
}
}
jump.at<float>(i) = (float) jumpCount;
}
for (int i = 0; i < img.rows; i++) {
if (jump.at<float>(i) <= x) {
for (int j = 0; j < img.cols; j++) {
img.at<char>(i, j) = 0;
}
}
}
}
bool clearLiuDing(Mat &img) {
std::vector<float> fJump;
int whiteCount = 0;
const int x = 7;
Mat jump = Mat::zeros(1, img.rows, CV_32F);
for (int i = 0; i < img.rows; i++) {
int jumpCount = 0;
for (int j = 0; j < img.cols - 1; j++) {
if (img.at<char>(i, j) != img.at<char>(i, j + 1)) jumpCount++;
if (img.at<uchar>(i, j) == 255) {
whiteCount++;
}
}
jump.at<float>(i) = (float) jumpCount;
}
int iCount = 0;
for (int i = 0; i < img.rows; i++) {
fJump.push_back(jump.at<float>(i));
if (jump.at<float>(i) >= 16 && jump.at<float>(i) <= 45) {
// jump condition
iCount++;
}
}
// if not is not plate
if (iCount * 1.0 / img.rows <= 0.40) {
return false;
}
if (whiteCount * 1.0 / (img.rows * img.cols) < 0.15 ||
whiteCount * 1.0 / (img.rows * img.cols) > 0.50) {
return false;
}
for (int i = 0; i < img.rows; i++) {
if (jump.at<float>(i) <= x) {
for (int j = 0; j < img.cols; j++) {
img.at<char>(i, j) = 0;
}
}
}
return true;
}
void clearBorder(const Mat &img, Rect& cropRect) {
int r = img.rows;
int c = img.cols;
Mat boder = Mat::zeros(1, r, CV_8UC1);
const int noJunpCount_thresh = int(0.15f * c);
// if nojumpcount >
for (int i = 0; i < r; i++) {
int nojumpCount = 0;
int isBorder = 0;
for (int j = 0; j < c - 1; j++) {
if (img.at<char>(i, j) == img.at<char>(i, j + 1))
nojumpCount++;
if (nojumpCount > noJunpCount_thresh) {
nojumpCount = 0;
isBorder = 1;
break;
}
}
boder.at<char>(i) = (char) isBorder;
}
const int mintop = int(0.1f * r);
const int maxtop = int(0.9f * r);
int minMatTop = 0;
int maxMatTop = r - 1;
for (int i = 0; i < mintop; i++) {
if (boder.at<char>(i) == 1) {
minMatTop = i;
}
}
for (int i = r - 1; i > maxtop; i--) {
if (boder.at<char>(i) == 1) {
maxMatTop = i;
}
}
cropRect = Rect(0, minMatTop, c, maxMatTop - minMatTop + 1);
}
void clearLiuDing(Mat mask, int &top, int &bottom) {
const int x = 7;
for (int i = 0; i < mask.rows / 2; i++) {
int whiteCount = 0;
int jumpCount = 0;
for (int j = 0; j < mask.cols - 1; j++) {
if (mask.at<char>(i, j) != mask.at<char>(i, j + 1)) jumpCount++;
if ((int) mask.at<uchar>(i, j) == 255) {
whiteCount++;
}
}
if ((jumpCount < x && whiteCount * 1.0 / mask.cols > 0.15) ||
whiteCount < 4) {
top = i;
}
}
top -= 1;
if (top < 0) {
top = 0;
}
// ok, find top and bottom boudnadry
for (int i = mask.rows - 1; i >= mask.rows / 2; i--) {
int jumpCount = 0;
int whiteCount = 0;
for (int j = 0; j < mask.cols - 1; j++) {
if (mask.at<char>(i, j) != mask.at<char>(i, j + 1)) jumpCount++;
if (mask.at<uchar>(i, j) == 255) {
whiteCount++;
}
}
if ((jumpCount < x && whiteCount * 1.0 / mask.cols > 0.15) ||
whiteCount < 4) {
bottom = i;
}
}
bottom += 1;
if (bottom >= mask.rows) {
bottom = mask.rows - 1;
}
if (top >= bottom) {
top = 0;
bottom = mask.rows - 1;
}
}
int ThresholdOtsu(Mat mat) {
int height = mat.rows;
int width = mat.cols;
// histogram
float histogram[256] = {0};
for (int i = 0; i < height; i++) {
for (int j = 0; j < width; j++) {
unsigned char p = (unsigned char) ((mat.data[i * mat.step[0] + j]));
histogram[p]++;
}
}
// normalize histogram
int size = height * width;
for (int i = 0; i < 256; i++) {
histogram[i] = histogram[i] / size;
}
// average pixel value
float avgValue = 0;
for (int i = 0; i < 256; i++) {
avgValue += i * histogram[i];
}
int thresholdV = 0;
float maxVariance = 0;
float w = 0, u = 0;
for (int i = 0; i < 256; i++) {
w += histogram[i];
u += i * histogram[i];
float t = avgValue * w - u;
float variance = t * t / (w * (1 - w));
if (variance > maxVariance) {
maxVariance = variance;
thresholdV = i;
}
}
return thresholdV;
}
Mat histeq(Mat in) {
Mat out(in.size(), in.type());
if (in.channels() == 3) {
Mat hsv;
std::vector<cv::Mat> hsvSplit;
cvtColor(in, hsv, CV_BGR2HSV);
split(hsv, hsvSplit);
equalizeHist(hsvSplit[2], hsvSplit[2]);
merge(hsvSplit, hsv);
cvtColor(hsv, out, CV_HSV2BGR);
} else if (in.channels() == 1) {
equalizeHist(in, out);
}
return out;
}
#define HORIZONTAL 1
#define VERTICAL 0
Mat CutTheRect(Mat &in, Rect &rect) {
int size = in.cols; // (rect.width>rect.height)?rect.width:rect.height;
Mat dstMat(size, size, CV_8UC1);
dstMat.setTo(Scalar(0, 0, 0));
int x = (int) floor((float) (size - rect.width) / 2.0f);
int y = (int) floor((float) (size - rect.height) / 2.0f);
for (int i = 0; i < rect.height; ++i) {
for (int j = 0; j < rect.width; ++j) {
dstMat.data[dstMat.step[0] * (i + y) + j + x] =
in.data[in.step[0] * (i + rect.y) + j + rect.x];
}
}
//
return dstMat;
}
Rect GetCenterRect(Mat &in) {
Rect _rect;
int top = 0;
int bottom = in.rows - 1;
// find the center rect
for (int i = 0; i < in.rows; ++i) {
bool bFind = false;
for (int j = 0; j < in.cols; ++j) {
if (in.data[i * in.step[0] + j] > 20) {
top = i;
bFind = true;
break;
}
}
if (bFind) {
break;
}
}
for (int i = in.rows - 1;
i >= 0;
--i) {
bool bFind = false;
for (int j = 0; j < in.cols; ++j) {
if (in.data[i * in.step[0] + j] > 20) {
bottom = i;
bFind = true;
break;
}
}
if (bFind) {
break;
}
}
int left = 0;
int right = in.cols - 1;
for (int j = 0; j < in.cols; ++j) {
bool bFind = false;
for (int i = 0; i < in.rows; ++i) {
if (in.data[i * in.step[0] + j] > 20) {
left = j;
bFind = true;
break;
}
}
if (bFind) {
break;
}
}
for (int j = in.cols - 1;
j >= 0;
--j) {
bool bFind = false;
for (int i = 0; i < in.rows; ++i) {
if (in.data[i * in.step[0] + j] > 20) {
right = j;
bFind = true;
break;
}
}
if (bFind) {
break;
}
}
_rect.x = left;
_rect.y = top;
_rect.width = right - left + 1;
_rect.height = bottom - top + 1;
return _rect;
}
float countOfBigValue(Mat &mat, int iValue) {
float iCount = 0.0;
if (mat.rows > 1) {
for (int i = 0; i < mat.rows; ++i) {
if (mat.data[i * mat.step[0]] > iValue) {
iCount += 1.0;
}
}
return iCount;
} else {
for (int i = 0; i < mat.cols; ++i) {
if (mat.data[i] > iValue) {
iCount += 1.0;
}
}
return iCount;
}
}
Mat ProjectedHistogram(Mat img, int t, int threshold) {
int sz = (t) ? img.rows : img.cols;
Mat mhist = Mat::zeros(1, sz, CV_32F);
for (int j = 0; j < sz; j++) {
Mat data = (t) ? img.row(j) : img.col(j);
mhist.at<float>(j) = countOfBigValue(data, threshold);
}
// Normalize histogram
double min, max;
minMaxLoc(mhist, &min, &max);
if (max > 0)
mhist.convertTo(mhist, -1, 1.0f / max, 0);
return mhist;
}
Mat showHistogram(const Mat &hist) {
int height = 32;
int width = hist.cols;
Mat show = Mat::zeros(height, width, CV_8UC1);
for (int i = 0; i < width; i++) {
int len = int((float) height * hist.at<float>(i));
for (int j = height - 1; j >= 0; j--) {
if (height - j <= len)
show.at<char>(j, i) = (char) 255;
}
}
return show;
}
Mat preprocessChar(Mat in, int char_size) {
// Remap image
int h = in.rows;
int w = in.cols;
int charSize = char_size;
Mat transformMat = Mat::eye(2, 3, CV_32F);
int m = max(w, h);
transformMat.at<float>(0, 2) = float(m / 2 - w / 2);
transformMat.at<float>(1, 2) = float(m / 2 - h / 2);
Mat warpImage(m, m, in.type());
warpAffine(in, warpImage, transformMat, warpImage.size(), INTER_LINEAR,
BORDER_CONSTANT, Scalar(0));
Mat out;
cv::resize(warpImage, out, Size(charSize, charSize));
return out;
}
Rect GetChineseRect(const Rect rectSpe) {
int height = rectSpe.height;
float newwidth = rectSpe.width * 1.10f;
int x = rectSpe.x;
int y = rectSpe.y;
int newx = x - int(newwidth * 1.10f);
newx = newx > 0 ? newx : 0;
Rect a(newx, y, int(newwidth), height);
return a;
}
bool verifyCharSizes(Rect r) {
// Char sizes 45x90
float aspect = 45.0f / 90.0f;
float charAspect = (float) r.width / (float) r.height;
float error = 0.35f;
float minHeight = 25.f;
float maxHeight = 50.f;
// We have a different aspect ratio for number 1, and it can be ~0.2
float minAspect = 0.05f;
float maxAspect = aspect + aspect * error;
// bb area
int bbArea = r.width * r.height;
if (charAspect > minAspect && charAspect < maxAspect /*&&
r.rows >= minHeight && r.rows < maxHeight*/)
return true;
else
return false;
}
Mat scaleImage(const Mat &image, const Size &maxSize, double &scale_ratio) {
Mat ret;
if (image.cols > maxSize.width || image.rows > maxSize.height) {
double widthRatio = image.cols / (double) maxSize.width;
double heightRatio = image.rows / (double) maxSize.height;
double m_real_to_scaled_ratio = max(widthRatio, heightRatio);
int newWidth = int(image.cols / m_real_to_scaled_ratio);
int newHeight = int(image.rows / m_real_to_scaled_ratio);
cv::resize(image, ret, Size(newWidth, newHeight), 0, 0);
scale_ratio = m_real_to_scaled_ratio;
} else {
ret = image;
scale_ratio = 1.0;
}
return ret;
}
// Scale back RotatedRect
RotatedRect scaleBackRRect(const RotatedRect &rr, const float scale_ratio) {
float width = rr.size.width * scale_ratio;
float height = rr.size.height * scale_ratio;
float x = rr.center.x * scale_ratio;
float y = rr.center.y * scale_ratio;
RotatedRect mserRect(Point2f(x, y), Size2f(width, height), rr.angle);
return mserRect;
}
bool verifyPlateSize(Rect mr) {
float error = 0.6f;
// Spain car plate size: 52x11 aspect 4,7272
// China car plate size: 440mm*140mm,aspect 3.142857
// Real car plate size: 136 * 32, aspect 4
float aspect = 3.75;
// Set a min and max area. All other patchs are discarded
// int min= 1*aspect*1; // minimum area
// int max= 2000*aspect*2000; // maximum area
int min = 34 * 8 * 1; // minimum area
int max = 34 * 8 * 200; // maximum area
// Get only patchs that match to a respect ratio.
float rmin = aspect - aspect * error;
float rmax = aspect + aspect * error;
float area = float(mr.height * mr.width);
float r = (float) mr.width / (float) mr.height;
if (r < 1) r = (float) mr.height / (float) mr.width;
// cout << "area:" << area << endl;
// cout << "r:" << r << endl;
if ((area < min || area > max) || (r < rmin || r > rmax))
return false;
else
return true;
}
bool verifyRotatedPlateSizes(RotatedRect mr, bool showDebug) {
float error = 0.65f;
// Spain car plate size: 52x11 aspect 4,7272
// China car plate size: 440mm*140mm,aspect 3.142857
// Real car plate size: 136 * 32, aspect 4
float aspect = 3.75f;
// Set a min and max area. All other patchs are discarded
// int min= 1*aspect*1; // minimum area
// int max= 2000*aspect*2000; // maximum area
//int min = 34 * 8 * 1; // minimum area
//int max = 34 * 8 * 200; // maximum area
// Get only patchs that match to a respect ratio.
float aspect_min = aspect - aspect * error;
float aspect_max = aspect + aspect * error;
float width_max = 600.f;
float width_min = 30.f;
float min = float(width_min * width_min / aspect_max); // minimum area
float max = float(width_max * width_max / aspect_min); // maximum area
float width = mr.size.width;
float height = mr.size.height;
float area = width * height;
float ratio = width / height;
float angle = mr.angle;
if (ratio < 1) {
swap(width, height);
ratio = width / height;
angle = 90.f + angle;
//std::cout << "angle:" << angle << std::endl;
}
float angle_min = -60.f;
float angle_max = 60.f;
//std::cout << "aspect_min:" << aspect_min << std::endl;
//std::cout << "aspect_max:" << aspect_max << std::endl;
if (area < min || area > max) {
if (0 && showDebug) {
std::cout << "area < min || area > max: " << area << std::endl;
}
return false;
} else if (ratio < aspect_min || ratio > aspect_max) {
if (0 && showDebug) {
std::cout << "ratio < aspect_min || ratio > aspect_max: " << ratio << std::endl;
}
return false;
} else if (angle < angle_min || angle > angle_max) {
if (0 && showDebug) {
std::cout << "angle < angle_min || angle > angle_max: " << angle << std::endl;
}
return false;
} else if (width < width_min || width > width_max) {
if (0 && showDebug) {
std::cout << "width < width_min || width > width_max: " << width << std::endl;
}
return false;
} else {
return true;
}
return true;
}
//! non-maximum suppression
void NMStoCharacter(std::vector<CCharacter> &inVec, double overlap) {
std::sort(inVec.begin(), inVec.end());
std::vector<CCharacter>::iterator it = inVec.begin();
for (; it != inVec.end(); ++it) {
CCharacter charSrc = *it;
//std::cout << "plateScore:" << plateSrc.getPlateScore() << std::endl;
Rect rectSrc = charSrc.getCharacterPos();
std::vector<CCharacter>::iterator itc = it + 1;
for (; itc != inVec.end();) {
CCharacter charComp = *itc;
Rect rectComp = charComp.getCharacterPos();
//Rect rectInter = rectSrc & rectComp;
//Rect rectUnion = rectSrc | rectComp;
//double r = double(rectInter.area()) / double(rectUnion.area());
float iou = computeIOU(rectSrc, rectComp);
if (iou > overlap) {
itc = inVec.erase(itc);
} else {
++itc;
}
}
}
}
// judge weather two CCharacter are nearly the same;
bool compareCharRect(const CCharacter &character1, const CCharacter &character2) {
Rect rect1 = character1.getCharacterPos();
Rect rect2 = character2.getCharacterPos();
// the character in plate are similar height
float width_1 = float(rect1.width);
float height_1 = float(rect1.height);
float width_2 = float(rect2.width);
float height_2 = float(rect2.height);
float height_diff = abs(height_1 - height_2);
double height_diff_ratio = height_diff / min(height_1, height_2);
if (height_diff_ratio > 0.25)
return false;
// the character in plate are similar in the y-axis
float y_1 = float(rect1.tl().y);
float y_2 = float(rect2.tl().y);
float y_diff = abs(y_1 - y_2);
double y_diff_ratio = y_diff / min(height_1, height_2);
if (y_diff_ratio > 0.5)
return false;
// the character center in plate are not to near in the x-axis
float x_1 = float(rect1.tl().x + rect1.width / 2);
float x_2 = float(rect2.tl().x + rect2.width / 2);
float x_diff = abs(x_1 - x_2);
double x_diff_ratio = x_diff / min(height_1, height_2);
if (x_diff_ratio < 0.25)
return false;
// the character in plate are near in the x-axis but not very near
float x_margin_left = float(min(rect1.br().x, rect2.br().x));
float x_margin_right = float(max(rect1.tl().x, rect2.tl().x));
float x_margin_diff = abs(x_margin_left - x_margin_right);
double x_margin_diff_ratio = x_margin_diff / min(height_1, height_2);
if (x_margin_diff_ratio > 1.0)
return false;
return true;
}