注:不定期更新中
平台:windows + vs2017
平移
平移分为图像大小不变、大小变化两种
平移后图像大小不变
#include "pch.h"
#include <iostream>
#include <highgui.h>
using namespace std;
using namespace cv;
//平移后图像的大小不变
void moveImage(Mat const& src, Mat& dst, int dx, int dy)
{
CV_Assert(src.depth() == CV_8U);
const int rows = src.rows;
const int cols = src.cols;
dst.create(rows, cols, src.type());
Vec3b *p;
for (int i = 0; i < rows; i++)
{
p = dst.ptr<Vec3b>(i);
for (int j = 0; j < cols; j++)
{
int x = j - dx; //平移后坐标映射到原图像
int y = i - dy;
if (x >= 0 && y >= 0 && x < cols && y < rows) //保证映射后的坐标在原图像范围内
{
p[j] = src.ptr<Vec3b>(y)[x];
}
}
}
}
int main()
{
Mat src = imread("1.png");
Mat dst;
moveImage(src, dst, 50, 50);
namedWindow("原图");
namedWindow("平移不改变图片大小");
imshow("平移不改变图片大小", dst);
imshow("原图", src);
waitKey(0);
}
平移后图像大小改变
#include "pch.h"
#include <iostream>
#include <highgui.h>
using namespace std;
using namespace cv;
//平移后图像的大小变化
void moveImage(Mat const& src, Mat& dst, int dx, int dy)
{
CV_Assert(src.depth() == CV_8U);
const int rows = src.rows + abs(dy); //输出图像的大小
const int cols = src.cols + abs(dx);
dst.create(rows, cols, src.type());
Vec3b *p;
for (int i = 0; i < rows; i++)
{
p = dst.ptr<Vec3b>(i);
for (int j = 0; j < cols; j++)
{
int x = j - dx; //平移后坐标映射到原图像
int y = i - dy;
if (x >= 0 && y >= 0 && x < src.cols && y < src.rows) //保证映射后的坐标在原图像范围内
{
p[j] = src.ptr<Vec3b>(y)[x];
}
}
}
}
int main()
{
Mat src = imread("1.png");
Mat dst;
moveImage(src, dst, 50, 50);
namedWindow("原图");
namedWindow("平移改变图片大小");
imshow("平移改变图片大小", dst);
imshow("原图", src);
waitKey(0);
}
平移图片大小是否变化是相对于图像框的整个区域,而不单纯是图片框内的图片大小区域