问题:
尝试使用 cv ::warpPerspective 在一组点上实现倾斜校正效果取得了不令人满意的结果。下图中的绿色矩形说明了所需的歪斜校正:
[带有绿色矩形概述感兴趣区域的文档图像]
原因:
不正确的结果可能归因于几个方面因子:
解决方案:
为了解决这些问题,下面的代码已修改:
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); }
改进:
要进一步提高效率,请考虑使用 cv::getAffineTransform() 和 cv::warpAffine() 代替 cv:: getPerspectiveTransform() 和 cv::warpPerspective()。这些函数是专门为仿射变换而设计的,并且速度明显更快。
以上是为什么我的 cv::warpPerspective 倾斜校正实施会产生不正确的结果,如何修复它?的详细内容。更多信息请关注PHP中文网其他相关文章!