問題:
嘗試使用cv ::warpPerspective ::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中文網其他相關文章!