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:
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); }
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!