如何使用 cv::warpPerspective 校正一组点?
在一组 cv::Point 上执行 cv::warpPerspective 进行假倾斜校正
问题: 如何我是否可以使用 cv::warpPerspective 对一组点实现倾斜校正效果?这些点不按特定顺序存储,而是存储在向量中。
理解问题:
- 点排序不正确:点的顺序输入和输出向量必须匹配才能实现所需的转换。
- 图像尺寸不正确:输出图像的宽度和高度应与边界匹配
假校正的步骤:
- 正确的点排序:确保点的顺序输入和输出向量都遵循相同的顺序(例如,左上、左下、右下、右上)。
- 旋转矩形调整:使用 cv::minAreaRect() 围绕输入点创建一个旋转矩形。但请注意,此方法可能会稍微改变原始点坐标。
- 仿射变换:利用仿射变换函数 cv::getAffineTransform() 和 cv::warpAffine(),如下所示对于这种特定的偏移校正操作,它们的计算效率更高。
- 不同的输出Size:要使校正图像仅包含感兴趣的对象,请定义与边界矩形大小匹配的新图像大小(例如,cv::Size(width, height))。
- 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中文网其他相关文章!
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

AI Hentai Generator
免费生成ai无尽的。

热门文章
R.E.P.O.能量晶体解释及其做什么(黄色晶体)
2 周前
By 尊渡假赌尊渡假赌尊渡假赌
仓库:如何复兴队友
4 周前
By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island冒险:如何获得巨型种子
3 周前
By 尊渡假赌尊渡假赌尊渡假赌
击败分裂小说需要多长时间?
3 周前
By DDD
R.E.P.O.保存文件位置:在哪里以及如何保护它?
3 周前
By DDD

热工具

记事本++7.3.1
好用且免费的代码编辑器

SublimeText3汉化版
中文版,非常好用

禅工作室 13.0.1
功能强大的PHP集成开发环境

Dreamweaver CS6
视觉化网页开发工具

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)