图像平移是将图像的所有像素坐标进行水平或垂直方向移动,也就是所有像素按照给定的偏移量在水平方向上沿x轴、垂直方向上沿y轴移动。这种操作分为两种,一种是图像大小不改变,这样最后原图像中会有一部分不在图像中。还有一种就是图像大小改变。这样可以保全原图像的内容。其公式如下:

\[ \begin{bmatrix}
x\\
y\\
1
\end{bmatrix} \
=
\begin{bmatrix}
1 & 0 & dx \\
0 & 1 &dy \\
0 & 0 & 1
\end{bmatrix} \
\begin{bmatrix}
x0\\
y0\\
1
\end{bmatrix} \
\]

从实现角度讲,其实就是拷贝原图像中的一部分到新图像中,用OpenCV实现代码如下:

Mat imgTranslate(Mat &matSrc, int xOffset, int yOffset, bool bScale)
{
// 判断是否改变图像大小,并设定被复制ROI
int nRows = matSrc.rows;
int nCols = matSrc.cols;
int nRowsRet = 0;
int nColsRet = 0;
Rect rectSrc;
Rect rectRet;
if (bScale)
{
nRowsRet = nRows + abs(yOffset);
nColsRet = nCols + abs(xOffset);
rectSrc.x = 0;
rectSrc.y = 0;
rectSrc.width = nCols;
rectSrc.height = nRows;
}
else
{
nRowsRet = matSrc.rows;
nColsRet = matSrc.cols;
if (xOffset >= 0)
{
rectSrc.x = 0;
}
else
{
rectSrc.x = abs(xOffset);
}
if (yOffset >= 0)
{
rectSrc.y = 0;
}
else
{
rectSrc.y = abs(yOffset);
}
rectSrc.width = nCols - abs(xOffset);
rectSrc.height = nRows - abs(yOffset);
}
// 修正输出的ROI
if (xOffset >= 0)
{
rectRet.x = xOffset;
}
else
{
rectRet.x = 0;
}
if (yOffset >= 0)
{
rectRet.y = yOffset;
}
else
{
rectRet.y = 0;
}
rectRet.width = rectSrc.width;
rectRet.height = rectSrc.height;
// 复制图像
Mat matRet(nRowsRet, nColsRet, matSrc.type(), Scalar(0));
matSrc(rectSrc).copyTo(matRet(rectRet));
return matRet;
}
... prompt'''

调用代码如下:

#include "opencv2/core/core.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/highgui/highgui.hpp"
#include <iostream>
#include <string> using namespace cv; int main()
{
std::string strPath = "D:\\我的文档\\My Pictures\\OpenCV\\";
Mat matSrc = imread(strPath + "熊猫.jpg");
// 判断是否真确读取数据
if (matSrc.empty())
return 1;
// 平移图像
Mat matScale_0 = imgTranslate(matSrc, 50, 50, false);
Mat matScale_1 = imgTranslate(matSrc, -50, -50, false);
Mat matScale_2= imgTranslate(matSrc, 50, 50, true);
Mat matScale_3 = imgTranslate(matSrc, -50, -50, true); // 保存图像
imwrite(strPath + "0.jpg", matScale_0);
imwrite(strPath + "1.jpg", matScale_1);
imwrite(strPath + "2.jpg", matScale_2);
imwrite(strPath + "3.jpg", matScale_3); // 显示图像
imshow("src", matSrc);
imshow("ret_0", matScale_0);
imshow("ret_1", matScale_1);
imshow("ret_2", matScale_2);
imshow("ret_3", matScale_3); waitKey();
return 0;
}

运行效果如下:

不改变图像大小



改变图像大小

OpenCV图像平移的更多相关文章

  1. OpenCV计算机视觉学习(11)——图像空间几何变换(图像缩放,图像旋转,图像翻转,图像平移,仿射变换,镜像变换)

    如果需要处理的原图及代码,请移步小编的GitHub地址 传送门:请点击我 如果点击有误:https://github.com/LeBron-Jian/ComputerVisionPractice 图像 ...

  2. OpenCV图像金字塔:高斯金字塔、拉普拉斯金字塔与图片尺寸缩放

    这篇已经写得很好,真心给作者点个赞.题目都是直接转过来的,直接去看吧. Reference Link : http://blog.csdn.net/poem_qianmo/article/detail ...

  3. 【OpenCV新手教程之十三】OpenCV图像金字塔:高斯金字塔、拉普拉斯金字塔与图片尺寸缩放

    本系列文章由@浅墨_毛星云 出品,转载请注明出处. 文章链接:http://blog.csdn.net/poem_qianmo/article/details/26157633 作者:毛星云(浅墨) ...

  4. Opencv 图像叠加 添加水印

    Opencv 图像叠加 添加水印 C++: void Mat::copyTo(OutputArray m) const C++: void Mat::copyTo(OutputArray m, Inp ...

  5. opencv图像读取-imread

    前言 图像的读取和保存一定要注意imread函数的各个参数及其意义,尽量不要使用默认参数,否则就像数据格式出现错误(here)一样,很难查找错误原因的: re: 1.opencv图像的读取与保存; 完

  6. 学习 opencv---(12)OpenCV 图像金字塔:高斯金字塔,拉普拉斯金字塔与图片尺寸缩放

    在这篇文章里,我们一起学习下 图像金字塔 的一些基本概念,如何使用OpenCV函数pyrUp和pyrDown 对图像进行向上和向下采样,以及了解专门用于缩放图像尺寸的resize函数的用法.此博文一共 ...

  7. [OpenCV Qt教程] 在Qt图形界面中显示OpenCV图像的OpenGL Widget(第二部分)

    本文译自:http://www.robot-home.it/blog/en/software/tutorial-opencv-qt-opengl-widget-per-visualizzare-imm ...

  8. [OpenCV Qt教程] 在Qt图形界面中显示OpenCV图像的OpenGL Widget (第一部分)

    本文译自:http://www.robot-home.it/blog/en/software/tutorial-opencv-qt-opengl-widget-per-visualizzare-imm ...

  9. 关于OpenCV图像操作的默认参数问题

    本系列文章由 @yhl_leo 出品,转载请注明出处. 文章链接: http://blog.csdn.net/yhl_leo/article/details/51559490 在使用OpenCV以及其 ...

随机推荐

  1. HashMap源码分析四

        HashMap源码在jdk1.8中,改动挺大,里面内容已经变的非常复杂了,后面另起博客分析.jdk1.8以前,HashMap一直是数组加链表的数据结构,在数组的某个下标位置,有多次碰撞,则使用 ...

  2. FFmpeg常用命令学习笔记(三)分解/复用命令

    分解/复用命令 比如文件格式的转换.将封装格式文件中的音频与视频文件分别抽取出来等. 多媒体格式的转换(将MP4文件转成flv格式) ffmpeg -i yan.mp4 -vcodec copy -a ...

  3. c#系统泛型委托

    Action<T> 无返回值的系统泛型委托 namespace ConsoleApp1 { public class UserInfo { public int Id { get; set ...

  4. ERROR 1130 (HY000): Host '127.0.0.1' is not allowed to connect to this MySQL server

    [root@zstedu mysql]# mysql --protocol=tcp -h 127.0.0.1 -uroot -pEnter password: ERROR 1130 (HY000): ...

  5. texture2dArray

    https://medium.com/@calebfaith/how-to-use-texture-arrays-in-unity-a830ae04c98b http://cdn.imgtec.com ...

  6. C3的坑之inline-block

    最近开始复习css一直在踩坑,今天分享一个inline-block 关于inline-block可能很多人都不熟悉,布局这方面很多人用的都是flex或者浮动,flex很强大毋庸置疑的可是关于兼容性就不 ...

  7. toggleClass(class|fn[,sw])

    toggleClass(class|fn[,sw]) 概述 如果存在(不存在)就删除(添加)一个类.直线电机参数   参数 classStringV1.0 CSS类名 class,switchStri ...

  8. 微信小程序填坑之旅(2)-wx.showLoading的时候,仍能点击穿透,造成重复点击button的问题

    解决办法:mask =true wx.showLoading({ title: '正在上传...', mask:true, })

  9. Codeforces 1246D/1225F Tree Factory (构造)

    题目链接 https://codeforces.com/contest/1246/problem/D 题解 首先考虑答案的下界是\(n-1-dep\) (\(dep\)为树的深度,即任何点到根的最大边 ...

  10. shell编程之 ()[] {}

    shell脚本中各种括号的区别以及用法 2018年08月19日 14:55:33 M_QiJunChao 阅读数:273   最近学到了shell脚本编程,觉得脚本中的不同括号有不同的用处,以及有些括 ...