OpenCV圖像幾何變換之透視變換
本文實(shí)例為大家分享了OpenCV圖像幾何變換之透視變換的具體代碼,供大家參考,具體內(nèi)容如下
1. 基本原理
透視變換(Perspective Transformation)的本質(zhì)是將圖像投影到一個新的視平面,其通用變換公式為:
(u,v)為原始圖像像素坐標(biāo),(x=x'/w',y=y'/w')為變換之后的圖像像素坐標(biāo)。透視變換矩陣圖解如下:
仿射變換(Affine Transformation)可以理解為透視變換的特殊形式。透視變換的數(shù)學(xué)表達(dá)式為:
所以,給定透視變換對應(yīng)的四對像素點(diǎn)坐標(biāo),即可求得透視變換矩陣;反之,給定透視變換矩陣,即可對圖像或像素點(diǎn)坐標(biāo)完成透視變換,如下圖所示:
2. OpenCV透視變換函數(shù)
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 (dstsrc). // 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; }
原始圖像及其透視變換結(jié)果:
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持我們。
上一篇:如何實(shí)現(xiàn)socket網(wǎng)絡(luò)編程的多線程
欄 目:C語言
下一篇:OpenCV實(shí)現(xiàn)馬賽克和毛玻璃濾鏡特效
本文標(biāo)題:OpenCV圖像幾何變換之透視變換
本文地址:http://mengdiqiu.com.cn/a1/Cyuyan/299.html
您可能感興趣的文章
- 01-10實(shí)現(xiàn)opencv圖像裁剪分屏顯示示例
- 01-10使用opencv拉伸圖像擴(kuò)大分辨率示例
- 01-10C++基于Directx MMX實(shí)現(xiàn)的圖像灰度轉(zhuǎn)換代碼
- 01-10C++將CBitmap類中的圖像保存到文件的方法
- 01-10VC++中圖像處理類CBitmap的用法
- 01-10基于C++實(shí)現(xiàn)kinect+opencv 獲取深度及彩色數(shù)據(jù)
- 01-10c++圖像處理:24位真彩圖顏色變換實(shí)例
- 01-10淺談CMake配置OpenCV 時靜態(tài)鏈接與動態(tài)鏈接的選擇
- 01-10C++ HLSL實(shí)現(xiàn)簡單的圖像處理功能
- 01-10C++函數(shù)pyrUp和pyrDown來實(shí)現(xiàn)圖像金字塔功能


閱讀排行
本欄相關(guān)
- 04-02c語言函數(shù)調(diào)用后清空內(nèi)存 c語言調(diào)用
- 04-02func函數(shù)+在C語言 func函數(shù)在c語言中
- 04-02c語言的正則匹配函數(shù) c語言正則表達(dá)
- 04-02c語言用函數(shù)寫分段 用c語言表示分段
- 04-02c語言中對數(shù)函數(shù)的表達(dá)式 c語言中對
- 04-02c語言編寫函數(shù)冒泡排序 c語言冒泡排
- 04-02c語言沒有round函數(shù) round c語言
- 04-02c語言分段函數(shù)怎么求 用c語言求分段
- 04-02C語言中怎么打出三角函數(shù) c語言中怎
- 04-02c語言調(diào)用函數(shù)求fibo C語言調(diào)用函數(shù)求
隨機(jī)閱讀
- 01-10SublimeText編譯C開發(fā)環(huán)境設(shè)置
- 08-05織夢dedecms什么時候用欄目交叉功能?
- 01-11ajax實(shí)現(xiàn)頁面的局部加載
- 04-02jquery與jsp,用jquery
- 08-05DEDE織夢data目錄下的sessions文件夾有什
- 01-10delphi制作wav文件的方法
- 01-10C#中split用法實(shí)例總結(jié)
- 01-10使用C語言求解撲克牌的順子及n個骰子
- 08-05dedecms(織夢)副欄目數(shù)量限制代碼修改
- 01-11Mac OSX 打開原生自帶讀寫NTFS功能(圖文