Perspective Transformation for Deskewing
To achieve a deskewing effect on a set of points using perspective transformation, it's essential to understand the following:
Point Ordering:
The order of points matters in perspective transformation. To ensure accuracy, the order must be consistent in both the source and destination vectors.
Image Size:
If you want the resulting image to only contain the object of interest, set its width and height to match the resulting rectangle's dimensions.
Performance Considerations:
For affine transformations like rotate, resize, and deskew, it's more efficient to use the affine counterparts:
Affine Transform:
getAffineTransform() only requires three points and provides a 2x3 matrix, while warpAffine() performs the warping.
Resizing the Resulting Image:
To resize the resulting image to a different size than the input, use:
cv::Size size(box.boundingRect().width, box.boundingRect().height); cv::warpPerspective(src, dst, size, ... );
Example:
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)); RotatedRect box = minAreaRect(cv::Mat(not_a_rect_shape)); Point2f pts[4]; box.points(pts); 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); Mat warpAffineMatrix = getAffineTransform(src_vertices, dst_vertices); cv::Size size(box.boundingRect().width, box.boundingRect().height); warpAffine(src, rotated, warpAffineMatrix, size, INTER_LINEAR, BORDER_CONSTANT);
The above is the detailed content of How to Deskew Points Using Perspective Transformation and Affine Transforms?. For more information, please follow other related articles on the PHP Chinese website!