Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: Fix segfault in QRCodeDetector.detectAndDecodeCurved #23802

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
88 changes: 68 additions & 20 deletions modules/objdetect/src/qrcode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1053,7 +1053,7 @@ class QRDecode
bool addPointsToSides();
void completeAndSortSides();
vector<vector<float> > computeSpline(const vector<int> &x_arr, const vector<int> &y_arr);
bool createSpline(vector<vector<Point2f> > &spline_lines);
bool getSplineInterpolation(vector<vector<Point2f> > &spline_lines);
bool divideIntoEvenSegments(vector<vector<Point2f> > &segments_points);
bool straightenQRCodeInParts();
bool preparingCurvedQRCodes();
Expand Down Expand Up @@ -1973,10 +1973,48 @@ vector<vector<float> > QRDecode::computeSpline(const vector<int> &x_arr, const v
return S;
}

bool QRDecode::createSpline(vector<vector<Point2f> > &spline_lines)
enum SplinePointsStatus {
OK = 0,
NEED_SWAP_AXES,
NEED_ADD_PARAMETRIZATION
Comment on lines +1977 to +1979
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With parameterized spline we do not need branches any more.

};

// get horizontal_order and update x_arr, y_arr if necessary
static inline SplinePointsStatus checkSplinePoints(vector<int>& x_arr, vector<int>& y_arr) {
SplinePointsStatus status = abs(x_arr.front() - x_arr.back()) > abs(y_arr.front() - y_arr.back()) ? NEED_SWAP_AXES : OK;
bool duplicateX = false, duplicateY = false;
for (size_t i = 1ull; i < x_arr.size(); i++) {
if (x_arr[i] == x_arr[i-1]) {
duplicateX = true;
break;
}
}
for (size_t i = 1ull; i < y_arr.size(); i++) {
if (y_arr[i] == y_arr[i-1]) {
duplicateY = true;
break;
}
}

// Only the points of the function can be interpolated using splines.
// Contour points may not be a function, there may be several points with the same x coordinate and different y.

if (duplicateX || duplicateY) {
if (duplicateX == false && duplicateY)
return NEED_SWAP_AXES;
else if (duplicateY == false && duplicateX)
return OK;
else // if duplicateX && duplicateY, then need to use parametrazation
return NEED_ADD_PARAMETRIZATION;
}
return status;
}

// Данная функция интерполирует точки с помощью сплайнов
bool QRDecode::getSplineInterpolation(vector<vector<Point2f> > &spline_lines)
{
int start, end;
vector<vector<float> > S;
vector<vector<float> > S, S2;

for (int idx = 0; idx < NUM_SIDES; idx++)
{
Expand All @@ -1987,19 +2025,28 @@ bool QRDecode::createSpline(vector<vector<Point2f> > &spline_lines)

for (size_t j = 0; j < spline_points.size(); j++)
{
x_arr.push_back(cvRound(spline_points[j].x));
y_arr.push_back(cvRound(spline_points[j].y));
x_arr.push_back(spline_points[j].x);
y_arr.push_back(spline_points[j].y);
}

bool horizontal_order = abs(x_arr.front() - x_arr.back()) > abs(y_arr.front() - y_arr.back());
vector<int>& second_arr = horizontal_order ? x_arr : y_arr;
vector<int>& first_arr = horizontal_order ? y_arr : x_arr;
SplinePointsStatus pointsStatus = checkSplinePoints(x_arr, y_arr);
if (pointsStatus == SplinePointsStatus::NEED_SWAP_AXES) {
swap(x_arr, y_arr);
}
if (pointsStatus != SplinePointsStatus::NEED_ADD_PARAMETRIZATION) {
S = computeSpline(x_arr, y_arr);
}
else {
vector<int> t(x_arr.size());
std::iota(t.begin(), t.end(), 0);
S = computeSpline(y_arr, t);

S = computeSpline(first_arr, second_arr);
S2 = computeSpline(x_arr, t);
}

int closest_point_first = horizontal_order ? closest_points[idx_curved_side].second.x
int closest_point_first = pointsStatus == NEED_SWAP_AXES ? closest_points[idx_curved_side].second.x
: closest_points[idx_curved_side].second.y;
int closest_point_second = horizontal_order ? closest_points[(idx_curved_side + 1) % 4].second.x
int closest_point_second = pointsStatus == NEED_SWAP_AXES ? closest_points[(idx_curved_side + 1) % 4].second.x
: closest_points[(idx_curved_side + 1) % 4].second.y;

start = idx_curved_side;
Expand All @@ -2010,22 +2057,23 @@ bool QRDecode::createSpline(vector<vector<Point2f> > &spline_lines)
end = idx_curved_side;
}

int closest_point_start = horizontal_order ? closest_points[start].second.x : closest_points[start].second.y;
int closest_point_end = horizontal_order ? closest_points[end].second.x : closest_points[end].second.y;
int closest_point_start = pointsStatus == NEED_SWAP_AXES ? closest_points[start].second.x : closest_points[start].second.y;
int closest_point_end = pointsStatus == NEED_SWAP_AXES ? closest_points[end].second.x : closest_points[end].second.y;

// переменная index, это на самом деле x
for (int index = closest_point_start; index <= closest_point_end; index++)
{
if (index == second_arr.front())
if (index == y_arr.front())
{
spline_lines[idx].push_back(closest_points[start].second);
}
for (size_t i = 0; i < second_arr.size() - 1; i++)
for (size_t i = 0; i < y_arr.size() - 1; i++)
{
if ((index > second_arr[i]) && (index <= second_arr[i + 1]))
if ((index > y_arr[i]) && (index <= y_arr[i + 1]))
{
float val = S[i][0] + S[i][1] * (index - second_arr[i]) + S[i][2] * (index - second_arr[i]) * (index - second_arr[i])
+ S[i][3] * (index - second_arr[i]) * (index - second_arr[i]) * (index - second_arr[i]);
spline_lines[idx].push_back(horizontal_order ? Point2f(static_cast<float>(index), val) : Point2f(val, static_cast<float>(index)));
int x = index - y_arr[i];
float val = S[i][0] + S[i][1]*x + S[i][2]*x*x + S[i][3]*x*x*x;
spline_lines[idx].push_back(pointsStatus == NEED_SWAP_AXES ? Point2f(static_cast<float>(index), val) : Point2f(val, static_cast<float>(index)));
}
}
}
Expand All @@ -2043,7 +2091,7 @@ bool QRDecode::createSpline(vector<vector<Point2f> > &spline_lines)
bool QRDecode::divideIntoEvenSegments(vector<vector<Point2f> > &segments_points)
{
vector<vector<Point2f> > spline_lines(NUM_SIDES);
if (!createSpline(spline_lines))
if (!getSplineInterpolation(spline_lines))
{
return false;
}
Expand Down
12 changes: 12 additions & 0 deletions modules/objdetect/test/test_qrcode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -635,6 +635,18 @@ TEST_P(Objdetect_QRCode_detectAndDecodeMulti, decode_9_qrcodes_version7)
}
}

TEST(Objdetect_QRCode_Curved_Test, detect_regression_22892)
{
const std::string name_current_image = "issue_22892.png";
const std::string root = "qrcode/curved/";

std::string image_path = findDataFile(root + name_current_image);
Mat src = imread(image_path);
auto qcd = QRCodeDetector();
vector<Point2f> points;
ASSERT_NO_THROW(qcd.detectAndDecodeCurved(src, points));
}

#endif // UPDATE_QRCODE_TEST_DATA

}} // namespace