在一组 cv::Point 上执行 cv::warpPerspective 进行假倾斜校正
问题: 如何我是否可以使用 cv::warpPerspective 对一组点实现倾斜校正效果?这些点不按特定顺序存储,而是存储在向量中。
理解问题:
假校正的步骤:
示例代码:
#include <opencv2/opencv.hpp> int main() { // Input image Mat src = imread("input.jpg"); // Input points (not in particular order) vector<Point> points = { Point(408, 69), // Top-left Point(72, 2186), // Bottom-left Point(1584, 2426), // Bottom-right Point(1912, 291), // Top-right }; // Rotated rectangle (bounding box) RotatedRect boundingRect = minAreaRect(Mat(points)); // Corrected point ordering Point2f vertices[3]; vertices[0] = boundingRect.center + boundingRect.size * 0.5f; // Top-left vertices[1] = boundingRect.center + boundingRect.size * 0.5f; // Bottom-left vertices[1].y += boundingRect.size.height; vertices[2] = boundingRect.center - boundingRect.size * 0.5f; // Bottom-right // Output point ordering Point2f outputVertices[3]; outputVertices[0] = Point(0, 0); // Top-left outputVertices[1].x = outputVertices[0].x + boundingRect.size.width; // Bottom-left outputVertices[1].y = outputVertices[1].x; outputVertices[2] = outputVertices[0]; // Bottom-right // Affine transformation matrix Mat transformationMatrix = getAffineTransform(vertices, outputVertices); // Deskewed image with corrected size Mat deskewedImage; Size outputSize(boundingRect.size.width, boundingRect.size.height); warpAffine(src, deskewedImage, transformationMatrix, outputSize, INTER_LINEAR); // Save deskewed image imwrite("deskewed.jpg", deskewedImage); }
以上是如何使用 cv::warpPerspective 校正一组点?的详细内容。更多信息请关注PHP中文网其他相关文章!