1. 使用OpenCV执行图像旋转
Mat image = imread("test.bmp");
imshow("test", image);
waitKey(0);
double angle;
cout << "请输入要旋转的角度(逆时针):" << endl;
cin >> angle;//输入旋转角度
// 高度和宽度除以2得到图像的中心
Point2f center((image.cols - 1) / 2.0, (image.rows - 1) / 2.0);
// 使用 getRotationMatrix2D() 获取旋转矩阵
Mat rotation_matix = getRotationMatrix2D(center, angle, 1.0);
// 我们将结果图像保存在rotated_image矩阵中
Mat rotated_image;
// 使用warpAffine旋转图像
warpAffine(image, rotated_image, rotation_matix, image.size());
imshow("Rotated image", rotated_image);
// 等待直到按键盘上的任意键退出
waitKey(0);
// 将旋转后的图像保存到磁盘
imwrite("rotated_im.bmp", rotated_image);
2. 使用OpenCV执行图像平移
Mat image = imread("test.bmp");
// 获取图像的宽度和高度
int height = image.cols;
int width = image.rows;
// 获取tx和ty值
float tx = float(width) / 4;;
float ty = float(height) / 4;
// 使用tx和ty创建平移矩阵
float warp_values[] = { 1.0, 0.0, tx, 0.0, 1.0, ty };
Mat translation_matrix = Mat(2, 3, CV_32F, warp_values);
// 将生成的图像保存在translated_image矩阵中
Mat translated_image;
// 利用平移矩阵对原始图像进行仿射变换
warpAffine(image, translated_image, translation_matrix, image.size());
//显示原始和平移图像
imshow("Translated image", translated_image);
imshow("Original image", image);
waitKey(0);
// 将平移后的图像保存到磁盘
imwrite("translated_image.bmp", translated_image);
3. 使用OpenCV执行比例缩放
Mat src = imread("test.bmp", IMREAD_UNCHANGED);
float scaleW = 0.8; // 定义新图像的大小,宽度缩小到80%
float scaleH = 0.8; //定义新图像的大小,高度缩小到80%
cin >> scaleW >> scaleH;
int width = int(src.cols * scaleW);
//定义想要扩大或者缩小后的宽度,src.cols为原图像的宽度,乘以80%则得到想要的大小
int height = int(src.rows * scaleH);
//定义想要扩大或者缩小后的高度,src.cols为原图像的高度,乘以80%则得到想要的大小
Mat dst;
resize(src, dst, Size(width, height));//缩放图像
/*
参数1:原图像
参数2:目标图像
参数3:目标图像的大小
*/
imshow("src", src);
imshow("dst", dst);
waitKey();
完整代码
#include <iostream>
#include<opencv2/opencv.hpp>
using namespace cv;
using namespace std;
int main()
{
cout << "请选择操作:旋转1,平移2,比例缩放3" << endl;
int i;
cin >> i;
switch (i) {
case 1: {
Mat image = imread("test.bmp");
imshow("test", image);
waitKey(0);
double angle;
cout << "请输入要旋转的角度(逆时针):" << endl;
cin >> angle;//输入旋转角度
// 高度和宽度除以2得到图像的中心
Point2f center((image.cols - 1) / 2.0, (image.rows - 1) / 2.0);
// 使用 getRotationMatrix2D() 获取旋转矩阵
Mat rotation_matix = getRotationMatrix2D(center, angle, 1.0);
// 我们将结果图像保存在rotated_image矩阵中
Mat rotated_image;
// 使用warpAffine旋转图像
warpAffine(image, rotated_image, rotation_matix, image.size());
imshow("Rotated image", rotated_image);
// 等待直到按键盘上的任意键退出
waitKey(0);
// 将旋转后的图像保存到磁盘
imwrite("rotated_im.bmp", rotated_image);
break;
}
case 2: {
cout << "请输入tx,ty" << endl;
Mat image = imread("test.bmp");
// 获取图像的宽度和高度
int height = image.cols;
int width = image.rows;
// 获取tx和ty值
float tx;
float ty;
cin >> tx >> ty;
// 使用tx和ty创建平移矩阵
float warp_values[] = { 1.0, 0.0, tx, 0.0, 1.0, ty };
Mat translation_matrix = Mat(2, 3, CV_32F, warp_values);
// 将生成的图像保存在translated_image矩阵中
Mat translated_image;
// 利用平移矩阵对原始图像进行仿射变换
warpAffine(image, translated_image, translation_matrix, image.size());
//显示原始和平移图像
imshow("Translated image", translated_image);
imshow("Original image", image);
waitKey(0);
// 将平移后的图像保存到磁盘
imwrite("translated_image.bmp", translated_image);
break;
}
case 3: {
cout << "请输入缩放比例scaleW,scaleH" << endl;
Mat src = imread("test.bmp", IMREAD_UNCHANGED);
float scaleW;
float scaleH;
cin >> scaleW >> scaleH;
int width = int(src.cols * scaleW);//定义想要扩大或者缩小后的宽度
int height = int(src.rows * scaleH);//定义想要扩大或者缩小后的高度
Mat dst;
resize(src, dst, Size(width, height));//缩放图像
/*
参数1:原图像
参数2:目标图像
参数3:目标图像的大小
*/
imshow("src", src);
imshow("dst", dst);
waitKey();
break; }
}
return 0;
}