OpenCV图像仿射变换
OpenCV圖像的旋轉(zhuǎn)是通過圖像的仿射變換來實現(xiàn)的,實現(xiàn)圖像的旋轉(zhuǎn),分為三個步驟:
第一步:確定旋轉(zhuǎn)角度和旋轉(zhuǎn)中心。
第二步:確定旋轉(zhuǎn)矩陣。通過getRotationMatrix2D函數(shù)計算出。
第三步:通過仿射變換實現(xiàn)旋轉(zhuǎn)。通過warpAffine函數(shù)實現(xiàn)。
一、getRotationMatrix2D函數(shù)
原型
CV_EXPORTS_W Mat getRotationMatrix2D(Point2f center, double angle, double scale);功能:計算一個二維旋轉(zhuǎn)的仿射矩陣。
函數(shù)計算下面的矩陣:
[[αβ(1?α)?center.x?β?center.y?βαβ?center.x+(1?α)?center.y]][\begin{bmatrix} \alpha & \beta & (1- \alpha ) \cdot \texttt{center.x} - \beta \cdot \texttt{center.y} \\ - \beta & \alpha & \beta \cdot \texttt{center.x} + (1- \alpha ) \cdot \texttt{center.y} \end{bmatrix}] [[α?β?βα?(1?α)?center.x?β?center.yβ?center.x+(1?α)?center.y?]]
此處:
[α=scale?cos?angle,β=scale?sin?angle][\begin{array}{l} \alpha = \texttt{scale} \cdot \cos \texttt{angle} , \\ \beta = \texttt{scale} \cdot \sin \texttt{angle} \end{array}] [α=scale?cosangle,β=scale?sinangle?]
變換映射旋轉(zhuǎn)中心是到中心自己本身,如果這個不是目標(biāo),調(diào)整偏移量。
參數(shù)釋義
- 參數(shù) center 源圖像中的旋轉(zhuǎn)中心
- 參數(shù) angle 讀書表示的旋轉(zhuǎn)角度.正值表示逆時針旋轉(zhuǎn) (坐標(biāo)原點假定在左上角).
- 參數(shù) scale 兩個軸的各向同性比例因子。1表示不縮放。
參考 getAffineTransform, warpAffine, transform
二、warpAffine函數(shù)
原型:
CV_EXPORTS_W void warpAffine( InputArray src, OutputArray dst,InputArray M, Size dsize,int flags = INTER_LINEAR,int borderMode = BORDER_CONSTANT,const Scalar& borderValue = Scalar());功能:對一個圖像實現(xiàn)一個仿射變換
當(dāng)設(shè)置標(biāo)志W(wǎng)ARP_INVERSE_MAP 的時候,函數(shù)warpAffine變換使用以下的矩陣來實現(xiàn)一個仿射變換:
[dst(x,y)=src(M11x+M12y+M13,M21x+M22y+M23)][\texttt{dst} (x,y) = \texttt{src} ( \texttt{M} _{11} x + \texttt{M} _{12} y + \texttt{M} _{13}, \texttt{M} _{21} x + \texttt{M} _{22} y + \texttt{M} _{23})] [dst(x,y)=src(M11?x+M12?y+M13?,M21?x+M22?y+M23?)]
否則,轉(zhuǎn)換首先使用invertAffineTransform進(jìn)行倒置,然后在上面的公式中代替M,函數(shù)不能就地轉(zhuǎn)換。
參數(shù)釋義
- 參數(shù) src 輸入圖像.
- 參數(shù) dst 輸出圖像,大小是dszie,類型和src相同 。
- 參數(shù) M 2*3的變換矩陣。
- 參數(shù) dsize 輸出圖像大小。
- 參數(shù) flags 插值方法的組合 (參見InterpolationFlags) 和可選的標(biāo)志 WARP_INVERSE_MAP 意味著M是逆變換.
- 參數(shù) borderMode 像素外推法 (參見 BorderTypes); 當(dāng)
borderMode=#BORDER_TRANSPARENT, 意味著目標(biāo)圖像的像素對應(yīng)于原圖像中的“異常值”,并且不能被函數(shù)修改。 - 參數(shù) borderValue 固定邊緣情況下的值,默認(rèn)是0。
參見warpPerspective, resize, remap, getRectSubPix, transform
三、源碼示例
1.旋轉(zhuǎn)
#include <opencv2/opencv.hpp> #include <iostream>using namespace cv; using namespace std;int main() {Mat src = imread("D:\\OpenCVtest\\images\\juice.png", 1);if (src.empty()){cout << "could not load image..." << endl;return -1;}double rotate_angle = 15; //設(shè)置旋轉(zhuǎn)的角度Point2f rotate_center(src.rows / 2, src.cols / 2); // 設(shè)置旋轉(zhuǎn)中心Mat rotate_matrix = getRotationMatrix2D(rotate_center, rotate_angle, 1); //計算旋轉(zhuǎn)矩陣Size dstSize(src.cols, src.rows);Mat image_dst;warpAffine(src, image_dst, rotate_matrix, dstSize ); // 仿射變換imshow("源圖像", src);imshow("仿射變換0", image_dst);waitKey();return 0; }運行結(jié)果:
2.三點映射
#include <opencv2/opencv.hpp> #include <iostream>using namespace cv; using namespace std;int main() {Mat src = imread("D:\\OpenCVtest\\images\\juice.png", 1);if (src.empty()){cout << "could not load image..." << endl;return -1;}Size dstSize(src.cols, src.rows);// 定義三個點Point2f srcPoints[3];Point2f dstPoints[3];srcPoints[0] = Point2f(0, 0);srcPoints[1] = Point2f(0, (float)(src.cols-1));srcPoints[2] = Point2f(float(src.rows-1), (float)(src.cols - 1));// 變換后的三個點dstPoints[0] = Point2f((float)(src.rows)*0.15, (float)(src.cols)*0.25);dstPoints[1] = Point2f((float)(src.rows) * 0.25, (float)(src.cols) * 0.65);dstPoints[2] = Point2f((float)(src.rows) * 0.70, (float)(src.cols) * 0.70);// 根據(jù)對應(yīng)點求仿射矩陣Mat rotate_matrx_1 = getAffineTransform(srcPoints, dstPoints);Mat image_warp;warpAffine(src, image_warp, rotate_matrx_1, dstSize);imshow("源圖像", src);imshow("三點映射仿射變換", image_warp);waitKey();return 0; }運行結(jié)果:
總結(jié)
以上是生活随笔為你收集整理的OpenCV图像仿射变换的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 对cv::findHomography未
- 下一篇: fatal error: alsa/as