opencv学习04图像的平移

图像的平移

#include<iostream>
#include<opencv2/opencv.hpp>
#include<math.h>
using namespace cv;
using namespace std;
void imagetransform1(Mat& image1,Mat &image2,int xoffset,int yoffset);//图像大小不改变
Mat imagetransform2(Mat& image1,int xoffset,int yoffset);//图像大小改变
int main()
{
	Mat image;
	image = imread("C:/Users/MECHREVO/Desktop/11.jpeg");//读入数据
	if (image.empty())
	{
		cout << "未能成功读取文件" << endl;
		return 0;
	}
	Mat resultimage;
	resultimage=imagetransform2(image,10,100);
	namedWindow("result", CV_WINDOW_NORMAL);
	namedWindow("resource", CV_WINDOW_NORMAL);
	imshow("result", resultimage);
	imshow("resource", image);
	waitKey(0);
	return 0;
}
void imagetransform1(Mat& image1, Mat& image2, int xoffset, int yoffset)
{
	int rows = image1.rows;
	int cols = image1.cols;
	for (int i = 0; i < rows; i++)
	{
		for (int j = 0; j < cols; j++)
		{
			//映射变换
			int x = j - xoffset;
			int y = i - yoffset;
			//边界判断
			if (x >= 00 && y >= 0 && x < cols && y < rows)
			{
				image2.at<Vec3b>(i, j) = image1.ptr<Vec3b>(y)[x];
			}
		}
	}
}
Mat imagetransform2(Mat& image1,int xoffset,int yoffset)
{
	int rows = image1.rows+abs(yoffset);
	int cols = image1.cols+abs(xoffset);
	Mat image2(rows, cols, image1.type());
	for (int i = 0; i < rows;++i)
	{
		for (int j = 0; j < cols; ++j)
		{
			//映射变换
			int x = j - xoffset;
			int y = i - yoffset;
			//边界判断
			if (x >= 00 && y >= 0 && x < cols && y < rows)
			{
				image2.at<Vec3b>(i, j) = image1.ptr<Vec3b>(y)[x];
			}
		}
	}
	return image2;
}
上一篇:4. 像素运算


下一篇:opencv中Mat究竟是什么?