Skip to content

Commit f821530

Browse files
committed
Merge pull request opencv#19677 from APrigarina:detection_fix
2 parents 2a808ae + 125cc79 commit f821530

File tree

1 file changed

+30
-8
lines changed

1 file changed

+30
-8
lines changed

modules/objdetect/src/qrcode.cpp

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -235,9 +235,11 @@ vector<Vec3d> QRDetect::searchHorizontalLines()
235235
vector<Point2f> QRDetect::separateVerticalLines(const vector<Vec3d> &list_lines)
236236
{
237237
CV_TRACE_FUNCTION();
238-
239-
for (int coeff_epsilon = 1; coeff_epsilon < 10; coeff_epsilon++)
238+
const double min_dist_between_points = 10.0;
239+
const double max_ratio = 1.0;
240+
for (int coeff_epsilon_i = 1; coeff_epsilon_i < 101; ++coeff_epsilon_i)
240241
{
242+
const float coeff_epsilon = coeff_epsilon_i * 0.1f;
241243
vector<Point2f> point2f_result = extractVerticalLines(list_lines, eps_horizontal * coeff_epsilon);
242244
if (!point2f_result.empty())
243245
{
@@ -247,9 +249,23 @@ vector<Point2f> QRDetect::separateVerticalLines(const vector<Vec3d> &list_lines)
247249
point2f_result, 3, labels,
248250
TermCriteria(TermCriteria::EPS + TermCriteria::COUNT, 10, 0.1),
249251
3, KMEANS_PP_CENTERS, centers);
250-
if (compactness == 0)
252+
double min_dist = std::numeric_limits<double>::max();
253+
for (size_t i = 0; i < centers.size(); i++)
254+
{
255+
double dist = norm(centers[i] - centers[(i+1) % centers.size()]);
256+
if (dist < min_dist)
257+
{
258+
min_dist = dist;
259+
}
260+
}
261+
if (min_dist < min_dist_between_points)
262+
{
251263
continue;
252-
if (compactness > 0)
264+
}
265+
double mean_compactness = compactness / point2f_result.size();
266+
double ratio = mean_compactness / min_dist;
267+
268+
if (ratio < max_ratio)
253269
{
254270
return point2f_result;
255271
}
@@ -456,15 +472,14 @@ bool QRDetect::localization()
456472
vector<Point2f> list_lines_y = separateVerticalLines(list_lines_x);
457473
if( list_lines_y.empty() ) { return false; }
458474

459-
vector<Point2f> centers;
460475
Mat labels;
461476
kmeans(list_lines_y, 3, labels,
462477
TermCriteria( TermCriteria::EPS + TermCriteria::COUNT, 10, 0.1),
463478
3, KMEANS_PP_CENTERS, localization_points);
464479

465480
fixationPoints(localization_points);
466481

467-
bool suare_flag = false, local_points_flag = false;
482+
bool square_flag = false, local_points_flag = false;
468483
double triangle_sides[3];
469484
double triangle_perim, square_area, img_square_area;
470485
if (localization_points.size() == 3)
@@ -482,14 +497,14 @@ bool QRDetect::localization()
482497

483498
if (square_area > (img_square_area * 0.2))
484499
{
485-
suare_flag = true;
500+
square_flag = true;
486501
}
487502
}
488503
else
489504
{
490505
local_points_flag = true;
491506
}
492-
if ((suare_flag || local_points_flag) && purpose == SHRINKING)
507+
if ((square_flag || local_points_flag) && purpose == SHRINKING)
493508
{
494509
localization_points.clear();
495510
bin_barcode = resized_bin_barcode.clone();
@@ -1970,6 +1985,13 @@ bool QRDecode::createSpline(vector<vector<Point2f> > &spline_lines)
19701985
}
19711986
}
19721987
}
1988+
for (int i = 0; i < NUM_SIDES; i++)
1989+
{
1990+
if (spline_lines[i].size() == 0)
1991+
{
1992+
return false;
1993+
}
1994+
}
19731995
return true;
19741996
}
19751997

0 commit comments

Comments
 (0)