Home >Backend Development >C++ >How to Deskew a Set of Points Using cv::warpPerspective?
Executing cv::warpPerspective for a Fake Deskewing on a Set of cv::Point
Question: How do I achieve a deskewing effect on a set of points using cv::warpPerspective? The points are not in a particular order and are stored within a vector.
Understanding the Issue:
Steps for Fake Deskewing:
Example Code:
#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); }
The above is the detailed content of How to Deskew a Set of Points Using cv::warpPerspective?. For more information, please follow other related articles on the PHP Chinese website!