OpenCV几何图像变换(3)计算透视变换矩阵函数getPerspectiveTransform()的使用

  • 操作系统:ubuntu22.04
  • OpenCV版本:OpenCV4.9
  • IDE:Visual Studio Code
  • 编程语言:C++11

算法描述

计算四对对应点之间的透视变换。

该函数计算 3×3 的透视变换矩阵,使得:

t i x i ′ t i y i ′ t i \] = map_matrix ⋅ \[ x i y i 1 \] \\begin{bmatrix} t_i x'_i \\\\ t_i y'_i \\\\ t_i \\end{bmatrix} = \\texttt{map\\_matrix} \\cdot \\begin{bmatrix} x_i \\\\ y_i \\\\ 1 \\end{bmatrix} tixi′tiyi′ti =map_matrix⋅ xiyi1 其中, d s t ( i ) = ( x i ′ , y i ′ ) , s r c ( i ) = ( x i , y i ) , i = 0 , 1 , 2 , 3 dst(i)=(x'_i,y'_i), src(i)=(x_i, y_i), i=0,1,2,3 dst(i)=(xi′,yi′),src(i)=(xi,yi),i=0,1,2,3 getPerspectiveTransform() 函数用于计算一个 3×3 的透视变换矩阵,该矩阵将源图像中的四边形映射到目标图像中的另一个四边形。这种变换可以实现图像的透视校正,例如将倾斜的图像拉直或调整视角。 ### 函数原型1 ```cpp Mat cv::getPerspectiveTransform ( InputArray src, InputArray dst, int solveMethod = DECOMP_LU ) ``` ### 函数参数1 * 参数src 源图像中四边形顶点的坐标。 * 参数dst 目标图像中对应四边形顶点的坐标。 * 参数solveMethod 传递给 cv::solve 函数的方法(DecompTypes 类型)。 ### 函数原型2 ```cpp Mat cv::getPerspectiveTransform ( const Point2f src[], const Point2f dst[], int solveMethod = DECOMP_LU ) ``` ### 函数参数2 * 参数src 源图像中四边形顶点的坐标。 * 参数dst 目标图像中对应四边形顶点的坐标。 * 参数solveMethod 传递给 cv::solve 函数的方法(DecompTypes 类型)。 ### 示例代码 ```cpp #include #include using namespace cv; using namespace std; int main() { // 加载图像 Mat src = imread("/media/dingxin/data/study/OpenCV/sources/images/hawk.jpg"); if (src.empty()) { cout << "Error: Image not found." << endl; return -1; } // 定义源图像中的四个点 Point2f srcQuad[4] = {Point2f(0, 0), Point2f(src.cols - 1, 0), Point2f(src.cols - 1, src.rows - 1), Point2f(0, src.rows - 1)}; // 定义目标图像中的四个点 Point2f dstQuad[4] = {Point2f(0, 0), Point2f(src.cols * 0.5, 0), Point2f(src.cols * 0.5, src.rows), Point2f(0, src.rows)}; // 获取透视变换矩阵 Mat perspMat = getPerspectiveTransform(srcQuad, dstQuad); // 应用透视变换 Mat warpedImage; warpPerspective(src, warpedImage, perspMat, src.size()); // 显示原图像和变换后的图像 namedWindow("Original Image", WINDOW_NORMAL); imshow("Original Image", src); namedWindow("Warped Image", WINDOW_NORMAL); imshow("Warped Image", warpedImage); // 等待按键并关闭窗口 waitKey(0); destroyAllWindows(); return 0; } ``` ### 运行结果 ![在这里插入图片描述](https://i-blog.csdnimg.cn/direct/b84a784a2e7d4b5dbd8ad41280c67436.png)

相关推荐
mit6.82435 分钟前
[AI React Web] 包与依赖管理 | `axios`库 | `framer-motion`库
前端·人工智能·react.js
小阿鑫1 小时前
不要太信任Cursor,这位网友被删库了。。。
人工智能·aigc·cursor·部署mcp
说私域1 小时前
基于定制开发开源 AI 智能名片 S2B2C 商城小程序的热点与人工下发策略研究
人工智能·小程序
GoGeekBaird2 小时前
GoHumanLoopHub开源上线,开启Agent人际协作新方式
人工智能·后端·github
Jinkxs2 小时前
测试工程师的AI转型指南:从工具使用到测试策略重构
人工智能·重构
别惹CC3 小时前
Spring AI 进阶之路01:三步将 AI 整合进 Spring Boot
人工智能·spring boot·spring
stbomei5 小时前
当 AI 开始 “理解” 情感:情感计算技术正在改写人机交互规则
人工智能·人机交互
Moshow郑锴10 小时前
人工智能中的(特征选择)数据过滤方法和包裹方法
人工智能
TY-202510 小时前
【CV 目标检测】Fast RCNN模型①——与R-CNN区别
人工智能·目标检测·目标跟踪·cnn
CareyWYR11 小时前
苹果芯片Mac使用Docker部署MinerU api服务
人工智能