Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Tetragramm
GitHub Repository: Tetragramm/opencv
Path: blob/master/modules/objdetect/src/qrcode.cpp
16337 views
1
// This file is part of OpenCV project.
2
// It is subject to the license terms in the LICENSE file found in the top-level directory
3
// of this distribution and at http://opencv.org/license.html.
4
//
5
// Copyright (C) 2018, Intel Corporation, all rights reserved.
6
// Third party copyrights are property of their respective owners.
7
8
#include "precomp.hpp"
9
#include "opencv2/objdetect.hpp"
10
#include "opencv2/calib3d.hpp"
11
12
#ifdef HAVE_QUIRC
13
#include "quirc.h"
14
#endif
15
16
#include <limits>
17
#include <cmath>
18
#include <iostream>
19
#include <queue>
20
21
namespace cv
22
{
23
using std::vector;
24
25
class QRDetect
26
{
27
public:
28
void init(const Mat& src, double eps_vertical_ = 0.2, double eps_horizontal_ = 0.1);
29
bool localization();
30
bool computeTransformationPoints();
31
Mat getBinBarcode() { return bin_barcode; }
32
Mat getStraightBarcode() { return straight_barcode; }
33
vector<Point2f> getTransformationPoints() { return transformation_points; }
34
static Point2f intersectionLines(Point2f a1, Point2f a2, Point2f b1, Point2f b2);
35
protected:
36
vector<Vec3d> searchHorizontalLines();
37
vector<Point2f> separateVerticalLines(const vector<Vec3d> &list_lines);
38
void fixationPoints(vector<Point2f> &local_point);
39
vector<Point2f> getQuadrilateral(vector<Point2f> angle_list);
40
bool testBypassRoute(vector<Point2f> hull, int start, int finish);
41
inline double getCosVectors(Point2f a, Point2f b, Point2f c);
42
43
Mat barcode, bin_barcode, straight_barcode;
44
vector<Point2f> localization_points, transformation_points;
45
double eps_vertical, eps_horizontal, coeff_expansion;
46
};
47
48
49
void QRDetect::init(const Mat& src, double eps_vertical_, double eps_horizontal_)
50
{
51
CV_Assert(!src.empty());
52
const double min_side = std::min(src.size().width, src.size().height);
53
if (min_side < 512.0)
54
{
55
coeff_expansion = 512.0 / min_side;
56
const int width = cvRound(src.size().width * coeff_expansion);
57
const int height = cvRound(src.size().height * coeff_expansion);
58
Size new_size(width, height);
59
resize(src, barcode, new_size, 0, 0, INTER_LINEAR);
60
}
61
else
62
{
63
coeff_expansion = 1.0;
64
barcode = src;
65
}
66
67
eps_vertical = eps_vertical_;
68
eps_horizontal = eps_horizontal_;
69
adaptiveThreshold(barcode, bin_barcode, 255, ADAPTIVE_THRESH_GAUSSIAN_C, THRESH_BINARY, 83, 2);
70
71
}
72
73
vector<Vec3d> QRDetect::searchHorizontalLines()
74
{
75
vector<Vec3d> result;
76
const int height_bin_barcode = bin_barcode.rows;
77
const int width_bin_barcode = bin_barcode.cols;
78
const size_t test_lines_size = 5;
79
double test_lines[test_lines_size];
80
const size_t count_pixels_position = 1024;
81
size_t pixels_position[count_pixels_position];
82
size_t index = 0;
83
84
for (int y = 0; y < height_bin_barcode; y++)
85
{
86
const uint8_t *bin_barcode_row = bin_barcode.ptr<uint8_t>(y);
87
88
int pos = 0;
89
for (; pos < width_bin_barcode; pos++) { if (bin_barcode_row[pos] == 0) break; }
90
if (pos == width_bin_barcode) { continue; }
91
92
index = 0;
93
pixels_position[index] = pixels_position[index + 1] = pixels_position[index + 2] = pos;
94
index +=3;
95
96
uint8_t future_pixel = 255;
97
for (int x = pos; x < width_bin_barcode; x++)
98
{
99
if (bin_barcode_row[x] == future_pixel)
100
{
101
future_pixel = 255 - future_pixel;
102
pixels_position[index] = x;
103
index++;
104
}
105
}
106
pixels_position[index] = width_bin_barcode - 1;
107
index++;
108
for (size_t i = 2; i < index - 4; i+=2)
109
{
110
test_lines[0] = static_cast<double>(pixels_position[i - 1] - pixels_position[i - 2]);
111
test_lines[1] = static_cast<double>(pixels_position[i ] - pixels_position[i - 1]);
112
test_lines[2] = static_cast<double>(pixels_position[i + 1] - pixels_position[i ]);
113
test_lines[3] = static_cast<double>(pixels_position[i + 2] - pixels_position[i + 1]);
114
test_lines[4] = static_cast<double>(pixels_position[i + 3] - pixels_position[i + 2]);
115
116
double length = 0.0, weight = 0.0;
117
118
for (size_t j = 0; j < test_lines_size; j++) { length += test_lines[j]; }
119
120
if (length == 0) { continue; }
121
for (size_t j = 0; j < test_lines_size; j++)
122
{
123
if (j == 2) { weight += fabs((test_lines[j] / length) - 3.0/7.0); }
124
else { weight += fabs((test_lines[j] / length) - 1.0/7.0); }
125
}
126
127
if (weight < eps_vertical)
128
{
129
Vec3d line;
130
line[0] = static_cast<double>(pixels_position[i - 2]);
131
line[1] = y;
132
line[2] = length;
133
result.push_back(line);
134
}
135
}
136
}
137
return result;
138
}
139
140
vector<Point2f> QRDetect::separateVerticalLines(const vector<Vec3d> &list_lines)
141
{
142
vector<Vec3d> result;
143
int temp_length = 0;
144
uint8_t next_pixel;
145
vector<double> test_lines;
146
147
148
for (size_t pnt = 0; pnt < list_lines.size(); pnt++)
149
{
150
const int x = cvRound(list_lines[pnt][0] + list_lines[pnt][2] * 0.5);
151
const int y = cvRound(list_lines[pnt][1]);
152
153
// --------------- Search vertical up-lines --------------- //
154
155
test_lines.clear();
156
uint8_t future_pixel_up = 255;
157
158
for (int j = y; j < bin_barcode.rows - 1; j++)
159
{
160
next_pixel = bin_barcode.at<uint8_t>(j + 1, x);
161
temp_length++;
162
if (next_pixel == future_pixel_up)
163
{
164
future_pixel_up = 255 - future_pixel_up;
165
test_lines.push_back(temp_length);
166
temp_length = 0;
167
if (test_lines.size() == 3) { break; }
168
}
169
}
170
171
// --------------- Search vertical down-lines --------------- //
172
173
uint8_t future_pixel_down = 255;
174
for (int j = y; j >= 1; j--)
175
{
176
next_pixel = bin_barcode.at<uint8_t>(j - 1, x);
177
temp_length++;
178
if (next_pixel == future_pixel_down)
179
{
180
future_pixel_down = 255 - future_pixel_down;
181
test_lines.push_back(temp_length);
182
temp_length = 0;
183
if (test_lines.size() == 6) { break; }
184
}
185
}
186
187
// --------------- Compute vertical lines --------------- //
188
189
if (test_lines.size() == 6)
190
{
191
double length = 0.0, weight = 0.0;
192
193
for (size_t i = 0; i < test_lines.size(); i++) { length += test_lines[i]; }
194
195
CV_Assert(length > 0);
196
for (size_t i = 0; i < test_lines.size(); i++)
197
{
198
if (i % 3 == 0) { weight += fabs((test_lines[i] / length) - 3.0/14.0); }
199
else { weight += fabs((test_lines[i] / length) - 1.0/ 7.0); }
200
}
201
202
if(weight < eps_horizontal)
203
{
204
result.push_back(list_lines[pnt]);
205
}
206
207
}
208
}
209
210
vector<Point2f> point2f_result;
211
for (size_t i = 0; i < result.size(); i++)
212
{
213
point2f_result.push_back(
214
Point2f(static_cast<float>(result[i][0] + result[i][2] * 0.5),
215
static_cast<float>(result[i][1])));
216
}
217
return point2f_result;
218
}
219
220
void QRDetect::fixationPoints(vector<Point2f> &local_point)
221
{
222
223
double cos_angles[3], norm_triangl[3];
224
225
norm_triangl[0] = norm(local_point[1] - local_point[2]);
226
norm_triangl[1] = norm(local_point[0] - local_point[2]);
227
norm_triangl[2] = norm(local_point[1] - local_point[0]);
228
229
cos_angles[0] = (norm_triangl[1] * norm_triangl[1] + norm_triangl[2] * norm_triangl[2]
230
- norm_triangl[0] * norm_triangl[0]) / (2 * norm_triangl[1] * norm_triangl[2]);
231
cos_angles[1] = (norm_triangl[0] * norm_triangl[0] + norm_triangl[2] * norm_triangl[2]
232
- norm_triangl[1] * norm_triangl[1]) / (2 * norm_triangl[0] * norm_triangl[2]);
233
cos_angles[2] = (norm_triangl[0] * norm_triangl[0] + norm_triangl[1] * norm_triangl[1]
234
- norm_triangl[2] * norm_triangl[2]) / (2 * norm_triangl[0] * norm_triangl[1]);
235
236
const double angle_barrier = 0.85;
237
if (fabs(cos_angles[0]) > angle_barrier || fabs(cos_angles[1]) > angle_barrier || fabs(cos_angles[2]) > angle_barrier)
238
{
239
local_point.clear();
240
return;
241
}
242
243
size_t i_min_cos =
244
(cos_angles[0] < cos_angles[1] && cos_angles[0] < cos_angles[2]) ? 0 :
245
(cos_angles[1] < cos_angles[0] && cos_angles[1] < cos_angles[2]) ? 1 : 2;
246
247
size_t index_max = 0;
248
double max_area = std::numeric_limits<double>::min();
249
for (size_t i = 0; i < local_point.size(); i++)
250
{
251
const size_t current_index = i % 3;
252
const size_t left_index = (i + 1) % 3;
253
const size_t right_index = (i + 2) % 3;
254
255
const Point2f current_point(local_point[current_index]),
256
left_point(local_point[left_index]), right_point(local_point[right_index]),
257
central_point(intersectionLines(current_point,
258
Point2f(static_cast<float>((local_point[left_index].x + local_point[right_index].x) * 0.5),
259
static_cast<float>((local_point[left_index].y + local_point[right_index].y) * 0.5)),
260
Point2f(0, static_cast<float>(bin_barcode.rows - 1)),
261
Point2f(static_cast<float>(bin_barcode.cols - 1),
262
static_cast<float>(bin_barcode.rows - 1))));
263
264
265
vector<Point2f> list_area_pnt;
266
list_area_pnt.push_back(current_point);
267
268
vector<LineIterator> list_line_iter;
269
list_line_iter.push_back(LineIterator(bin_barcode, current_point, left_point));
270
list_line_iter.push_back(LineIterator(bin_barcode, current_point, central_point));
271
list_line_iter.push_back(LineIterator(bin_barcode, current_point, right_point));
272
273
for (size_t k = 0; k < list_line_iter.size(); k++)
274
{
275
uint8_t future_pixel = 255, count_index = 0;
276
for(int j = 0; j < list_line_iter[k].count; j++, ++list_line_iter[k])
277
{
278
if (list_line_iter[k].pos().x >= bin_barcode.cols ||
279
list_line_iter[k].pos().y >= bin_barcode.rows) { break; }
280
const uint8_t value = bin_barcode.at<uint8_t>(list_line_iter[k].pos());
281
if (value == future_pixel)
282
{
283
future_pixel = 255 - future_pixel;
284
count_index++;
285
if (count_index == 3)
286
{
287
list_area_pnt.push_back(list_line_iter[k].pos());
288
break;
289
}
290
}
291
}
292
}
293
294
const double temp_check_area = contourArea(list_area_pnt);
295
if (temp_check_area > max_area)
296
{
297
index_max = current_index;
298
max_area = temp_check_area;
299
}
300
301
}
302
if (index_max == i_min_cos) { std::swap(local_point[0], local_point[index_max]); }
303
else { local_point.clear(); return; }
304
305
const Point2f rpt = local_point[0], bpt = local_point[1], gpt = local_point[2];
306
Matx22f m(rpt.x - bpt.x, rpt.y - bpt.y, gpt.x - rpt.x, gpt.y - rpt.y);
307
if( determinant(m) > 0 )
308
{
309
std::swap(local_point[1], local_point[2]);
310
}
311
}
312
313
bool QRDetect::localization()
314
{
315
Point2f begin, end;
316
vector<Vec3d> list_lines_x = searchHorizontalLines();
317
if( list_lines_x.empty() ) { return false; }
318
vector<Point2f> list_lines_y = separateVerticalLines(list_lines_x);
319
if( list_lines_y.size() < 3 ) { return false; }
320
321
vector<Point2f> centers;
322
Mat labels;
323
kmeans(list_lines_y, 3, labels,
324
TermCriteria( TermCriteria::EPS + TermCriteria::COUNT, 10, 0.1),
325
3, KMEANS_PP_CENTERS, localization_points);
326
327
fixationPoints(localization_points);
328
if (localization_points.size() != 3) { return false; }
329
330
if (coeff_expansion > 1.0)
331
{
332
const int width = cvRound(bin_barcode.size().width / coeff_expansion);
333
const int height = cvRound(bin_barcode.size().height / coeff_expansion);
334
Size new_size(width, height);
335
Mat intermediate;
336
resize(bin_barcode, intermediate, new_size, 0, 0, INTER_LINEAR);
337
bin_barcode = intermediate.clone();
338
for (size_t i = 0; i < localization_points.size(); i++)
339
{
340
localization_points[i] /= coeff_expansion;
341
}
342
}
343
344
for (size_t i = 0; i < localization_points.size(); i++)
345
{
346
for (size_t j = i + 1; j < localization_points.size(); j++)
347
{
348
if (norm(localization_points[i] - localization_points[j]) < 10)
349
{
350
return false;
351
}
352
}
353
}
354
return true;
355
356
}
357
358
bool QRDetect::computeTransformationPoints()
359
{
360
if (localization_points.size() != 3) { return false; }
361
362
vector<Point> locations, non_zero_elem[3], newHull;
363
vector<Point2f> new_non_zero_elem[3];
364
for (size_t i = 0; i < 3; i++)
365
{
366
Mat mask = Mat::zeros(bin_barcode.rows + 2, bin_barcode.cols + 2, CV_8UC1);
367
uint8_t next_pixel, future_pixel = 255;
368
int count_test_lines = 0, index = cvRound(localization_points[i].x);
369
for (; index < bin_barcode.cols - 1; index++)
370
{
371
next_pixel = bin_barcode.at<uint8_t>(
372
cvRound(localization_points[i].y), index + 1);
373
if (next_pixel == future_pixel)
374
{
375
future_pixel = 255 - future_pixel;
376
count_test_lines++;
377
if (count_test_lines == 2)
378
{
379
floodFill(bin_barcode, mask,
380
Point(index + 1, cvRound(localization_points[i].y)), 255,
381
0, Scalar(), Scalar(), FLOODFILL_MASK_ONLY);
382
break;
383
}
384
}
385
}
386
Mat mask_roi = mask(Range(1, bin_barcode.rows - 1), Range(1, bin_barcode.cols - 1));
387
findNonZero(mask_roi, non_zero_elem[i]);
388
newHull.insert(newHull.end(), non_zero_elem[i].begin(), non_zero_elem[i].end());
389
}
390
convexHull(Mat(newHull), locations);
391
for (size_t i = 0; i < locations.size(); i++)
392
{
393
for (size_t j = 0; j < 3; j++)
394
{
395
for (size_t k = 0; k < non_zero_elem[j].size(); k++)
396
{
397
if (locations[i] == non_zero_elem[j][k])
398
{
399
new_non_zero_elem[j].push_back(locations[i]);
400
}
401
}
402
}
403
}
404
405
double pentagon_diag_norm = -1;
406
Point2f down_left_edge_point, up_right_edge_point, up_left_edge_point;
407
for (size_t i = 0; i < new_non_zero_elem[1].size(); i++)
408
{
409
for (size_t j = 0; j < new_non_zero_elem[2].size(); j++)
410
{
411
double temp_norm = norm(new_non_zero_elem[1][i] - new_non_zero_elem[2][j]);
412
if (temp_norm > pentagon_diag_norm)
413
{
414
down_left_edge_point = new_non_zero_elem[1][i];
415
up_right_edge_point = new_non_zero_elem[2][j];
416
pentagon_diag_norm = temp_norm;
417
}
418
}
419
}
420
421
if (down_left_edge_point == Point2f(0, 0) ||
422
up_right_edge_point == Point2f(0, 0) ||
423
new_non_zero_elem[0].size() == 0) { return false; }
424
425
double max_area = -1;
426
up_left_edge_point = new_non_zero_elem[0][0];
427
428
for (size_t i = 0; i < new_non_zero_elem[0].size(); i++)
429
{
430
vector<Point2f> list_edge_points;
431
list_edge_points.push_back(new_non_zero_elem[0][i]);
432
list_edge_points.push_back(down_left_edge_point);
433
list_edge_points.push_back(up_right_edge_point);
434
435
double temp_area = fabs(contourArea(list_edge_points));
436
437
if (max_area < temp_area)
438
{
439
up_left_edge_point = new_non_zero_elem[0][i];
440
max_area = temp_area;
441
}
442
}
443
444
Point2f down_max_delta_point, up_max_delta_point;
445
double norm_down_max_delta = -1, norm_up_max_delta = -1;
446
for (size_t i = 0; i < new_non_zero_elem[1].size(); i++)
447
{
448
double temp_norm_delta = norm(up_left_edge_point - new_non_zero_elem[1][i])
449
+ norm(down_left_edge_point - new_non_zero_elem[1][i]);
450
if (norm_down_max_delta < temp_norm_delta)
451
{
452
down_max_delta_point = new_non_zero_elem[1][i];
453
norm_down_max_delta = temp_norm_delta;
454
}
455
}
456
457
458
for (size_t i = 0; i < new_non_zero_elem[2].size(); i++)
459
{
460
double temp_norm_delta = norm(up_left_edge_point - new_non_zero_elem[2][i])
461
+ norm(up_right_edge_point - new_non_zero_elem[2][i]);
462
if (norm_up_max_delta < temp_norm_delta)
463
{
464
up_max_delta_point = new_non_zero_elem[2][i];
465
norm_up_max_delta = temp_norm_delta;
466
}
467
}
468
469
transformation_points.push_back(down_left_edge_point);
470
transformation_points.push_back(up_left_edge_point);
471
transformation_points.push_back(up_right_edge_point);
472
transformation_points.push_back(
473
intersectionLines(down_left_edge_point, down_max_delta_point,
474
up_right_edge_point, up_max_delta_point));
475
476
vector<Point2f> quadrilateral = getQuadrilateral(transformation_points);
477
transformation_points = quadrilateral;
478
479
return true;
480
}
481
482
Point2f QRDetect::intersectionLines(Point2f a1, Point2f a2, Point2f b1, Point2f b2)
483
{
484
Point2f result_square_angle(
485
((a1.x * a2.y - a1.y * a2.x) * (b1.x - b2.x) -
486
(b1.x * b2.y - b1.y * b2.x) * (a1.x - a2.x)) /
487
((a1.x - a2.x) * (b1.y - b2.y) -
488
(a1.y - a2.y) * (b1.x - b2.x)),
489
((a1.x * a2.y - a1.y * a2.x) * (b1.y - b2.y) -
490
(b1.x * b2.y - b1.y * b2.x) * (a1.y - a2.y)) /
491
((a1.x - a2.x) * (b1.y - b2.y) -
492
(a1.y - a2.y) * (b1.x - b2.x))
493
);
494
return result_square_angle;
495
}
496
497
// test function (if true then ------> else <------ )
498
bool QRDetect::testBypassRoute(vector<Point2f> hull, int start, int finish)
499
{
500
int index_hull = start, next_index_hull, hull_size = (int)hull.size();
501
double test_length[2] = { 0.0, 0.0 };
502
do
503
{
504
next_index_hull = index_hull + 1;
505
if (next_index_hull == hull_size) { next_index_hull = 0; }
506
test_length[0] += norm(hull[index_hull] - hull[next_index_hull]);
507
index_hull = next_index_hull;
508
}
509
while(index_hull != finish);
510
511
index_hull = start;
512
do
513
{
514
next_index_hull = index_hull - 1;
515
if (next_index_hull == -1) { next_index_hull = hull_size - 1; }
516
test_length[1] += norm(hull[index_hull] - hull[next_index_hull]);
517
index_hull = next_index_hull;
518
}
519
while(index_hull != finish);
520
521
if (test_length[0] < test_length[1]) { return true; } else { return false; }
522
}
523
524
vector<Point2f> QRDetect::getQuadrilateral(vector<Point2f> angle_list)
525
{
526
size_t angle_size = angle_list.size();
527
uint8_t value, mask_value;
528
Mat mask = Mat::zeros(bin_barcode.rows + 2, bin_barcode.cols + 2, CV_8UC1);
529
Mat fill_bin_barcode = bin_barcode.clone();
530
for (size_t i = 0; i < angle_size; i++)
531
{
532
LineIterator line_iter(bin_barcode, angle_list[ i % angle_size],
533
angle_list[(i + 1) % angle_size]);
534
for(int j = 0; j < line_iter.count; j++, ++line_iter)
535
{
536
value = bin_barcode.at<uint8_t>(line_iter.pos());
537
mask_value = mask.at<uint8_t>(line_iter.pos() + Point(1, 1));
538
if (value == 0 && mask_value == 0)
539
{
540
floodFill(fill_bin_barcode, mask, line_iter.pos(), 255,
541
0, Scalar(), Scalar(), FLOODFILL_MASK_ONLY);
542
}
543
}
544
}
545
vector<Point> locations;
546
Mat mask_roi = mask(Range(1, bin_barcode.rows - 1), Range(1, bin_barcode.cols - 1));
547
548
findNonZero(mask_roi, locations);
549
550
for (size_t i = 0; i < angle_list.size(); i++)
551
{
552
int x = cvRound(angle_list[i].x);
553
int y = cvRound(angle_list[i].y);
554
locations.push_back(Point(x, y));
555
}
556
557
vector<Point> integer_hull;
558
convexHull(Mat(locations), integer_hull);
559
int hull_size = (int)integer_hull.size();
560
vector<Point2f> hull(hull_size);
561
for (int i = 0; i < hull_size; i++)
562
{
563
float x = saturate_cast<float>(integer_hull[i].x);
564
float y = saturate_cast<float>(integer_hull[i].y);
565
hull[i] = Point2f(x, y);
566
}
567
568
const double experimental_area = fabs(contourArea(hull));
569
570
vector<Point2f> result_hull_point(angle_size);
571
double min_norm;
572
for (size_t i = 0; i < angle_size; i++)
573
{
574
min_norm = std::numeric_limits<double>::max();
575
Point closest_pnt;
576
for (int j = 0; j < hull_size; j++)
577
{
578
double temp_norm = norm(hull[j] - angle_list[i]);
579
if (min_norm > temp_norm)
580
{
581
min_norm = temp_norm;
582
closest_pnt = hull[j];
583
}
584
}
585
result_hull_point[i] = closest_pnt;
586
}
587
588
int start_line[2] = { 0, 0 }, finish_line[2] = { 0, 0 }, unstable_pnt = 0;
589
for (int i = 0; i < hull_size; i++)
590
{
591
if (result_hull_point[2] == hull[i]) { start_line[0] = i; }
592
if (result_hull_point[1] == hull[i]) { finish_line[0] = start_line[1] = i; }
593
if (result_hull_point[0] == hull[i]) { finish_line[1] = i; }
594
if (result_hull_point[3] == hull[i]) { unstable_pnt = i; }
595
}
596
597
int index_hull, extra_index_hull, next_index_hull, extra_next_index_hull;
598
Point result_side_begin[4], result_side_end[4];
599
600
bool bypass_orientation = testBypassRoute(hull, start_line[0], finish_line[0]);
601
602
min_norm = std::numeric_limits<double>::max();
603
index_hull = start_line[0];
604
do
605
{
606
if (bypass_orientation) { next_index_hull = index_hull + 1; }
607
else { next_index_hull = index_hull - 1; }
608
609
if (next_index_hull == hull_size) { next_index_hull = 0; }
610
if (next_index_hull == -1) { next_index_hull = hull_size - 1; }
611
612
Point angle_closest_pnt = norm(hull[index_hull] - angle_list[1]) >
613
norm(hull[index_hull] - angle_list[2]) ? angle_list[2] : angle_list[1];
614
615
Point intrsc_line_hull =
616
intersectionLines(hull[index_hull], hull[next_index_hull],
617
angle_list[1], angle_list[2]);
618
double temp_norm = getCosVectors(hull[index_hull], intrsc_line_hull, angle_closest_pnt);
619
if (min_norm > temp_norm &&
620
norm(hull[index_hull] - hull[next_index_hull]) >
621
norm(angle_list[1] - angle_list[2]) * 0.1)
622
{
623
min_norm = temp_norm;
624
result_side_begin[0] = hull[index_hull];
625
result_side_end[0] = hull[next_index_hull];
626
}
627
628
629
index_hull = next_index_hull;
630
}
631
while(index_hull != finish_line[0]);
632
633
if (min_norm == std::numeric_limits<double>::max())
634
{
635
result_side_begin[0] = angle_list[1];
636
result_side_end[0] = angle_list[2];
637
}
638
639
min_norm = std::numeric_limits<double>::max();
640
index_hull = start_line[1];
641
bypass_orientation = testBypassRoute(hull, start_line[1], finish_line[1]);
642
do
643
{
644
if (bypass_orientation) { next_index_hull = index_hull + 1; }
645
else { next_index_hull = index_hull - 1; }
646
647
if (next_index_hull == hull_size) { next_index_hull = 0; }
648
if (next_index_hull == -1) { next_index_hull = hull_size - 1; }
649
650
Point angle_closest_pnt = norm(hull[index_hull] - angle_list[0]) >
651
norm(hull[index_hull] - angle_list[1]) ? angle_list[1] : angle_list[0];
652
653
Point intrsc_line_hull =
654
intersectionLines(hull[index_hull], hull[next_index_hull],
655
angle_list[0], angle_list[1]);
656
double temp_norm = getCosVectors(hull[index_hull], intrsc_line_hull, angle_closest_pnt);
657
if (min_norm > temp_norm &&
658
norm(hull[index_hull] - hull[next_index_hull]) >
659
norm(angle_list[0] - angle_list[1]) * 0.05)
660
{
661
min_norm = temp_norm;
662
result_side_begin[1] = hull[index_hull];
663
result_side_end[1] = hull[next_index_hull];
664
}
665
666
index_hull = next_index_hull;
667
}
668
while(index_hull != finish_line[1]);
669
670
if (min_norm == std::numeric_limits<double>::max())
671
{
672
result_side_begin[1] = angle_list[0];
673
result_side_end[1] = angle_list[1];
674
}
675
676
bypass_orientation = testBypassRoute(hull, start_line[0], unstable_pnt);
677
const bool extra_bypass_orientation = testBypassRoute(hull, finish_line[1], unstable_pnt);
678
679
vector<Point2f> result_angle_list(4), test_result_angle_list(4);
680
double min_diff_area = std::numeric_limits<double>::max();
681
index_hull = start_line[0];
682
const double standart_norm = std::max(
683
norm(result_side_begin[0] - result_side_end[0]),
684
norm(result_side_begin[1] - result_side_end[1]));
685
do
686
{
687
if (bypass_orientation) { next_index_hull = index_hull + 1; }
688
else { next_index_hull = index_hull - 1; }
689
690
if (next_index_hull == hull_size) { next_index_hull = 0; }
691
if (next_index_hull == -1) { next_index_hull = hull_size - 1; }
692
693
if (norm(hull[index_hull] - hull[next_index_hull]) < standart_norm * 0.1)
694
{ index_hull = next_index_hull; continue; }
695
696
extra_index_hull = finish_line[1];
697
do
698
{
699
if (extra_bypass_orientation) { extra_next_index_hull = extra_index_hull + 1; }
700
else { extra_next_index_hull = extra_index_hull - 1; }
701
702
if (extra_next_index_hull == hull_size) { extra_next_index_hull = 0; }
703
if (extra_next_index_hull == -1) { extra_next_index_hull = hull_size - 1; }
704
705
if (norm(hull[extra_index_hull] - hull[extra_next_index_hull]) < standart_norm * 0.1)
706
{ extra_index_hull = extra_next_index_hull; continue; }
707
708
test_result_angle_list[0]
709
= intersectionLines(result_side_begin[0], result_side_end[0],
710
result_side_begin[1], result_side_end[1]);
711
test_result_angle_list[1]
712
= intersectionLines(result_side_begin[1], result_side_end[1],
713
hull[extra_index_hull], hull[extra_next_index_hull]);
714
test_result_angle_list[2]
715
= intersectionLines(hull[extra_index_hull], hull[extra_next_index_hull],
716
hull[index_hull], hull[next_index_hull]);
717
test_result_angle_list[3]
718
= intersectionLines(hull[index_hull], hull[next_index_hull],
719
result_side_begin[0], result_side_end[0]);
720
721
const double test_diff_area
722
= fabs(fabs(contourArea(test_result_angle_list)) - experimental_area);
723
if (min_diff_area > test_diff_area)
724
{
725
min_diff_area = test_diff_area;
726
for (size_t i = 0; i < test_result_angle_list.size(); i++)
727
{
728
result_angle_list[i] = test_result_angle_list[i];
729
}
730
}
731
732
extra_index_hull = extra_next_index_hull;
733
}
734
while(extra_index_hull != unstable_pnt);
735
736
index_hull = next_index_hull;
737
}
738
while(index_hull != unstable_pnt);
739
740
// check label points
741
if (norm(result_angle_list[0] - angle_list[1]) > 2) { result_angle_list[0] = angle_list[1]; }
742
if (norm(result_angle_list[1] - angle_list[0]) > 2) { result_angle_list[1] = angle_list[0]; }
743
if (norm(result_angle_list[3] - angle_list[2]) > 2) { result_angle_list[3] = angle_list[2]; }
744
745
// check calculation point
746
if (norm(result_angle_list[2] - angle_list[3]) >
747
(norm(result_angle_list[0] - result_angle_list[1]) +
748
norm(result_angle_list[0] - result_angle_list[3])) * 0.5 )
749
{ result_angle_list[2] = angle_list[3]; }
750
751
return result_angle_list;
752
}
753
754
// / | b
755
// / |
756
// / |
757
// a/ | c
758
759
inline double QRDetect::getCosVectors(Point2f a, Point2f b, Point2f c)
760
{
761
return ((a - b).x * (c - b).x + (a - b).y * (c - b).y) / (norm(a - b) * norm(c - b));
762
}
763
764
struct QRCodeDetector::Impl
765
{
766
public:
767
Impl() { epsX = 0.2; epsY = 0.1; }
768
~Impl() {}
769
770
double epsX, epsY;
771
};
772
773
QRCodeDetector::QRCodeDetector() : p(new Impl) {}
774
QRCodeDetector::~QRCodeDetector() {}
775
776
void QRCodeDetector::setEpsX(double epsX) { p->epsX = epsX; }
777
void QRCodeDetector::setEpsY(double epsY) { p->epsY = epsY; }
778
779
bool QRCodeDetector::detect(InputArray in, OutputArray points) const
780
{
781
Mat inarr = in.getMat();
782
CV_Assert(!inarr.empty());
783
CV_Assert(inarr.type() == CV_8UC1);
784
QRDetect qrdet;
785
qrdet.init(inarr, p->epsX, p->epsY);
786
if (!qrdet.localization()) { return false; }
787
if (!qrdet.computeTransformationPoints()) { return false; }
788
vector<Point2f> pnts2f = qrdet.getTransformationPoints();
789
Mat(pnts2f).convertTo(points, points.fixedType() ? points.type() : CV_32FC2);
790
return true;
791
}
792
793
CV_EXPORTS bool detectQRCode(InputArray in, vector<Point> &points, double eps_x, double eps_y)
794
{
795
QRCodeDetector qrdetector;
796
qrdetector.setEpsX(eps_x);
797
qrdetector.setEpsY(eps_y);
798
799
return qrdetector.detect(in, points);
800
}
801
802
class QRDecode
803
{
804
public:
805
void init(const Mat &src, const vector<Point2f> &points);
806
Mat getIntermediateBarcode() { return intermediate; }
807
Mat getStraightBarcode() { return straight; }
808
size_t getVersion() { return version; }
809
std::string getDecodeInformation() { return result_info; }
810
bool fullDecodingProcess();
811
protected:
812
bool updatePerspective();
813
bool versionDefinition();
814
bool samplingForVersion();
815
bool decodingProcess();
816
Mat original, no_border_intermediate, intermediate, straight;
817
vector<Point2f> original_points;
818
std::string result_info;
819
uint8_t version, version_size;
820
float test_perspective_size;
821
};
822
823
void QRDecode::init(const Mat &src, const vector<Point2f> &points)
824
{
825
original = src.clone();
826
intermediate = Mat::zeros(src.size(), CV_8UC1);
827
original_points = points;
828
version = 0;
829
version_size = 0;
830
test_perspective_size = 251;
831
result_info = "";
832
}
833
834
bool QRDecode::updatePerspective()
835
{
836
const Point2f centerPt = QRDetect::intersectionLines(original_points[0], original_points[2],
837
original_points[1], original_points[3]);
838
if (cvIsNaN(centerPt.x) || cvIsNaN(centerPt.y))
839
return false;
840
841
const Size temporary_size(cvRound(test_perspective_size), cvRound(test_perspective_size));
842
843
vector<Point2f> perspective_points;
844
perspective_points.push_back(Point2f(0.f, 0.f));
845
perspective_points.push_back(Point2f(test_perspective_size, 0.f));
846
847
perspective_points.push_back(Point2f(test_perspective_size, test_perspective_size));
848
perspective_points.push_back(Point2f(0.f, test_perspective_size));
849
850
perspective_points.push_back(Point2f(test_perspective_size * 0.5f, test_perspective_size * 0.5f));
851
852
vector<Point2f> pts = original_points;
853
pts.push_back(centerPt);
854
855
Mat H = findHomography(pts, perspective_points);
856
Mat bin_original;
857
adaptiveThreshold(original, bin_original, 255, ADAPTIVE_THRESH_GAUSSIAN_C, THRESH_BINARY, 83, 2);
858
Mat temp_intermediate;
859
warpPerspective(bin_original, temp_intermediate, H, temporary_size, INTER_NEAREST);
860
no_border_intermediate = temp_intermediate(Range(1, temp_intermediate.rows), Range(1, temp_intermediate.cols));
861
862
const int border = cvRound(0.1 * test_perspective_size);
863
const int borderType = BORDER_CONSTANT;
864
copyMakeBorder(no_border_intermediate, intermediate, border, border, border, border, borderType, Scalar(255));
865
return true;
866
}
867
868
inline Point computeOffset(const vector<Point>& v)
869
{
870
// compute the width/height of convex hull
871
Rect areaBox = boundingRect(v);
872
873
// compute the good offset
874
// the box is consisted by 7 steps
875
// to pick the middle of the stripe, it needs to be 1/14 of the size
876
const int cStep = 7 * 2;
877
Point offset = Point(areaBox.width, areaBox.height);
878
offset /= cStep;
879
return offset;
880
}
881
882
bool QRDecode::versionDefinition()
883
{
884
LineIterator line_iter(intermediate, Point2f(0, 0), Point2f(test_perspective_size, test_perspective_size));
885
Point black_point = Point(0, 0);
886
for(int j = 0; j < line_iter.count; j++, ++line_iter)
887
{
888
const uint8_t value = intermediate.at<uint8_t>(line_iter.pos());
889
if (value == 0) { black_point = line_iter.pos(); break; }
890
}
891
892
Mat mask = Mat::zeros(intermediate.rows + 2, intermediate.cols + 2, CV_8UC1);
893
floodFill(intermediate, mask, black_point, 255, 0, Scalar(), Scalar(), FLOODFILL_MASK_ONLY);
894
895
vector<Point> locations, non_zero_elem;
896
Mat mask_roi = mask(Range(1, intermediate.rows - 1), Range(1, intermediate.cols - 1));
897
findNonZero(mask_roi, non_zero_elem);
898
convexHull(Mat(non_zero_elem), locations);
899
Point offset = computeOffset(locations);
900
901
Point temp_remote = locations[0], remote_point;
902
const Point delta_diff = offset;
903
for (size_t i = 0; i < locations.size(); i++)
904
{
905
if (norm(black_point - temp_remote) <= norm(black_point - locations[i]))
906
{
907
const uint8_t value = intermediate.at<uint8_t>(temp_remote - delta_diff);
908
temp_remote = locations[i];
909
if (value == 0) { remote_point = temp_remote - delta_diff; }
910
else { remote_point = temp_remote - (delta_diff / 2); }
911
}
912
}
913
914
size_t transition_x = 0 , transition_y = 0;
915
916
uint8_t future_pixel = 255;
917
const uint8_t *intermediate_row = intermediate.ptr<uint8_t>(remote_point.y);
918
for(int i = remote_point.x; i < intermediate.cols; i++)
919
{
920
if (intermediate_row[i] == future_pixel)
921
{
922
future_pixel = 255 - future_pixel;
923
transition_x++;
924
}
925
}
926
927
future_pixel = 255;
928
for(int j = remote_point.y; j < intermediate.rows; j++)
929
{
930
const uint8_t value = intermediate.at<uint8_t>(Point(j, remote_point.x));
931
if (value == future_pixel)
932
{
933
future_pixel = 255 - future_pixel;
934
transition_y++;
935
}
936
}
937
938
version = saturate_cast<uint8_t>((std::min(transition_x, transition_y) - 1) * 0.25 - 1);
939
if ( !( 0 < version && version <= 40 ) ) { return false; }
940
version_size = 21 + (version - 1) * 4;
941
return true;
942
}
943
944
bool QRDecode::samplingForVersion()
945
{
946
const double multiplyingFactor = (version < 3) ? 1 :
947
(version == 3) ? 1.5 :
948
version * (5 + version - 4);
949
const Size newFactorSize(
950
cvRound(no_border_intermediate.size().width * multiplyingFactor),
951
cvRound(no_border_intermediate.size().height * multiplyingFactor));
952
Mat postIntermediate(newFactorSize, CV_8UC1);
953
resize(no_border_intermediate, postIntermediate, newFactorSize, 0, 0, INTER_AREA);
954
955
const int no_inter_rows = postIntermediate.rows;
956
const int no_inter_cols = postIntermediate.cols;
957
const int delta_rows = cvRound((no_inter_rows * 1.0) / version_size);
958
const int delta_cols = cvRound((no_inter_cols * 1.0) / version_size);
959
960
vector<double> listFrequencyElem;
961
for (int r = 0; r < no_inter_rows; r += delta_rows)
962
{
963
for (int c = 0; c < no_inter_cols; c += delta_cols)
964
{
965
Mat tile = postIntermediate(
966
Range(r, min(r + delta_rows, no_inter_rows)),
967
Range(c, min(c + delta_cols, no_inter_cols)));
968
const double frequencyElem = (countNonZero(tile) * 1.0) / tile.total();
969
listFrequencyElem.push_back(frequencyElem);
970
}
971
}
972
973
double dispersionEFE = std::numeric_limits<double>::max();
974
double experimentalFrequencyElem = 0;
975
for (double expVal = 0; expVal < 1; expVal+=0.001)
976
{
977
double testDispersionEFE = 0.0;
978
for (size_t i = 0; i < listFrequencyElem.size(); i++)
979
{
980
testDispersionEFE += (listFrequencyElem[i] - expVal) *
981
(listFrequencyElem[i] - expVal);
982
}
983
testDispersionEFE /= (listFrequencyElem.size() - 1);
984
if (dispersionEFE > testDispersionEFE)
985
{
986
dispersionEFE = testDispersionEFE;
987
experimentalFrequencyElem = expVal;
988
}
989
}
990
991
straight = Mat(Size(version_size, version_size), CV_8UC1, Scalar(0));
992
size_t k = 0;
993
for (int r = 0; r < no_inter_rows &&
994
k < listFrequencyElem.size() &&
995
floor((r * 1.0) / delta_rows) < version_size; r += delta_rows)
996
{
997
for (int c = 0; c < no_inter_cols &&
998
k < listFrequencyElem.size() &&
999
floor((c * 1.0) / delta_cols) < version_size; c += delta_cols, k++)
1000
{
1001
Mat tile = postIntermediate(
1002
Range(r, min(r + delta_rows, no_inter_rows)),
1003
Range(c, min(c + delta_cols, no_inter_cols)));
1004
1005
if (listFrequencyElem[k] < experimentalFrequencyElem) { tile.setTo(0); }
1006
else
1007
{
1008
tile.setTo(255);
1009
straight.at<uint8_t>(cvRound(floor((r * 1.0) / delta_rows)),
1010
cvRound(floor((c * 1.0) / delta_cols))) = 255;
1011
}
1012
}
1013
}
1014
return true;
1015
}
1016
1017
bool QRDecode::decodingProcess()
1018
{
1019
#ifdef HAVE_QUIRC
1020
if (straight.empty()) { return false; }
1021
1022
quirc_code qr_code;
1023
memset(&qr_code, 0, sizeof(qr_code));
1024
1025
qr_code.size = straight.size().width;
1026
for (int x = 0; x < qr_code.size; x++)
1027
{
1028
for (int y = 0; y < qr_code.size; y++)
1029
{
1030
int position = y * qr_code.size + x;
1031
qr_code.cell_bitmap[position >> 3]
1032
|= straight.at<uint8_t>(y, x) ? 0 : (1 << (position & 7));
1033
}
1034
}
1035
1036
quirc_data qr_code_data;
1037
quirc_decode_error_t errorCode = quirc_decode(&qr_code, &qr_code_data);
1038
if (errorCode != 0) { return false; }
1039
1040
for (int i = 0; i < qr_code_data.payload_len; i++)
1041
{
1042
result_info += qr_code_data.payload[i];
1043
}
1044
return true;
1045
#else
1046
return false;
1047
#endif
1048
1049
}
1050
1051
bool QRDecode::fullDecodingProcess()
1052
{
1053
#ifdef HAVE_QUIRC
1054
if (!updatePerspective()) { return false; }
1055
if (!versionDefinition()) { return false; }
1056
if (!samplingForVersion()) { return false; }
1057
if (!decodingProcess()) { return false; }
1058
return true;
1059
#else
1060
std::cout << "Library QUIRC is not linked. No decoding is performed. Take it to the OpenCV repository." << std::endl;
1061
return false;
1062
#endif
1063
}
1064
1065
CV_EXPORTS bool decodeQRCode(InputArray in, InputArray points, std::string &decoded_info, OutputArray straight_qrcode)
1066
{
1067
Mat inarr = in.getMat();
1068
CV_Assert(!inarr.empty());
1069
inarr.convertTo(inarr, CV_8UC1);
1070
1071
CV_Assert(points.isVector());
1072
vector<Point2f> src_points;
1073
points.copyTo(src_points);
1074
CV_Assert(src_points.size() == 4);
1075
CV_CheckGT(contourArea(src_points), 0.0, "Invalid QR code source points");
1076
1077
QRDecode qrdec;
1078
qrdec.init(inarr, src_points);
1079
bool exit_flag = qrdec.fullDecodingProcess();
1080
1081
decoded_info = qrdec.getDecodeInformation();
1082
1083
if (exit_flag && straight_qrcode.needed())
1084
{
1085
qrdec.getStraightBarcode().convertTo(straight_qrcode,
1086
straight_qrcode.fixedType() ?
1087
straight_qrcode.type() : CV_32FC2);
1088
}
1089
1090
return exit_flag;
1091
}
1092
1093
}
1094
1095