首页 后端开发 C++ 如何使用 cv::warpPerspective 校正一组点?

如何使用 cv::warpPerspective 校正一组点?

Dec 03, 2024 pm 05:40 PM

How to Deskew a Set of Points Using cv::warpPerspective?

在一组 cv::Point 上执行 cv::warpPerspective 进行假倾斜校正

问题: 如何我是否可以使用 cv::warpPerspective 对一组点实现倾斜校正效果?这些点不按特定顺序存储,而是存储在向量中。

理解问题:

  • 点排序不正确:点的顺序输入和输出向量必须匹配才能实现所需的转换。
  • 图像尺寸不正确:输出图像的宽度和高度应与边界匹配

假校正的步骤:

  1. 正确的点排序:确保点的顺序输入和输出向量都遵循相同的顺序(例如,左上、左下、右下、右上)。
  2. 旋转矩形调整:使用 cv::minAreaRect() 围绕输入点创建一个旋转矩形。但请注意,此方法可能会稍微改变原始点坐标。
  3. 仿射变换:利用仿射变换函数 cv::getAffineTransform() 和 cv::warpAffine(),如下所示对于这种特定的偏移校正操作,它们的计算效率更高。
  4. 不同的输出Size:要使校正图像仅包含感兴趣的对象,请定义与边界矩形大小匹配的新图像大小(例如,cv::Size(width, height))。
  5. 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

Undresser.AI Undress

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

AI Clothes Remover

AI Clothes Remover

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

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

AI Hentai Generator

AI Hentai Generator

免费生成ai无尽的。

热门文章

R.E.P.O.能量晶体解释及其做什么(黄色晶体)
2 周前 By 尊渡假赌尊渡假赌尊渡假赌
仓库:如何复兴队友
4 周前 By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island冒险:如何获得巨型种子
3 周前 By 尊渡假赌尊渡假赌尊渡假赌

热工具

记事本++7.3.1

记事本++7.3.1

好用且免费的代码编辑器

SublimeText3汉化版

SublimeText3汉化版

中文版,非常好用

禅工作室 13.0.1

禅工作室 13.0.1

功能强大的PHP集成开发环境

Dreamweaver CS6

Dreamweaver CS6

视觉化网页开发工具

SublimeText3 Mac版

SublimeText3 Mac版

神级代码编辑软件(SublimeText3)

c语言函数返回值的类型有哪些?返回值是由什么决定的? c语言函数返回值的类型有哪些?返回值是由什么决定的? Mar 03, 2025 pm 05:52 PM

c语言函数返回值的类型有哪些?返回值是由什么决定的?

Gulc:从头开始建造的C库 Gulc:从头开始建造的C库 Mar 03, 2025 pm 05:46 PM

Gulc:从头开始建造的C库

c语言函数格式字母大小写转换步骤 c语言函数格式字母大小写转换步骤 Mar 03, 2025 pm 05:53 PM

c语言函数格式字母大小写转换步骤

c语言函数的定义和调用规则是什么 c语言函数的定义和调用规则是什么 Mar 03, 2025 pm 05:53 PM

c语言函数的定义和调用规则是什么

c语言函数返回值在内存保存在哪里? c语言函数返回值在内存保存在哪里? Mar 03, 2025 pm 05:51 PM

c语言函数返回值在内存保存在哪里?

distinct用法和短语分享 distinct用法和短语分享 Mar 03, 2025 pm 05:51 PM

distinct用法和短语分享

如何有效地使用STL(排序,查找,转换等)的算法? 如何有效地使用STL(排序,查找,转换等)的算法? Mar 12, 2025 pm 04:52 PM

如何有效地使用STL(排序,查找,转换等)的算法?

C标准模板库(STL)如何工作? C标准模板库(STL)如何工作? Mar 12, 2025 pm 04:50 PM

C标准模板库(STL)如何工作?

See all articles