Home > Backend Development > C++ > body text

Why Does My cv::warpPerspective Deskewing Implementation Produce Incorrect Results, and How Can I Fix It?

DDD
Release: 2024-11-23 03:32:11
Original
582 people have browsed it

Why Does My cv::warpPerspective Deskewing Implementation Produce Incorrect Results, and How Can I Fix It?

How to Correctly Execute cv::warpPerspective for Deskewing a Set of Points

Problem:

Attempts to use cv::warpPerspective to achieve a deskewing effect on a set of points have yielded unsatisfactory results. The desired deskewing is illustrated by the green rectangle in the image below:

[Image of a document with a green rectangle outlining the region of interest]

Cause:

The incorrect results can be attributed to several factors:

  1. Point Order: The points in the input and output vectors must be in the same order (e.g., top-left, bottom-left, bottom-right, top-right).
  2. Output Image Size: To prevent the resulting image from containing excess background, its width and height should be set to match those of the bounding rectangle around the deskewed region.

Solution:

To resolve these issues, the code below has been modified:

void main()
{
    cv::Mat src = cv::imread("r8fmh.jpg", 1);

    // Points representing the corners of the paper in the picture:
    vector<Point> not_a_rect_shape;
    not_a_rect_shape.push_back(Point(408, 69));
    not_a_rect_shape.push_back(Point(72, 2186));
    not_a_rect_shape.push_back(Point(1584, 2426));
    not_a_rect_shape.push_back(Point(1912, 291));

    // Assemble a rotated rectangle from the points
    RotatedRect box = minAreaRect(cv::Mat(not_a_rect_shape));

    // Extract the corner points of the rotated rectangle
    Point2f pts[4];
    box.points(pts);

    // Define the vertices for the warp transformation
    Point2f src_vertices[3];
    src_vertices[0] = pts[0];
    src_vertices[1] = pts[1];
    src_vertices[2] = pts[3];

    Point2f dst_vertices[3];
    dst_vertices[0] = Point(0, 0);
    dst_vertices[1] = Point(box.boundingRect().width - 1, 0);
    dst_vertices[2] = Point(0, box.boundingRect().height - 1);

    // Use the affine transform as it's faster for the given use case
    Mat warpAffineMatrix = getAffineTransform(src_vertices, dst_vertices);

    cv::Mat rotated;
    cv::Size size(box.boundingRect().width, box.boundingRect().height);
    warpAffine(src, rotated, warpAffineMatrix, size, INTER_LINEAR, BORDER_CONSTANT);

    imwrite("rotated.jpg", rotated);
}
Copy after login

Improvements:

To further enhance efficiency, consider using cv::getAffineTransform() and cv::warpAffine() instead of cv::getPerspectiveTransform() and cv::warpPerspective(). These functions are specifically designed for affine transformations and are significantly faster.

The above is the detailed content of Why Does My cv::warpPerspective Deskewing Implementation Produce Incorrect Results, and How Can I Fix It?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template