您好,登錄后才能下訂單哦!
這篇文章主要為大家展示了“OpenCV圖像幾何變換之透視變換的示例分析”,內容簡而易懂,條理清晰,希望能夠幫助大家解決疑惑,下面讓小編帶領大家一起研究并學習一下“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; }
原始圖像及其透視變換結果:
以上是“OpenCV圖像幾何變換之透視變換的示例分析”這篇文章的所有內容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內容對大家有所幫助,如果還想學習更多知識,歡迎關注億速云行業資訊頻道!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。