在一組cv::Point 上執行cv::warpPerspective 進行假傾斜校正
問題:
問題: 如何我是否可以使用cv::warpPerspective 對一組點實現傾斜校正效果?這些點不按特定順序存儲,而是存儲在向量中。
影像尺寸不正確:輸出影像的寬度和高度應與邊界匹配
Apply Affine Transform:將輸入影像、輸入點、輸出點和定義的輸出大小傳遞給 cv::warpAffine() 來執行實際的去歪斜變換。
#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中文網其他相關文章!