目标是应用透视变换为一组点以获得偏移校正效果,类似于下图视频:
[视频链接](http://nuigroup.com/?ACT=28&fid=27&aid=1892_H6eNAaign4Mrnn30Au8d)
提供的示例图片和代码演示了偏移校正效果,但并不准确。具体来说,转换后的图像不包含整个 ROI,并且长宽比不正确。
为了解决这些问题,对代码进行了以下更改:
void main() { cv::Mat src = cv::imread("r8fmh.jpg", 1); // Points representing the corners of the paper 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)); // Debug drawing of the points const Point* point = &not_a_rect_shape[0]; int n = (int)not_a_rect_shape.size(); Mat draw = src.clone(); polylines(draw, &point, &n, 1, true, Scalar(0, 255, 0), 3, CV_AA); imwrite("draw.jpg", draw); // Rotated rectangle around the points RotatedRect box = minAreaRect(cv::Mat(not_a_rect_shape)); // Source and destination vertex points 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); // Affine transform matrix Mat warpAffineMatrix = getAffineTransform(src_vertices, dst_vertices); // Transformation and output image 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); }
以上是如何使用 OpenCV 的'warpAffine”模拟点偏移?的详细内容。更多信息请关注PHP中文网其他相关文章!