#include<opencv2\imgproc\imgproc.hpp>
#include<opencv2\highgui\highgui.hpp>
#include<stdio.h>
using namespace cv;
void pingyi(Mat img, Mat &dst, int x, int y);
int main() {
Mat img = imread("H:\\city.jpg");//图片读取
Mat dst = img.clone();//将img储存到dst
namedWindow("001", 500);//创建窗口
namedWindow("002", 500);
int x, y;//x,y为平移距离
printf("输入x:");
scanf("%d", &x);//输入x
printf("输入y:");
scanf("%d", &y);//输入y
pingyi(img, dst, x, y);//平移函数
imshow("001", img);//输出图片
imshow("002", dst);
waitKey(0);//按任意键退出
return 0;
}
void pingyi(Mat img, Mat &dst, int x, int y) {
int maxrows = img.rows;//用来储存图像的长和宽
int maxcols = img.cols;
for (int i = 0; i < maxrows; i++) {
for (int j = 0; j < maxcols; j++) {
//判断平移位置后赋值给新的图像
if (i - x > 0 && i - x < maxrows&&j - y>0 && j - y < maxcols) {
dst.at<Vec3b>(i, j) = img.at<Vec3b>(i - x, j - y);
}
//将平移后的区域变成黑色
else {
//Vec3b储存彩色图,uchar储存灰色图
dst.at<Vec3b>(i, j)[0] = 0;
dst.at<Vec3b>(i, j)[1] = 0;
dst.at<Vec3b>(i, j)[2] = 0;
}
}
}
}
图像的平移
最新推荐文章于 2023-05-06 13:58:36 发布