opencv仿射变换
API函數
1、獲取仿射矩陣
(1)getAffineTransform ? ? ? //設置原圖像與目標圖像上三點計算矩陣
Mat A=getAffineTransform(const Point2f* src, const Point2f* dst)
?? ???參數const Point2f* src:原圖的三個固定頂點
?? ???參數const Point2f* dst:目標圖像的三個固定頂點
? ? ? 返回值:Mat型變換矩陣,可直接用于warpAffine()函數
? ? ? 注意,頂點數組長度超過3個,則會自動以前3個為變換頂點;數組可用Point2f[]或Point2f*表示
(2)getRotationMatrix2D ? ? ? //計算二維旋轉變換矩陣
Mat B=getRotationMatrix2D (CvPoint2D32f ?center,double angle,double scale)
? ? ? ?參數CvPoint2D32f ?center,表示源圖像旋轉中心
? ? ? ?參數double angle,旋轉角度,正值表示逆時針旋轉
? ? ? ?參數double scale,縮放系數
2、進行仿射變換
C++ void warpAffine(InputArray src, OutputArray dst, InputArray M, Size dsize, int flags=INTER_LINEAR, int borderMode=BORDER_CONSTANT, const Scalar& borderValue=Scalar())
?? ? ? //參數InputArray src:輸入變換前圖像
?? ??? ?//參數OutputArray dst:輸出變換后圖像,需要初始化一個空矩陣用來保存結果,不用設定矩陣尺寸
?? ??? ?//參數InputArray M:變換矩陣,用另一個函數getAffineTransform()計算
?? ??? ?//參數Size dsize:設置輸出圖像大小
?? ??? ?//參數int flags = INTER_LINEAR:設置插值方式,默認方式為線性插值(另一種WARP_FILL_OUTLIERS)
? ? ? ? //參數int borderMode=BORDER_CONSTANT:邊界像素模式,默認值BORDER_CONSTANT
? ? ? ? //參數const Scalar& borderValue=Scalar(),在恒定邊界情況下取的值,默認值為Scalar(),即0
示例代碼
#include <opencv2/opencv.hpp>
#include <iostream>
using namespace cv;
using namespace std;//全局變量
Mat warpmat(2, 3, CV_32FC1);
Mat ropmat(2, 3, CV_32FC1);
int s11, s12, s21, s22, s31, s32;
int d11, d12, d21, d22, d31, d32;void warp()
{//定義原圖與目標圖像的三組點Point2f src_point[3];Point2f dst_point[3];cout << "請輸入原圖像的第一個點" << endl;cin >> s11;cin >> s12;src_point[0] = Point2f(s11, s12);cout << "請輸入原圖像的第二個點" << endl;cin >> s21;cin >> s22;src_point[1] = Point2f(s21, s22); cout << "請輸入原圖像的第三個點" << endl;cin >> s31;cin >> s32;src_point[2] = Point2f(s31, s32);cout << "原圖像的三個點為:" << endl;cout << src_point[0] << " ";cout << src_point[1] << " ";cout << src_point[2] << endl;cout << "——————————————" << endl;cout << "請輸入目標圖像的第一個點" << endl;cin >> d11;cin >> d12;dst_point[0] = Point2f(d11, d12);cout << "請輸入目標圖像的第二個點" << endl;cin >> d21;cin >> d22;dst_point[1] = Point2f(d21,d22);cout << "請輸入目標圖像的第三個點" << endl;cin >> d31;cin >> d32;dst_point[2] = Point2f(d31, d32);cout << "目標圖像的三個點為:" << endl;cout << dst_point[0] << " ";cout << dst_point[1] << " ";cout << dst_point[2] << endl;cout << "——————————————" << endl;warpmat = getAffineTransform(src_point, dst_point);
}void rot(Mat src)
{//圖像旋轉中心Point center = Point(src.cols / 2, src.rows / 2);//旋轉角度:角度為正表示逆時針旋轉double angle;cout << "旋轉角度為:" << endl;cin >> angle;//縮放系數double scale;cout << "縮放系數為" << endl;cin >> scale;cout << "——————————————" << endl;//得到旋轉矩陣ropmat = getRotationMatrix2D(center, angle, scale);
}int main()
{//載入原圖Mat src = imread("12.bmp");cvtColor(src, src, COLOR_BGR2GRAY);Mat dst=Mat::zeros(src.size(),src.type());Mat dst1 = Mat::zeros(src.size(), src.type());//改變console(控制臺)字體顏色 system("color 0B");//判斷圖片是否讀入if (!src.data){printf("讀取圖片image0錯誤~! \n");return false;}//先顯示原圖namedWindow("【原始圖像】");imshow("【原始圖像】", src);//循環(huán)輪詢按鍵while (1){cout << "\n此圖像的大小為:" << src.size() << endl;cout << "按下任意數字鍵開始程序" << endl;cout << "按下Esc鍵退出程序" << endl;cout << "——————————————" << endl;//獲取鍵盤按鍵int c = waitKey(0);//判斷ESC是否按下,若按下便退出if ((c & 255) == 27){cout << "程序退出!\n";break;}//得到坐標變換矩陣warp();cout << "您是否需要進行旋轉變換" << endl;cout << "【1】:否" << endl;cout << "【2】:是" << endl;cin >> c;if (c == 1){cout << "您正在進行坐標變換圖像操作" << endl;//進行仿射變換warpAffine(src, dst, warpmat, dst.size());imshow("【結果圖】", dst);}else if (c == 2){cout << "您正在進行旋轉圖像操作" << endl;//得到旋轉變換矩陣rot(src);//進行仿射變換warpAffine(src, dst, warpmat, dst.size());warpAffine(dst, dst, ropmat, dst.size());imshow("【旋轉結果圖】", dst);}}return 0;
}
?
總結
以上是生活随笔為你收集整理的opencv仿射变换的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: opencv函数copyto的用法
- 下一篇: 直方图均衡化opencv(彩色、灰度图)