Merge pull request #25145 from MaximSmolskiy:improve-contours-approximations-in-ChessBoardDetector-generateQuads-2

Improve contours approximations in ChessBoardDetector::generateQuads
This commit is contained in:
Alexander Smorkalov 2024-03-06 14:03:50 +03:00 committed by GitHub
commit 0e524ee95a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -1796,22 +1796,12 @@ void ChessBoardDetector::generateQuads(const cv::Mat& image_, int flags, int dil
if (contour_rect.area() < min_area)
continue;
std::vector<Point> approx_contour;
std::vector<Point> approx_contour = contour;
const int min_approx_level = 1, max_approx_level = MAX_CONTOUR_APPROX;
for (int approx_level = min_approx_level; approx_level <= max_approx_level; approx_level++ )
for (int approx_level = min_approx_level; approx_contour.size() > 4 && approx_level <= max_approx_level; approx_level++ )
{
approxPolyDP(contour, approx_contour, (float)approx_level, true);
if (approx_contour.size() == 4)
break;
// we call this again on its own output, because sometimes
// approxPoly() does not simplify as much as it should.
std::vector<Point> approx_contour_tmp;
std::swap(approx_contour, approx_contour_tmp);
approxPolyDP(approx_contour_tmp, approx_contour, (float)approx_level, true);
if (approx_contour.size() == 4)
break;
approxPolyDP(approx_contour, approx_contour, (float)approx_level, true);
}
// reject non-quadrangles