(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#include 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); //循环轮询按键 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; }



