OpenCV图像平移
图像平移是将图像的所有像素坐标进行水平或垂直方向移动,也就是所有像素按照给定的偏移量在水平方向上沿x轴、垂直方向上沿y轴移动。这种操作分为两种,一种是图像大小不改变,这样最后原图像中会有一部分不在图像中。还有一种就是图像大小改变。这样可以保全原图像的内容。其公式如下:
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图像平移的更多相关文章
- OpenCV计算机视觉学习(11)——图像空间几何变换(图像缩放,图像旋转,图像翻转,图像平移,仿射变换,镜像变换)
如果需要处理的原图及代码,请移步小编的GitHub地址 传送门:请点击我 如果点击有误:https://github.com/LeBron-Jian/ComputerVisionPractice 图像 ...
- OpenCV图像金字塔:高斯金字塔、拉普拉斯金字塔与图片尺寸缩放
这篇已经写得很好,真心给作者点个赞.题目都是直接转过来的,直接去看吧. Reference Link : http://blog.csdn.net/poem_qianmo/article/detail ...
- 【OpenCV新手教程之十三】OpenCV图像金字塔:高斯金字塔、拉普拉斯金字塔与图片尺寸缩放
本系列文章由@浅墨_毛星云 出品,转载请注明出处. 文章链接:http://blog.csdn.net/poem_qianmo/article/details/26157633 作者:毛星云(浅墨) ...
- Opencv 图像叠加 添加水印
Opencv 图像叠加 添加水印 C++: void Mat::copyTo(OutputArray m) const C++: void Mat::copyTo(OutputArray m, Inp ...
- opencv图像读取-imread
前言 图像的读取和保存一定要注意imread函数的各个参数及其意义,尽量不要使用默认参数,否则就像数据格式出现错误(here)一样,很难查找错误原因的: re: 1.opencv图像的读取与保存; 完
- 学习 opencv---(12)OpenCV 图像金字塔:高斯金字塔,拉普拉斯金字塔与图片尺寸缩放
在这篇文章里,我们一起学习下 图像金字塔 的一些基本概念,如何使用OpenCV函数pyrUp和pyrDown 对图像进行向上和向下采样,以及了解专门用于缩放图像尺寸的resize函数的用法.此博文一共 ...
- [OpenCV Qt教程] 在Qt图形界面中显示OpenCV图像的OpenGL Widget(第二部分)
本文译自:http://www.robot-home.it/blog/en/software/tutorial-opencv-qt-opengl-widget-per-visualizzare-imm ...
- [OpenCV Qt教程] 在Qt图形界面中显示OpenCV图像的OpenGL Widget (第一部分)
本文译自:http://www.robot-home.it/blog/en/software/tutorial-opencv-qt-opengl-widget-per-visualizzare-imm ...
- 关于OpenCV图像操作的默认参数问题
本系列文章由 @yhl_leo 出品,转载请注明出处. 文章链接: http://blog.csdn.net/yhl_leo/article/details/51559490 在使用OpenCV以及其 ...
随机推荐
- Modbus通讯协议
<ignore_js_op> O1CN01P1wxTI1dCdw5nAeMO_!!85243700.jpg (287.43 KB, 下载次数: 0) 下载附件 保存到相册 2019-6- ...
- socket 在Unix domain的使用
server.c #include <stdio.h> #include <sys/types.h> #include <sys/socket.h> #includ ...
- [AngularJS] Decorator pattern for code reuse
Imaging you have a large application, inside this large application you have many small individual a ...
- .net core 反编译一小段
public static class A { private static readonly MethodInfo GetServiceInfo; public static IApplicatio ...
- virtualBox中有线和无线两种情况下centos虚拟机和本地机互ping的方案
之前写微信点餐系统的时候,刚开始是无线连接,然后每次进去虚拟机ip和本地ip都会改变,所以每次都需要配置一下nginx,还有本地的路径.之后换有线连接,就研究了一下桥接模式有线情况下虚拟机静态ip设置 ...
- Selenium常见异常分析及解决方案
pycharm中导入selenium报错 现象: pycharm中输入from selenium import webdriver, selenium标红 原因1: pycharm使用的虚拟环境中没有 ...
- UVA 11174 Stand in a Line,UVA 1436 Counting heaps —— (组合数的好题)
这两个题的模型是有n个人,有若干的关系表示谁是谁的父亲,让他们进行排队,且父亲必须排在儿子前面(不一定相邻).求排列数. 我们假设s[i]是i这个节点,他们一家子的总个数(或者换句话说,等于他的子孙数 ...
- linux下编译利用CMakeLists.txt 编译C++写的opencv程序
https://hihozhou.com/blog/2017/05/11/linux-compile-opencv-c++-file.html cmake . make -j8
- 0ctf-ezdoor-复现分析
在学习opcache的时候,看到了这个题目,刚好有环境,就来复现一下,这个题目也让我学到了很多. 创建镜像: docker build -t 0ctf-ezdoor . 启动容器: docker ru ...
- Nslookup: command not found error on RHEL/CentOS 7
Reference: https://unix.stackexchange.com/questions/164210/nslookup-command-not-found-error-on-rhel- ...