一、图像的几何变换
1.1 图像的几何变换
图像的几何变换包括图像的平移、旋转、缩小、放大和镜像映射。
1.2 功能
图像的几何变换改变图像的空间关系,但不改变图像的色彩属性。
1.3 数学关系
图像的几何变换一般定义为:
其中,为输入图像,为输出图像。坐标之间事平移的映射关系。
1.4 主代码
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <iostream>
using namespace std;
using namespace cv;
// 函数声明
void ImgMove(Mat& inputImag_1, Mat& outputImage, int dx, int dy);
int main()
{
// 载入图像并显示
Mat originImage_boy = imread("baby.png", 1);
imshow("original_boy", originImage_boy);
// 创建效果图
Mat resultImage;
resultImage.create(originImage_boy.rows, originImage_boy.cols, originImage_boy.type());
// 记录时间
double timeClock = static_cast<double>(getTickCount());
// 图像平移变换, 可定义和调用不同的函数
ImgMove(originImage_boy, resultImage, 30, 30);
// 输出时间
timeClock = ((double)getTickCount() - timeClock) / getTickCount();
cout << "run time:" << timeClock << "seconds" << endl;
imshow("result_Move",resultImage);
waitKey(0);
return 0;
}
2.1 图像平移变换
图像的平移变换就是在变换前后像素的水平和垂直的坐标的变换。
2.2 图像平移变换的数学关系-矩阵形式表示平移像素前后的关系
(1)平移后图像的大小变化
后向映射:计算平移后的像素在原图像中的坐标
2.2 编程实例-图像平移变换函数
// 使用动态地址计算图像的平移变换-变换后图像的大小变化
void ImgMove(Mat& inputImage_1, Mat& outputImage, int dx, int dy)
{
outputImage = inputImage_1.clone();
int rowsNum = outputImage.rows;
int colsNum = outputImage.cols;
for (int i = 0; i < rowsNum; i++)
{
for (int j = 0; j < colsNum; j++)
{
// 计算像素在原图像中的坐标
int x0 = i - dx;
int y0 = j - dy;
// 映射后的坐标在图像范围内
if ((x0 >= 0) && (x0 < colsNum) && (y0 >= 0) && (y0 < rowsNum))
{
outputImage.at<Vec3b>(i,j) = inputImage_1.at<Vec3b>(x0,y0);
}
else
{
outputImage.at<Vec3b>(i,j) = 255;
}
}
}
}
2.3 图像平移变换结果-图像大小变化
(2)图像平移变换后的大小不变
原理雷同
2.4 编程实例-图像平移变换-图像大小不变
// 使用动态地址计算图像的平移变换-变换后图像的大小不变
void ImgMove(Mat& inputImage_1, Mat& outputImage, int dx, int dy)
{
int rowsNum = outputImage.rows + abs(dx);
int colsNum = outputImage.cols + abs(dy);
outputImage.create(rowsNum, colsNum, inputImage_1.type());
for (int i = 0; i < rowsNum; i++)
{
for (int j = 0; j < colsNum; j++)
{
// 计算像素在原图像中的坐标
int x0 = i - dx;
int y0 = j - dy;
// 映射后的坐标在图像范围内
if ((x0 >= 0) && (x0 < colsNum) && (y0 >= 0) && (y0 < rowsNum))
{
outputImage.at<Vec3b>(i,j) = inputImage_1.at<Vec3b>(x0,y0);
}
else
{
outputImage.at<Vec3b>(i,j) = 255;
}
}
}
}
2.5 图像平移变换-图像大小不变结果