OpenCV图像几何变换之透视变换

 更新时间:2021年03月19日 15:10:17   作者:liekkas0626  
这篇文章主要为大家详细介绍了OpenCV图像几何变换之透视变换,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

本文实例为大家分享了OpenCV图像几何变换之透视变换的具体代码,供大家参考,具体内容如下

1. 基本原理

透视变换(Perspective Transformation)的本质是将图像投影到一个新的视平面,其通用变换公式为:

(u,v)为原始图像像素坐标,(x=x'/w',y=y'/w')为变换之后的图像像素坐标。透视变换矩阵图解如下:

仿射变换(Affine Transformation)可以理解为透视变换的特殊形式。透视变换的数学表达式为:

所以,给定透视变换对应的四对像素点坐标,即可求得透视变换矩阵;反之,给定透视变换矩阵,即可对图像或像素点坐标完成透视变换,如下图所示:

2. OpenCV透视变换函数

Mat getPerspectiveTransform(const Point2f* src, const Point2f* dst)
// Calculate a perspective transform from four pairs of the corresponding points.
// src – Coordinates of quadrangle vertices in the source image.
// dst – Coordinates of the corresponding quadrangle vertices in the destination image.
 
void warpPerspective(InputArray src, OutputArray dst, InputArray M, Size dsize, int flags=INTER_LINEAR, int borderMode=BORDER_CONSTANT, const Scalar& borderValue=Scalar())
// Apply a perspective transform to an image.
// src – Source image.
// dst – Destination image that has the size dsize and the same type as src.
// M – 3*3 transformation matrix.
// dsize – Size of the destination image.
// flags – Combination of interpolation methods and the optional flag WARP_INVERSE_MAP that means that M is the inverse transformation (dstsrc).
// borderMode – Pixel extrapolation method. When borderMode=BORDER_TRANSPARENT, it means that the pixels in the destination image that corresponds to the “outliers” in the source image are not modified by the function.
// borderValue – Value used in case of a constant border. By default, it is 0.

3. 程序

#include <iostream>
 
#include "highgui.h"
#include "opencv2/imgproc/imgproc.hpp"
 
int main()
{
 // get original image.
 cv::Mat originalImage = cv::imread("road.png");
 
 // perspective image.
 cv::Mat perspectiveImage;
 
 // perspective transform
 cv::Point2f objectivePoints[4], imagePoints[4];
 
 // original image points.
 imagePoints[0].x = 10.0; imagePoints[0].y = 457.0;
 imagePoints[1].x = 395.0; imagePoints[1].y = 291.0;
 imagePoints[2].x = 624.0; imagePoints[2].y = 291.0;
 imagePoints[3].x = 1000.0; imagePoints[3].y = 457.0;
 
 // objective points of perspective image.
 // move up the perspective image : objectivePoints.y - value .
 // move left the perspective image : objectivePoints.x - value.
 double moveValueX = 0.0;
 double moveValueY = 0.0;
 
 objectivePoints[0].x = 46.0 + moveValueX; objectivePoints[0].y = 920.0 + moveValueY;
 objectivePoints[1].x = 46.0 + moveValueX; objectivePoints[1].y = 100.0 + moveValueY;
 objectivePoints[2].x = 600.0 + moveValueX; objectivePoints[2].y = 100.0 + moveValueY;
 objectivePoints[3].x = 600.0 + moveValueX; objectivePoints[3].y = 920.0 + moveValueY;
 
 cv::Mat transform = cv::getPerspectiveTransform(objectivePoints, imagePoints);
 
 // perspective.
 cv::warpPerspective(originalImage,
   perspectiveImage,
   transform,
   cv::Size(originalImage.rows, originalImage.cols),
   cv::INTER_LINEAR | cv::WARP_INVERSE_MAP);
 
 // cv::imshow("perspective image", perspectiveImage);
 // cvWaitKey(0);
 
 cv::imwrite("perspectiveImage.png", perspectiveImage);
 
 return 0;
}

原始图像及其透视变换结果:

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

相关文章

  • C语言动态内存管理分析总结

    C语言动态内存管理分析总结

    C语言中开辟内存有很多种方式,目前我们最常用的也就是数组,但数组是在我们用到他之前就得设定好它的长度,有时很不方便。随意我们来探究动态内存管理
    2021-11-11
  • 浅谈C++反向迭代器的设计

    浅谈C++反向迭代器的设计

    本文主要介绍了浅谈C++反向迭代器的设计,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2023-04-04
  • C++实现LeetCode165.版本比较)

    C++实现LeetCode165.版本比较)

    这篇文章主要介绍了C++实现LeetCode165.版本比较),本篇文章通过简要的案例,讲解了该项技术的了解与使用,以下就是详细内容,需要的朋友可以参考下
    2021-07-07
  • C++连接mysql数据库(改进版)

    C++连接mysql数据库(改进版)

    C++是大家都非常熟悉的,也是大家平时办公中经常会用到的,下面这篇文章主要给大家介绍了关于C++连接mysql数据库的相关资料,文中通过代码介绍的非常详细,需要的朋友可以参考下
    2023-12-12
  • C/C++左旋字符串实现代码举例

    C/C++左旋字符串实现代码举例

    在C/C++语言中没有专门的字符串变量,通常用字符数组来存放字符串,下面这篇文章主要给大家介绍了关于C/C++左旋字符串实现的相关资料,需要的朋友可以参考下
    2023-12-12
  • 基于C语言实现简单的扫雷游戏

    基于C语言实现简单的扫雷游戏

    这篇文章主要为大家详细介绍了基于C语言实现简单的扫雷游戏,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2021-10-10
  • VisualStudio2022下配置 OpenMP多线程编程环境与运行

    VisualStudio2022下配置 OpenMP多线程编程环境与运行

    本文主要介绍了VisualStudio2022下配置 OpenMP多线程编程环境与运行,文中通过图文介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2024-06-06
  • QT基于TCP实现网络聊天室程序

    QT基于TCP实现网络聊天室程序

    这篇文章主要为大家详细介绍了QT基于TCP实现网络聊天室程序,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2022-08-08
  • C语言输入一个数判断是否为素数的多种方法

    C语言输入一个数判断是否为素数的多种方法

    素数是只能被1和它自己本身整除,不能被其他自然数整除的大于1的正整数,下面这篇文章主要给大家介绍了关于C语言输入一个数判断是否为素数的多种方法,文中通过实例代码介绍的非常详细,需要的朋友可以参考下
    2023-04-04
  • Seesion在C++服务端的使用方法

    Seesion在C++服务端的使用方法

    这篇文章主要介绍了Seesion在C++服务端是怎么使用的?本文给出了解决方案和实例代码供大家参考,需要的朋友可以参考下
    2020-02-02

最新评论