-
Notifications
You must be signed in to change notification settings - Fork 6
/
ScoreEngine.cpp
3241 lines (2193 loc) · 71.1 KB
/
ScoreEngine.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
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
/*
Copyright(c) 2015, Fang Li
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met :
*Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and / or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED.IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
anthor: Fangli
e-mail: [email protected]
*/
#include "ScoreEngine.h"
int bar_table[22] = { -1, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0,
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 5 };
typedef struct t_Rects
{
vector<Rect> rects;
double average = 0;
}Rects;
//构造函数
ScoreEngine::ScoreEngine(){
templ.create(template_r * 2, template_r * 2, CV_8UC1);
templ.setTo(Scalar::all(255));
Rect roi_rect(0, 0, template_r, template_r);
templ(roi_rect).setTo(Scalar::all(0));
roi_rect.x = template_r;
roi_rect.y = template_r;
templ(roi_rect).setTo(Scalar::all(0));
init_is_ready = false;
}
//析构函数
ScoreEngine::~ScoreEngine(){
}
//先按照个数排序
//个数相等的情形下,按照y的坐标排序
bool ScoreEngine::sort_y_rect(const vector<Rect> & rects1, const vector<Rect> & rects2){
double sum1 = 0;
double sum2 = 0;
for (int i = 0; i < rects1.size(); i++)
sum1 += rects1[i].y;
for (int i = 0; i < rects2.size(); i++)
sum2 += rects2[i].y;
if (rects1.size() == 0){
sum1 = 0;
}
else{
sum1 = sum1 / rects1.size();
}
if (rects2.size() == 0){
sum2 = 0;
}
else {
sum2 = sum2 / rects2.size();
}
if (rects1.size() > rects2.size()){
return true;
}
else if (rects1.size() == rects2.size()){
if (sum1 > sum2){
return true;
}
}
return false;
}
bool ScoreEngine::sort_x_rect(const Rect & rect1, const Rect & rect2){
return rect1.x < rect2.x;
}
void ScoreEngine::getAllFiles(string path, vector<string>& files){
_finddata_t fileDir;
string dir = path;
dir = dir + "*.jpg";
//char* dir = "./fangli/*.txt";
intptr_t lfDir;
if ((lfDir = _findfirst(dir.c_str(), &fileDir)) == -1L)
printf("No file is found\n");
else{
//printf("file list:\n");
do{
//printf("%s\n", fileDir.name);
char buffer[256];
sprintf_s(buffer, "%s", fileDir.name);
string ss(buffer);
files.push_back(ss);
} while (_findnext(lfDir, &fileDir) == 0);
}
_findclose(lfDir);
}
/*
function: erase the outline of the input image
src: input image, gray, the square is white, the other is black
dst: output image, gray
gray_threshold: binay threshold
debug_flag: display debug image
return: 0, no outline, just binary; -1, find outline and erase; -2, src is empty or channel error
version: 1.2,
log: 将二值化的功能改为了opencv自带的大津法
*/
int ScoreEngine::eraseOutline(const Mat & src, Mat & dst, const int gray_threshold, int debug_flag){
if (src.empty()){
return -2;
}
Mat src_binary;
if (src.channels() == 1){
src_binary = src.clone();
}
else if (src.channels() == 3){
cvtColor(src, src_binary, CV_BGR2GRAY);
}
else{
return -2;
}
dst = src_binary.clone();
threshold(src_binary, src_binary, gray_threshold, 255, CV_THRESH_BINARY);
// blur(src_binary, src_binary, Size(2, 2));
//
// threshold(src_binary, src_binary, 2, 255, CV_THRESH_OTSU | CV_THRESH_BINARY_INV);
//imshow("src_binary", src_binary);
vector<vector<Point>> contours;
vector<Vec4i> hierarchy;
findContours(src_binary, contours, hierarchy, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_NONE, Point(0, 0));
bool outline_flag = false;
for (int j = 0; j < contours.size(); j++){
Rect bound_rect = boundingRect(contours[j]);
if (bound_rect.area() > 1600 || bound_rect.area() < 150 || bound_rect.width > 50 || bound_rect.height > 50){
//cout << "come into" << endl;
drawContours(dst, contours, j, Scalar::all(0), 3); //擦除矩形边框
outline_flag = true;
}
}
if (outline_flag == true){
return -1;
}
else{
return 0;
}
}
/*
return : -1 error, 0 normal
输入必须是单通道
*/
int ScoreEngine::getStandardRect(const Mat & input_src, Rect & standard_rect){
if (!input_src.data){
return -1;
}
Mat src;
if (input_src.channels() == 1){
//cvtColor(input_src, src, CV_GRAY2BGR);
src = input_src.clone();
}
else if (input_src.channels() == 3){
//src = input_src.clone();
cvtColor(input_src, src, CV_BGR2GRAY);
}
else {
return -1;
}
int gray_threshold = myOtsu(src) * 1.4;
if (gray_threshold > 255)
gray_threshold = 200;
threshold(src, src, 200, 255, CV_THRESH_OTSU | CV_THRESH_BINARY_INV);
Mat src_temp = src.clone();
//将src_temp中的方框给搞掉
eraseOutline(src_temp, src_temp, gray_threshold, 0);
vector<vector<Point>> contours;
vector<Vec4i> hierarchy;
findContours(src_temp, contours, hierarchy, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_NONE, Point(0, 0));
//RNG rng(clock());
//Mat disp(src_temp.size(), CV_8UC3, Scalar::all(0));
Mat disp_wh(src_temp.size(), CV_8UC1, Scalar::all(0));
for (int j = 0; j < contours.size(); j++){
Rect bound_rect = boundingRect(contours[j]);
Point temp_point(bound_rect.width, bound_rect.height);
//width_height.push_back(temp_point);
if (temp_point.x < 10 || temp_point.y < 10)
continue;
disp_wh.at<uchar>(temp_point.y, temp_point.x) += 1;
}
double minVal;
double maxVal;
Point minLoc;
Point maxLoc;
Point matchLoc;
minMaxLoc(disp_wh, &minVal, &maxVal, &minLoc, &maxLoc, Mat());
circle(disp_wh, maxLoc, 2, Scalar::all(255), -1);
standard_rect = Rect(0, 0, maxLoc.x - 1, maxLoc.y - 1);
if (standard_rect.width < 1 || standard_rect.height < 1){
return -1;
}
return 0;
}
/*
func: 识别试卷考号
input_src: 待识别考号的试卷
search_rect: 包含考号区域的矩形
input_len: 学生考号的位数
result_id: 识别出的考号结果
debug_flag: 调试标志位
return: 0,正常, result_id 为正常值
-1,图片异常,包含图片错误,识别出的id数字个数和传入的input_len不相等,
-2,有一列考号没有涂抹,result_id = "U", 请外部人工check
-3,有一列考号,被涂抹了多次, result_id = "M", 请外部分工check
version: 1.0 2015年7月6日12:59:05
log: 这个版本的每个数字的比率没有归一化,用单纯的阈值虚警率有点高
version: 1.2 2015年7月26日08:58:43
log: 每一列只有两种情况 空或者选出最可能被涂抹的那个
*/
int ScoreEngine::scanSpottedID(const Mat & input_src, const Rect & search_rect, int input_len, char result_id[32], int * output_len, int debug_flag){
if (!input_src.data){
return -1;
}
if (search_rect.x < 0 || search_rect.y < 0 || search_rect.height <= 0 || search_rect.width <= 0)
return -1;
Rect standard_rect(0, 0, 0, 0);
if (getStandardRect(input_src(search_rect), standard_rect) < 0){
cout << "no find standard rect" << endl;
return -1;
}
const double standard_width = 35;
double zoom_ratio = standard_width * 1.0 / standard_rect.width;
standard_rect.width *= zoom_ratio;
standard_rect.height *= zoom_ratio;
cout << "zoom_ratio = " << zoom_ratio << endl;
Mat src = input_src(search_rect).clone();
imshow("srcccccccc", src);
resize(src, src, Size(src.cols * zoom_ratio, src.rows * zoom_ratio));
if (zoom_ratio < 1.2){
blur(src, src, Size(5, 5));
}
Mat src_copy, src_copy1;
if (src.channels() == 1){
cvtColor(src, src_copy, CV_GRAY2BGR);
}
else if (src.channels() == 3){
src_copy = src.clone();
cvtColor(src, src, CV_BGR2GRAY);
}
else {
return -1;
}
//src 是gray, src_copy是三通道的, src_copy1 是单通道的
src_copy1 = src.clone();
int gray_threshold = myOtsu(src) * 1.4;
if (gray_threshold > 255)
gray_threshold = 200;
threshold(src, src, 2, 255, CV_THRESH_OTSU | CV_THRESH_BINARY_INV);
if (debug_flag){
//imshow("1_src", src);
}
Mat src_temp = src.clone();
//这个地方有优化的地方,可以将这个函数拿到这里来实现
for (int j = 0; j < 1; j++){
if (!eraseOutline(src_temp, src_temp, gray_threshold, 0)){
break;
}
}
vector<vector<Point>> contours;
vector<Vec4i> hierarchy;
findContours(src_temp, contours, hierarchy, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_NONE, Point(0, 0));
RNG rng(clock());
Mat disp(src_temp.size(), CV_8UC3, Scalar::all(0));
Mat disp_wh(src_temp.size(), CV_8UC1, Scalar::all(0));
for (int j = 0; j < contours.size(); j++){
Rect bound_rect = boundingRect(contours[j]);
Point temp_point(bound_rect.width, bound_rect.height);
//width_height.push_back(temp_point);
if (temp_point.x < 10 || temp_point.y < 10)
continue;
disp_wh.at<uchar>(temp_point.y, temp_point.x) += 1;
}
double minVal;
double maxVal;
Point minLoc;
Point maxLoc;
Point matchLoc;
minMaxLoc(disp_wh, &minVal, &maxVal, &minLoc, &maxLoc, Mat());
circle(disp_wh, maxLoc, 2, Scalar::all(255), -1);
cout << "befor, standard_rect = " << standard_rect << endl;
standard_rect = Rect(0, 0, maxLoc.x -1 , maxLoc.y -1 );
cout << "after, standard_rect = " << standard_rect << endl;
// if (zoom_ratio > 1.2){
// //blur(src_copy1, src, Size(1, 1));
// src = src_copy1.clone();
// }
// else{
// blur(src_copy1, src, Size(5, 5));
// }
// if (standard_rect.width >= 30){
// blur(src_copy1, src, Size(5, 5));
// }
// else if (standard_rect.width >= 25){
// blur(src_copy1, src, Size(2, 2));
// }
// else{
// blur(src_copy1, src, Size(3, 1));
// }
//threshold(src, src, 200, 255, CV_THRESH_OTSU | CV_THRESH_BINARY_INV);
//cout << "standard_rect.width ============================================= " << standard_rect.width << endl;
if (debug_flag){
//imshow("2_src", src);
}
rectangle(disp_wh, standard_rect, Scalar::all(255), 1);
vector<Point> approx;
vector<Rect> rects;
for (int j = 0; j < contours.size(); j++){
// approx.clear();
// approxPolyDP(Mat(contours[i]), approx, arcLength(Mat(contours[i]), true)*0.1, true);
// if (!(approx.size() == 4 && isContourConvex(Mat(approx))))
// continue;
Rect bound_rect = boundingRect(contours[j]);
if (bound_rect.height * 1.0 / bound_rect.width >0.96)
continue;
Point temp_point(bound_rect.width, bound_rect.height);
if (temp_point.x < 10 || temp_point.y < 10)
continue;
if (compareRect(bound_rect, standard_rect) > 0.9){
Scalar color = Scalar(rng.uniform(0, 255), rng.uniform(0, 255), rng.uniform(0, 255));
//drawContours(disp, contours, j, color, 1);
//drawContours(disp, contours, j, color, 1);
rects.push_back(bound_rect);
}
}
vector<vector<Rect>> y_rects, x_rects;
//对列操作
for (int j = 0; j < rects.size(); j++){
bool is_new_rect = true;
for (int k = 0; k < x_rects.size(); k++){
double sum = 0;
for (int q = 0; q < x_rects[k].size(); q++){
sum += x_rects[k][q].x;
}
double average = sum / x_rects[k].size();
if (abs(rects[j].x - average) <= 5.0){
x_rects[k].push_back(rects[j]);
is_new_rect = false;
break;
}
}
if (is_new_rect){
vector<Rect> temp_rects;
temp_rects.push_back(rects[j]);
x_rects.push_back(temp_rects);
}
}
//擦除聚类太少的个数
for (int i = 0; i < x_rects.size(); i++){
if (x_rects[i].size() < 3){
x_rects.erase(x_rects.begin() + i);
i--;
}
}
rects.clear();
//重新转载rects
for (int j = 0; j < x_rects.size(); j++){
for (int k = 0; k < x_rects[j].size(); k++){
rects.push_back(x_rects[j][k]);
}
}
//for y,对每行处理
for (int j = 0; j < rects.size(); j++){
bool is_new_rect = true;
for (int k = 0; k < y_rects.size(); k++){
double sum = 0;
for (int q = 0; q < y_rects[k].size(); q++){
sum += y_rects[k][q].y;
}
double average = sum / y_rects[k].size();
if (abs(rects[j].y - average) <= 5.0){
y_rects[k].push_back(rects[j]);
is_new_rect = false;
break;
}
}
if (is_new_rect){
vector<Rect> temp_rects;
temp_rects.push_back(rects[j]);
y_rects.push_back(temp_rects);
}
}
//这里可以改为选择题那样的方式
//判断起点和终点,还有间隙
if (y_rects.size() < 10){
cout << "fail to find " << endl;
return -1;
}
sort(y_rects.begin(), y_rects.end(), sort_y_rect);
vector<double> ys, xs;
for (int i = 0; i < 10; i++){
double sum = 0;
for (int j = 0; j < y_rects[i].size(); j++){
sum += y_rects[i][j].y;
}
if (y_rects[i].size() == 0){
sum = 0;
}
else{
sum = sum / (y_rects[i].size());
}
ys.push_back(sum);
}
sort(ys.begin(), ys.end());
// if (!checkDelta(ys, 4)){
//
// for (int i = 0; i < ys.size(); i++){
// cout << ys[i] << endl;
// }
// cout << "y delta wrong!" << endl;
// return -1;
// }
for (int i = 0; i < x_rects.size(); i++){
double sum = 0;
for (int j = 0; j < x_rects[i].size(); j++){
sum += x_rects[i][j].x;
}
if (x_rects[i].size() == 0){
sum = 0;
}
else{
sum = sum / (x_rects[i].size());
}
xs.push_back(sum);
}
sort(xs.begin(), xs.end());
// if (!checkDelta(xs, 4)){
// cout << "x delta wrong!" << endl;
// return -1;
// }
for (int j = 0; j < xs.size(); j++){
line(src_copy, Point(xs[j], 0), Point(xs[j], 1000), CV_RGB(255, 0, 0));
}
for (int j = 0; j < ys.size(); j++){
//cout << ys[j] << endl;
line(src_copy, Point(0, ys[j]), Point(1000, ys[j]), CV_RGB(255, 0, 0));
}
for (int j = 0; j < y_rects.size(); j++){
//rects_num += y_rects[j].size();
Scalar color(rng.uniform(0, 255), rng.uniform(0, 255), rng.uniform(0, 255));
for (int k = 0; k < y_rects[j].size(); k++){
rectangle(disp, y_rects[j][k], color, 1);
}
}
string result_id1;
bool legal_id = true;
int return_value = 0;
const int MAXCOL = 10;
//申请一维数据并将其转成二维数组指针
double *pp_arr = new double[xs.size() * MAXCOL];
double(*binary_ratio)[MAXCOL] = (double(*)[MAXCOL])pp_arr;
vector<vector<double>> ratios(xs.size()); //里面的vector的size为10,外面的vector size 为xs.size
for (int j = 0; j < xs.size(); j++){
vector<int> x_col;
//cout << j + 1 << " th col" << endl;
double first_value = 0;
double second_value = 0;
int first_index = 0;
int second_index = 0;
// Mat temp_src = src_copy1.clone();
// int border = 5;
// Rect rect_bin(xs[j] - border, 0, standard_rect.width + border * 2, temp_src.rows);
//
//
// if (rect_bin.x < 0)
// rect_bin.x = 0;
// if (rect_bin.y < 0)
// rect_bin.y = 0;
//
// if (rect_bin.br().y >= src.rows)
// rect_bin.y = src.rows - rect_bin.height;
// if (rect_bin.br().x >= src.cols){
// rect_bin.x = src.cols - rect_bin.width;
// }
//
//
// Mat src_roi_bin = temp_src(rect_bin);
// blur(src_roi_bin, src_roi_bin, Size(3, 3));
// adaptiveThreshold(src_roi_bin, src_roi_bin, 255, CV_ADAPTIVE_THRESH_MEAN_C, CV_THRESH_BINARY_INV, 3, 5);
// blur(src_roi_bin, src_roi_bin, Size(3, 3));
// threshold(src_roi_bin, src_roi_bin, 2, 255, CV_THRESH_OTSU | CV_THRESH_BINARY_INV);
// imshow("edsfa", temp_src);
for (int k = 0; k < ys.size(); k++){
Rect roi_rect(xs[j], ys[k], standard_rect.width, standard_rect.height);
if (roi_rect.x < 0)
roi_rect.x = 0;
if (roi_rect.y < 0)
roi_rect.y = 0;
if (roi_rect.br().y >= src.rows)
roi_rect.y = src.rows - roi_rect.height;
if (roi_rect.br().x >= src.cols){
roi_rect.x = src.cols - roi_rect.width;
}
Mat roi(src, roi_rect);
// Mat roi(temp_src, roi_rect);
double no_zero = countNonZero(roi);
double ratio = no_zero / standard_rect.area();
ratios[j].push_back(ratio);
//cout << ratio << endl;
if (ratio > first_value){
second_value = first_value;
second_index = first_index;
first_value = ratio;
first_index = k;
}
else if (ratio > second_value){
second_value = ratio;
second_index = k;
}
}
// waitKey(0);
//cout << endl;
//double threshold = 0.66;
// double threshold = 0.6;
//
// cout << "first_value = " << first_value << ", second value = " << second_value << endl;
//
// if (first_value < threshold){
// //cout << j << " th col has no num" << endl;
// //result_id1 = "U";
// result_id[0] = 'U';
// result_id[1] = '\0';
// *output_len = 1;
// putText(src_copy, "U", Point(xs[j], ys[0]), CV_FONT_HERSHEY_SIMPLEX, 1.0, CV_RGB(255, 0, 255), 2);
// return_value = -2;
// goto return_positon;
// //return -2;
// }
// //Muti
// else if (first_value >= threshold && second_value > 0.70){
// cout << "one col has two num" << endl;
//
// result_id[0] = 'M';
// result_id[1] = '\0';
// *output_len = 1;
// putText(src_copy, "M", Point(xs[j], ys[0]), CV_FONT_HERSHEY_SIMPLEX, 1.0, CV_RGB(255, 0, 255), 2);
// return_value = -3;
// goto return_positon;
// }
// else{
// string temp;
// temp.push_back('0' + first_index);
// //result_id1.push_back('0' + first_index);
// result_id[j] = '0' + first_index;
// putText(src_copy, temp, Point(xs[j], ys[0]), CV_FONT_HERSHEY_SIMPLEX, 1.0, CV_RGB(255, 0, 255), 2);
// }
}
//sprintf_s(result_id, "%s", result_id1.c_str());
*output_len = xs.size();
result_id[xs.size()] = '\0';
//cout << result_id << endl;
double not_choose_threshold = 0.66;
vector<double> temp_ratio;
bool is_undefined = false;
for (int i = 0; i < ratios.size(); i++){
temp_ratio = ratios[i];
sort(temp_ratio.begin(), temp_ratio.end());
// for (int j = temp_ratio.size() - 1; j >= 0; j--){
// cout << temp_ratio[j] << " ";
// }
// cout << endl;
int max_index = 0;
double max_value = temp_ratio[temp_ratio.size() - 1];
for (int j = 0; j < ratios[i].size(); j++){
if (ratios[i][j] == max_value){
max_index = j;
break;
}
}
if (max_value < not_choose_threshold || max_value - temp_ratio[6] < 0.1){
putText(src_copy, "U", Point(xs[i], ys[0]), CV_FONT_HERSHEY_SIMPLEX, 1.0, CV_RGB(0, 0, 255), 2);
is_undefined = true;
}
else{
string temp;
temp.push_back('0' + max_index);
result_id[i] = '0' + max_index;
putText(src_copy, temp, Point(xs[i], ys[0]), CV_FONT_HERSHEY_SIMPLEX, 1.0, CV_RGB(255, 0, 255), 2);
}
}
return_value = 0;
result_id[xs.size()] = '\0';
if (is_undefined){
result_id[0] = 'U';
result_id[1] = '\0';
return_value = -1;
*output_len = 1;
}
return_positon:
if (debug_flag){
resize(src_copy, src_copy, Size(src_copy.cols / zoom_ratio, src_copy.rows / zoom_ratio));
imshow("src_copy", src_copy);
//resize(src, src, src.size() / 2);
//imshow("disp", disp);
//imshow("wh", disp_wh);
}
delete[] pp_arr;
return return_value;
}
double ScoreEngine::compareRect(const Rect & r1, const Rect & r2){
if (r2.width <= 0 || r2.height <= 0)
return -1;
double ratio = 0;
ratio = 0.5 * (1 - abs(r1.height - r2.height) * 1.0 / r2.height) + 0.5 * (1 - abs(r1.width - r2.width) * 1.0 / r2.width);
return ratio;
}
double ScoreEngine::compareRect1(const Rect & r1, const Rect & r2){
if (r2.width <= 0 || r2.height <= 0)
return -1;
double ratio = 0;
ratio = 0.5 * (1 - (r1.height - r2.height) * 1.0 / r2.height) + 0.5 * (1 - (r1.width - r2.width) * 1.0 / r2.width);
return ratio;
}
bool ScoreEngine::checkDelta(const vector<double> & value, double precision){
if (value.size() < 2)
return false;
vector<double> deltas;
for (int i = 0; i < value.size() - 1; i++){
deltas.push_back(value[i + 1] - value[i]);
}
for (int i = 0; i < deltas.size(); i++){
for (int j = i + 1; j < deltas.size(); j++){
if (abs(deltas[j] - deltas[i]) > precision){
return false;
}
}
}
return true;
}
//return 0, normal, -1,abnormal
int ScoreEngine::getUpDownRect(const Mat & input_src, int gray_threshold, Rect & first_rect, Rect & second_rect){
if (!input_src.data){
return -1;
}
//必须为单通道的数据
//CV_Assert(input_src.channels() == 1);
if (input_src.channels() != 1){
cout << "channels != 1" << endl;
return -1;
}
Mat src = input_src.clone();
vector<vector<Point>> contours;
vector<Vec4i> hierarchy;
//int gray_threshold = 100;
threshold(src, src, gray_threshold, 255, CV_THRESH_BINARY_INV);
Mat src_temp = src.clone();
Mat hough_src = src.clone();
//去除小的轮廓误差
findContours(src_temp, contours, hierarchy, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_NONE, Point(0, 0));