-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprogram.txt
646 lines (644 loc) · 37 KB
/
program.txt
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
#include "opencv2/imgproc.hpp"
#include "opencv2/highgui.hpp"
#include <iostream>
#include<conio.h>
using namespace std;
using namespace cv;
Mat ResizeImage(Mat img, int height = 800)
{
//If you want 75 % along each axis, you should be able to use cv::resize to do:
//cv::resize(inImg, outImg, cv::Size(), 0.75, 0.75);
//cvtColor(dst, dst1, COLOR_BGR2GRAY);
float rat = height / (1.0*img.size().height);
int width = (int)(rat*img.size().width);
Mat dst = Mat::ones(Size((int)width, height), img.type());
resize(img, dst, dst.size(), 0, 0, INTER_AREA);
return dst;
}
Mat SharpenImage(Mat img)
{
Mat dst;
/*Mat kernel = (Mat_<char>(3, 3) << -1, -1, -1, -1, 9, -1, -1, -1, -1);
//other Mat 0 -1 0 -1 5 -1 0 -1 0;
filter2D(img, dst, img.depth(), kernel);*/
GaussianBlur(img, dst, Size(0, 0), 3);
addWeighted(img, 1.5, dst, -0.5, 0, dst);
return dst;
}
int FindPositionMin(float _array[], int length)
{
int vt = 0;
float min = _array[0];
for (int i = 1; i < length; i++)
{
if (_array[i] < min)
{
min = _array[i];
vt = i;
}
}
return vt;
}
int FindPositionMax(float _array[], int length)
{
int vt = 0;
float max = _array[0];
for (int i = 1; i < length; i++)
{
if (_array[i] > max)
{
max = _array[i];
vt = i;
}
}
return vt;
}
vector<Point2f> SortCornerPoints(vector<Point2f> points)
{
vector<Point2f> _pts(4);
float *_sum = new float[points.size()];
float *_diff= new float[points.size()];
for (size_t i = 0; i < points.size(); i++)
{
_sum[i] = points[i].x + points[i].y;
}
for (size_t i = 0; i < points.size(); i++)
{
_diff[i] = points[i].y - points[i].x;
}
/*the top-left point will have the smallest sum, whereas
the bottom - right point will have the largest sum
now, compute the difference between the points, the
top - right point will have the smallest difference,
whereas the bottom - left will have the largest difference*/
int vt0 = FindPositionMin(_sum, points.size());
int vt1 = FindPositionMin(_diff, points.size());
int vt2 = FindPositionMax(_sum, points.size());
int vt3 = FindPositionMax(_diff, points.size());
_pts[0] = points[vt0];
_pts[1] = points[vt1];
_pts[2] = points[vt2];
_pts[3] = points[vt3];
return _pts;
}
bool Ycompare(const Point2f p1, const Point2f p2)
{
return(p1.y + 5 < p2.y);
}
bool Xcompare(const Point2f p1, const Point2f p2)
{
return(p1.x + 5 < p2.x);
}
vector<Point2f> SortPoints(vector<Point2f> points, int axis = 0)
{
if (axis == 0)
{
std::stable_sort(points.begin(), points.end(), Ycompare);
std::stable_sort(points.begin(), points.end(), Xcompare);
}
else if(axis==1)
{
std::stable_sort(points.begin(), points.end(), Xcompare);
std::stable_sort(points.begin(), points.end(), Ycompare);
}
return points;
}
vector<Point2f> TranformPoints(vector<Point2f> points, Point2f offset, float rat=1)
{
for (size_t i = 0; i < points.size(); i++)
{
points[i].x = (points[i].x *rat + offset.x);
points[i].y = (points[i].y *rat + offset.y);
}
return points;
}
Mat TranformImage(Mat img, vector<Point2f> points)
{
float newHeight = max(norm(points[0] - points[3]), norm(points[1] - points[2]));
float newWidth = max(norm(points[0] - points[1]), norm(points[2] - points[3]));
vector<Point2f> _dst;
_dst.push_back(Point2f(0, 0));
_dst.push_back(Point2f(newWidth-1, 0));
_dst.push_back(Point2f(newWidth-1, newHeight-1));
_dst.push_back(Point2f(0, newHeight-1));
Mat _M = getPerspectiveTransform(points, _dst);
Mat _Warp;
warpPerspective(img, _Warp, _M, Size((int)newWidth, (int)newHeight));
return _Warp;
}
Mat DocummentScan(Mat src, Point2f offset)
{
Mat orig = src.clone();
Mat dst, dst1;
vector< Point2f> roi_corners;
vector< Point2f> dst_corners(4);
int h = 800;
float rat = src.size().height / (h*1.0);
src = ResizeImage(src, h);
cvtColor(src, dst, COLOR_BGR2RGB);
cvtColor(dst, dst, COLOR_BGR2GRAY);
bilateralFilter(dst, dst1, 9, 75, 75);
adaptiveThreshold(dst1, dst1, 255, ADAPTIVE_THRESH_GAUSSIAN_C, THRESH_BINARY, 115, 2);
medianBlur(dst1, dst1, 11);
copyMakeBorder(dst1, dst1, 5, 5, 5, 5, BORDER_CONSTANT, Scalar(0, 0, 0));
int height = src.size().height;
int width = src.size().width;
Mat edges;
Canny(dst1, edges, 200, 250);
vector<vector<Point> > contours;
vector<Vec4i> hierarchy;
findContours(edges, contours, hierarchy, RETR_TREE, CHAIN_APPROX_SIMPLE);
int MAX_COUNTOUR_AREA = (width - 30) * (height - 30);
double maxAreaFound = MAX_COUNTOUR_AREA * 0.1;
roi_corners.push_back(Point2f(5, 5));
roi_corners.push_back(Point2f((width - 5), 5));
roi_corners.push_back(Point2f((width - 5), (height - 5)));
roi_corners.push_back(Point2f(5, (height - 5)));
for (size_t i = 0; i < contours.size(); i++)
{
double perimeter = arcLength(contours[i], true);
vector<Point2f> approx;
approxPolyDP(contours[i], approx, 0.03*perimeter, true);
bool isConvex = isContourConvex(approx);
double area = contourArea(approx);
bool ok = (approx.size() == 4 && isContourConvex(approx) && maxAreaFound < contourArea(approx) && contourArea(approx) < MAX_COUNTOUR_AREA);
if (ok)
{
maxAreaFound = contourArea(approx);
roi_corners = approx;
}
}
roi_corners = SortCornerPoints(roi_corners);
dst_corners = TranformPoints(roi_corners, Point2f(), rat);
Mat i = TranformImage(orig, dst_corners);
vector<Point2f> newDst_corners;
newDst_corners.push_back(Point2f(offset.y, offset.x));
newDst_corners.push_back(Point2f(i.size().width - offset.y, offset.x));
newDst_corners.push_back(Point2f(i.size().width - offset.y, i.size().height - offset.x));
newDst_corners.push_back(Point2f(offset.y, i.size().height - offset.x));
Mat newImage = TranformImage(i, newDst_corners);
return newImage;
}
vector<vector<Point2f>> FindAnchors(Mat img,double Area=INT_MAX,double deltaArea= INT_MAX)
{
vector<vector<Point2f>> anchors;
Mat src_HSV;
cvtColor(img, src_HSV, COLOR_BGR2HSV);
blur(src_HSV, src_HSV, Size(5,5));
Mat edges;
inRange(src_HSV, Scalar(0, 0, 0), Scalar(180, 255, 50), edges);
vector<vector<Point>> contours;
vector<Vec4i> hierarchy;
/// Find contours
findContours(edges, contours, hierarchy, RETR_TREE, CHAIN_APPROX_SIMPLE, Point(0, 0));
for (int i = 0; i < contours.size(); i++)
{
double perimeter = arcLength(contours[i], true);
vector<Point2f> approx;
approxPolyDP(contours[i], approx, 0.03*perimeter, true);
double area = contourArea(contours[i]);
bool ok;
if (Area == INT_MAX || deltaArea == INT_MAX)
{
ok = (approx.size() == 4 && isContourConvex(approx));
}
else
{
ok =( approx.size() == 4 && (area > Area - deltaArea) && (area < Area + deltaArea) && isContourConvex(approx));
}
if (ok)
{
anchors.push_back(approx);
}
}
return anchors;
}
vector<vector<Point2f>> FindRectangles(Mat img, double fromThreshold, double toThreshold,double ratioArea=0)
{
vector<vector<Point2f>> rectangles;
Mat src_gray;
GaussianBlur(img, img, Size(3,3),0);
cvtColor(img, src_gray, COLOR_BGR2RGB);
cvtColor(src_gray, src_gray, COLOR_BGR2GRAY);
Mat dst1;
bilateralFilter(src_gray, dst1, 9, 75, 75);
adaptiveThreshold(dst1, dst1, 255, ADAPTIVE_THRESH_GAUSSIAN_C, THRESH_BINARY, 115, 10);
medianBlur(dst1, dst1, 11);
Mat edges;
vector<vector<Point>> contours;
vector<Vec4i> hierarchy;
Canny(src_gray, edges, fromThreshold, toThreshold, 3);
double Max_Area_Find = (img.size().width - 5)*(img.size().height - 5);
/// Find contours
findContours(edges, contours, hierarchy, RETR_TREE, CHAIN_APPROX_SIMPLE, Point(0, 0));
for (int i = 0; i < contours.size(); i++)
{
double perimeter = arcLength(contours[i], true);
vector<Point2f> approx;
approxPolyDP(contours[i], approx, 0.03*perimeter, true);
double area = contourArea(contours[i]);
bool ok;
if (ratioArea == 0)
{
ok = approx.size() == 4 && isContourConvex(approx);
}
else
{
ok = approx.size() == 4 && isContourConvex(approx) && area >= ratioArea * Max_Area_Find;
}
if (ok)
{
rectangles.push_back(approx);
}
}
return rectangles;
}
vector<vector<Point2f>> TranformContours(vector<vector<Point2f>> contours)
{
// sort 4 conner contour
vector<vector<Point2f>> temp = contours;
for (size_t i = 0; i < contours.size(); i++)
{
temp[i]= SortCornerPoints(contours[i]);
}
return temp;
}
Point2f PointIntersecsion(vector<Point2f> contour)
{
Moments M;
M = moments(contour);
int x = int(M.m10 / M.m00);
int y = int(M.m01 / M.m00);
return Point(x, y);
}
vector<Point2f> ListPointIntersection(vector<vector<Point2f>> contours)
{
vector<Point2f> dst;
for (size_t i = 0; i < contours.size(); i++)
{
Point2f p = PointIntersecsion(contours[i]);
dst.push_back(p);
}
return dst;
}
Point2f FindPointInRegion(vector<Point2f> points, Point2f condition1, Point2f condition2, int axis)
{
// axis =0,1; 1 theo chiều x,0 theo chiều y
Point2f point;
for (size_t i = 0; i < points.size(); i++)
{
if (axis == 1)
{
// tìm point trong khoang delta y
float average = (condition1.y + condition2.y) / 2.0;
float delta = std::fabs(condition1.y - condition2.y)+5;
if (points[i].y == average)
{
point = points[i];
break;
}
else
{
bool ok = points[i].y > (average - delta) && points[i].y < (average + delta);
if (ok)
{
point = points[i];
break;
}
}
}
else
{
//tìm point trong khoảng delta x
float average = (condition1.x + condition2.x) / 2.0;
float delta = std::fabs(condition1.x - condition2.x)+5;
if (points[i].x == average)
{
point = points[i];
break;
}
else
{
bool ok = points[i].x > (average - delta) && points[i].x < (average + delta);
if (ok)
{
point = points[i];
break;
}
}
}
}
return point;
}
double Distance(Point2f p1, Point2f p2)
{
double d = (p1.x - p2.x)*(p1.x - p2.x) + (p1.y - p2.y)*(p1.y - p2.y);
return std::sqrtf(d);
}
vector<Point2f> ClusterPoints(vector<Point2f> points,double distance=3)
{
vector<Point2f> dst;
while (points.size() != 0)
{
int _count = 1;
Point2f p = points[0];
int sumX = p.x;
int sumY = p.y;
points.erase(points.begin());
for (size_t i = 0; i < points.size(); i++)
{
if (p == points[i])
{
points.erase(points.begin() + i);
i--;
}
else if (Distance(p, points[i]) < distance)
{
sumX += points[i].x;
sumY += points[i].y;
_count++;
points.erase(points.begin() + i);
i--;
}
}
dst.push_back(Point2f(int(sumX / _count * 1.0), int(sumY / _count * 1.0)));
}
return dst;
}
vector<Point2f> SortAnchors(vector<Point2f> src)
{
vector<Point2f> dst;
vector<Point2f> corners = SortCornerPoints(src);
for (size_t i = 0; i < corners.size(); i++)
{
src.erase(std::remove(src.begin(), src.end(), corners[i]));
}
//corner 1
dst.push_back(corners[0]);
Point2f point = FindPointInRegion(src, corners[0], corners[1], 1);
dst.push_back(point);
src.erase(std::remove(src.begin(), src.end(), point));
//corner 3
dst.push_back(corners[1]);
point = FindPointInRegion(src, corners[1], corners[2], 0);
dst.push_back(point);
src.erase(std::remove(src.begin(), src.end(), point));
//corner 5
dst.push_back(corners[2]);
dst.push_back(corners[3]);
point = FindPointInRegion(src, corners[0], corners[3], 0);
dst.push_back(point);
src.erase(std::remove(src.begin(), src.end(), point));
//corner 7
dst.push_back(src[0]);
return dst;
}
vector<vector<Point2f>> SortRectangles(vector<vector<Point2f>> rects, int axis = 0)
{
vector<vector<Point2f>> rectangles;
vector<Point2f> centerPoints;
for (size_t i = 0; i < rects.size(); i++)
{
Point2f p = PointIntersecsion(rects[i]);
centerPoints.push_back(p);
}
vector<Point2f> _copy = centerPoints;
_copy = ClusterPoints(_copy);
_copy = SortPoints(_copy, axis);
for (size_t i = 0; i < _copy.size(); i++)
{
for (size_t j = 0; i < centerPoints.size();j++)
{
if (_copy[i] == centerPoints[j])
{
rectangles.push_back(rects[j]);
centerPoints.erase(centerPoints.begin() + j);
break;
}
else
{
if (Distance(_copy[i], centerPoints[j]) < 3)
{
rectangles.push_back(rects[j]);
centerPoints.erase(centerPoints.begin() + j);
break;
}
}
}
}
return rectangles;
}
Mat SubRectangleImage(Mat src, vector<Point2f> roi)
{
Rect rect = boundingRect(roi);
Mat subimage = src(rect).clone();
return subimage;
}
Mat SubCircleImage(Mat src, Point2f center, double radius)
{
Mat subImage = src(Rect(center.x - radius, // ROI x-offset, left coordinate
center.y - radius, // ROI y-offset, top coordinate
2 * radius, // ROI width
2 * radius)).clone();
return subImage;
}
vector<Mat> SeriImage(Mat src, vector<vector<Point2f>> rects)
{
vector<Mat> serious;
for (size_t i = 0; i < rects.size(); i++)
{
vector<Point2f> cornners = SortCornerPoints(rects[i]);
Mat M = TranformImage(src, cornners);
serious.push_back(M);
}
return serious;
}
vector<vector<Point2f>> FindCircle(Mat img, double adaptiveThresHold, double Area=INT_MAX, double deltaArea= INT_MAX)
{
vector<vector<Point2f>> circles;
cv::Size size(3, 3);
cv::GaussianBlur(img, img, size, 0);
Mat dst;
bilateralFilter(img, dst, 9, 75, 75);
img = dst;
cvtColor(img, img, COLOR_BGR2GRAY);
adaptiveThreshold(img, img, 255, ADAPTIVE_THRESH_MEAN_C, THRESH_BINARY, 75, 10);
cv::bitwise_not(img, img);
vector<vector<Point>> contours;
vector<Vec4i> hierarchy;
/// Find contours
findContours(img, contours, hierarchy, RETR_TREE, CHAIN_APPROX_SIMPLE, Point(0, 0));
for (int i = 0; i < contours.size(); i++)
{
double perimeter = arcLength(contours[i], true);
vector<Point2f> approx;
approxPolyDP(contours[i], approx, 0.03*perimeter, true);
double area = contourArea(contours[i]);
bool ok;
if (area == INT_MAX || deltaArea == INT_MAX)
{
ok = approx.size() >= 6 && isContourConvex(approx);
}
else
{
ok = approx.size() >= 6 && (area > Area - deltaArea) && (area < Area + deltaArea)&& isContourConvex(approx);
}
if (ok)
{
circles.push_back(approx);
}
}
return circles;
}
double FindRatioWhiteRegion(Mat src, int threadhold = 185)
{
Mat src_gray;
cvtColor(src, src_gray, COLOR_BGR2GRAY);
blur(src_gray, src_gray, Size(3, 3));
threshold(src_gray, src_gray, threadhold, 255, THRESH_BINARY);
countNonZero(src_gray);
int white = countNonZero(src_gray);
double ratio = white / (src_gray.size().width*src_gray.size().height*1.0);
return ratio;
}
vector<int> FindChoice(Mat src, vector<Point2f> points,int radious=10)
{
vector<int> dst;
for (size_t i = 0; i < points.size(); i++)
{
Mat img = SubCircleImage(src, points[i], radious);
double _ratio = FindRatioWhiteRegion(img);
if (_ratio <= 0.35)
{
dst.push_back(1);
}
else
{
dst.push_back(0);
}
}
return dst;
}
vector<int> AnswerChoice(Mat src, double adaptiveThresHold=75, int axis = 0)
{
vector<vector<Point2f>> circles = FindCircle(src, adaptiveThresHold);
vector<Point2f> CenterPoints = ListPointIntersection(circles);
CenterPoints = ClusterPoints(CenterPoints,3);
CenterPoints = SortPoints(CenterPoints, axis);
vector<int> choices = FindChoice(src, CenterPoints);
return choices;
}
vector<string> CharacterResults(vector<int> answerChoice, int optionChoices = 4)
{
vector<char> characters;
int ch = 65;
for (int i = 0; i < optionChoices; i++)
{
char x(ch);
characters.push_back(x);
ch++;
}
vector<String> results;
int _count = 0;
string str = "";
for (int i = 0; i < answerChoice.size(); i++)
{
_count++;
if (answerChoice[i] == 1)
{
str.append(1, characters[_count - 1]);
str += " ";
}
if (_count >= optionChoices)
{
results.push_back(str);
str = "";
_count = 0;
}
}
return results;
}
String NumberResults(vector<int> answerChoice,int optionChoices=10)
{
int _count = 0;
string str = "";
for (int i = 0; i < answerChoice.size(); i++)
{
_count++;
if (answerChoice[i] == 1)
{
str += to_string(_count - 1);
}
if (_count >= optionChoices)
{
_count = 0;
}
}
return str;
}
vector<string> IdentityHeader(vector<Mat> mats,int optionChoice=10)
{
vector<string> indentity;
for (size_t i = 0; i < mats.size(); i++)
{
vector<int> ac = AnswerChoice(ResizeImage( mats[i]),optionChoice);
string str = NumberResults(ac);
indentity.push_back(str);
}
return indentity;
}
vector<vector<string>> AllAnswers(vector<Mat> mats,int optionChoice=4)
{
vector<vector<string>> allAnswers;
for (size_t i = 0; i < mats.size(); i++)
{
vector<int> ac = AnswerChoice(ResizeImage(mats[i]), 75, 1);
vector<string> str = CharacterResults(ac, optionChoice);
allAnswers.push_back(str);
}
return allAnswers;
}
void main(int argc, char** argv)
{
Mat src= imread("C:\\Users\\Admin\\Desktop\\1.jpeg");
namedWindow("src", WINDOW_GUI_NORMAL);
Mat _copy = ResizeImage(src);
imshow("src", _copy);
vector<vector<Point2f>> contours = FindAnchors(_copy);
vector<Point2f> lstPointIntersection = ListPointIntersection(contours);
lstPointIntersection = ClusterPoints(lstPointIntersection,3);
vector<Point2f> anchors;
anchors = SortAnchors(lstPointIntersection);
anchors = TranformPoints(anchors, Point2f(), src.size().height / 800.0);
vector<Point2f> indentityRegion = { anchors[1],anchors[3] };
vector<Point2f> answerRegion = { anchors[3],anchors[5] };
Mat subIdImage = SubRectangleImage(src, indentityRegion);
subIdImage = ResizeImage(subIdImage);
Mat subAnswerImage = SubRectangleImage(src, answerRegion);
subAnswerImage = ResizeImage(subAnswerImage);
namedWindow("g", WINDOW_GUI_NORMAL);
namedWindow("gg", WINDOW_GUI_NORMAL);
vector<vector<Point2f>> IdRects = FindRectangles(subIdImage, 200, 250, 0.05);
IdRects = SortRectangles(IdRects, 1);
vector<vector<Point2f>> answerRects = FindRectangles(subAnswerImage, 200, 250,0.05);
answerRects = SortRectangles(answerRects, 1);
imshow("g", subIdImage);
imshow("gg", subAnswerImage);
vector<Mat> IdMats = SeriImage(subIdImage, IdRects);
vector<Mat> AnswerMats = SeriImage(subAnswerImage, answerRects);
vector<string> id = IdentityHeader(IdMats);
vector<vector<string>> allAnswers = AllAnswers(AnswerMats);
cout << "SBD :" << id[0] << endl;
cout << "Ma De :" << id[1] << endl;
int k = 1;
for (int i = 0; i < allAnswers.size(); i++)
{
for (int j = 0; j < allAnswers[i].size(); j++)
{
cout << k << "." << allAnswers[i][j] << " ";
k++;
}
}
waitKey();
_getch();
}