ubuntu22.04@laptop OpenCV Get Started: 005_rotate_and_translate_image

2024-02-09 08:28

本文主要是介绍ubuntu22.04@laptop OpenCV Get Started: 005_rotate_and_translate_image,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

ubuntu22.04@laptop OpenCV Get Started: 005_rotate_and_translate_image

  • 1. 源由
  • 2. translate/rotate应用Demo
  • 3 translate_image
    • 3.1 C++应用Demo
    • 3.2 Python应用Demo
    • 3.3 平移图像过程
  • 4. rotate_image
    • 4.1 C++应用Demo
    • 4.2 Python应用Demo
    • 4.3 旋转图像过程
  • 5. 总结
  • 6. 参考资料

1. 源由

图像的平移和旋转是图像编辑中最基本的操作之一。两者都属于广义仿射变换的范畴。

因此,在研究更复杂的变换之前,首先学习使用OpenCV中提供的函数旋转和平移图像。

2. translate/rotate应用Demo

005_rotate_and_translate_image是OpenCV平移和旋转的示例程序。

确认OpenCV安装路径:

$ find /home/daniel/ -name "OpenCVConfig.cmake"
/home/daniel/OpenCV/installation/opencv-4.9.0/lib/cmake/opencv4/
/home/daniel/OpenCV/opencv/build/OpenCVConfig.cmake
/home/daniel/OpenCV/opencv/build/unix-install/OpenCVConfig.cmake$ export OpenCV_DIR=/home/daniel/OpenCV/installation/opencv-4.9.0/lib/cmake/opencv4/

3 translate_image

3.1 C++应用Demo

C++应用Demo工程结构:

005_rotate_and_translate_image/CPP$ tree .
.
└── translate_image├── CMakeLists.txt├── image.jpg└── translate_image.cpp2 directories, 6 files

C++应用Demo工程编译执行:

$ cd translate_image
$ mkdir build
$ cd build
$ cmake ..
$ cmake --build . --config Release
$ cd ..
$ ./build/translate_image

3.2 Python应用Demo

Python应用Demo工程结构:

005_rotate_and_translate_image/Python$ tree .
.
├── image.jpg
├── image_translation.py
├── requirements.txt
└── rotate_image.py0 directories, 4 files

Python应用Demo工程执行:

$ workoncv-4.9.0
$ python image_translation.py

3.3 平移图像过程

引入平移矩阵对图像进行移动:

  • t x t_x tx: X方向,正数向右移动;反之向左移动。
  • t y t_y ty: Y方向,正数向下移动;反之向上移动。

在这里插入图片描述

C++:

// get tx and ty values for translation
float tx = float(width) / 4;
float ty = float(height) / 4;
// create the translation matrix using tx and ty
float warp_values[] = { 1.0, 0.0, tx, 0.0, 1.0, ty };
Mat translation_matrix = Mat(2, 3, CV_32F, warp_values);// we will save the resulting image in translated_image matrix
Mat translated_image;
// apply affine transformation to the original image using translation matrix
warpAffine(image, translated_image, translation_matrix, image.size());

Python:

# get tx and ty values for translation
tx, ty = width / 4, height / 4 # you divide by value of your choice
# create the translation matrix using tx and ty, it is a NumPy array 
translation_matrix = np.array([[1, 0, tx],[0, 1, ty]
], dtype=np.float32)# apply the translation to the image
translated_image = cv2.warpAffine(src=image, M=translation_matrix, dsize=(width, height)
)

4. rotate_image

4.1 C++应用Demo

C++应用Demo工程结构:

005_rotate_and_translate_image/CPP$ tree .
.
└── rotate_image├── CMakeLists.txt├── image.jpg└── rotate_image.cpp2 directories, 6 files

C++应用Demo工程编译执行:

$ cd rotate_image
$ mkdir build
$ cd build
$ cmake ..
$ cmake --build . --config Release
$ cd ..
$ ./build/rotate_image

4.2 Python应用Demo

Python应用Demo工程结构:

005_rotate_and_translate_image/Python$ tree .
.
├── image.jpg
├── image_translation.py
├── requirements.txt
└── rotate_image.py0 directories, 4 files

Python应用Demo工程执行:

$ workoncv-4.9.0
$ python rotate_image.py

4.3 旋转图像过程

沿旋转中心点进行角度旋转,采用线性代数矩阵运算:

  • 旋转中心点:采用了图片的重心
  • 旋转角度:示例为45度

在这里插入图片描述

C++:

double angle = 45;// get the center coordinates of the image to create the 2D rotation matrix
Point2f center((image.cols - 1) / 2.0, (image.rows - 1) / 2.0);
// create the rotation matrix using the image center
Mat rotation_matix = getRotationMatrix2D(center, angle, 1.0);// we will save the resulting image in rotated_image matrix
Mat rotated_image;
// apply affine transformation to the original image using the 2D rotaiton matrix
warpAffine(image, rotated_image, rotation_matix, image.size());

Python:

# dividing height and width by 2 to get the center of the image
height, width = image.shape[:2]
center = (width/2, height/2)# the above center is the center of rotation axis
# using cv2.getRotationMatrix2D() to get the rotation matrix
rotate_matrix = cv2.getRotationMatrix2D(center=center, angle=45, scale=1)# rotate the image using cv2.warpAffine
rotated_image = cv2.warpAffine(src=image, M=rotate_matrix, dsize=(width, height))

5. 总结

通过对NumPy二维数组操作,对图像进行旋转和移动。

  • getRotationMatrix2D(center, angle, scale)
  • center: 旋转中心点
  • angle: 旋转角度
  • scale: 缩放尺寸
  • warpAffine(src, M, dsize[, dst[, flags[, borderMode[, borderValue]]]])
  • src: 源图像数组
  • M: 转换矩阵
  • dsize: 输出图像尺寸
  • dst: 输出图像
  • flags: 插值方法, INTER_LINEAR or INTER_NEAREST
  • borderMode: 像素外推方法
  • borderValue: 在恒定边界的情况下使用的值,默认值为0

6. 参考资料

【1】ubuntu22.04@laptop OpenCV Get Started
【2】ubuntu22.04@laptop OpenCV安装
【3】ubuntu22.04@laptop OpenCV定制化安装

这篇关于ubuntu22.04@laptop OpenCV Get Started: 005_rotate_and_translate_image的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/693694

相关文章

vcpkg安装opencv中的特殊问题记录(无法找到opencv_corexd.dll)

我是按照网上的vcpkg安装opencv方法进行的(比如这篇:从0开始在visual studio上安装opencv(超详细,针对小白)),但是中间出现了一些别人没有遇到的问题,虽然原因没有找到,但是本人给出一些暂时的解决办法: 问题1: 我在安装库命令行使用的是 .\vcpkg.exe install opencv 我的电脑是x64,vcpkg在这条命令后默认下载的也是opencv2:x6

brew install opencv@2 时报错 Error: Can't create update lock in /usr/local/var/homebrew/locks!

解决方案,报错里已经说明了: 我的解决方案: sudo chown -R "$USER":admin /usr/local   stackoverflow上的答案 I was able to solve the problem by using chown on the folder: sudo chown -R "$USER":admin /usr/local Also you'

[vivado]translate_off\on

答疑帖: 1)https://forums.xilinx.com/t5/Synthesis/Question-about-synthesis-translate-on-and-translate-off/td-p/658790

BD错误集锦3——ERROR: Can't get master address from ZooKeeper; znode data == null

hbase集群没启动,傻子!   启动集群 [s233 s234 s235]启动zk集群 $>zkServer.sh start $>zkServer.sh status   [s233] 启动dfs系统 $>start-dfs.sh 如果s237 namenode启动失败,则 [s237] $>hadoop-daemon.sh start namenode [s233]启动yarn集群

[分布式网络通讯框架]----Zookeeper客户端基本操作----ls、get、create、set、delete

Zookeeper数据结构 zk客户端常用命令 进入客户端 在bin目录下输入./zkCli.sh 查看根目录下数据ls / 注意:要查看哪一个节点,必须把路径写全 查看节点数据信息 get /第一行代码数据,没有的话表示没有数据 创建节点create /sl 20 /sl为节点的路径,20为节点的数据 注意,不能跨越创建,也就是说,创建sl2的时候,必须确保sl

《学习OpenCV》课后习题解答7

题目:(P105) 创建一个结构,结构中包含一个整数,一个CvPoint和一个 CvRect;称结构体为“my_struct”。 a. 写两个函数:void Write_my_strct(CvFileStorage* fs, const char * name, my_struct* ms) 和 void read_my_struct(CvFileStorage* fs, CvFileNode

OpenCV中的按钮问题

在HighGUI中,没有显示提供任何形式的按钮。一般有两种方法替代: 1.用只有两个状态的滑动条来替代按钮。开关(switch)事实上就是只有两个状态的滑动条,这两个状态是on和off。然后通过回调函数来实现相关的功能。 实例源码(使用滑动条实现一个开关功能) #include<cv.h>#include<highgui.h>int g_switch_value = 0;void swit

《学习OpenCV》课后习题解答6

题目:(P104) 使用cvCmp()创建一个掩码。加载一个真实的图像。使用cvsplit()将图像分割成红,绿,蓝三个单通道图像。 a.找到并显示绿图。 b.克隆这个绿图两次(分别命名为clone1和clone2)。 c.求出这个绿色平面的最大值和最小值。 d.将clone1的所有元素赋值为theash=(unsigned char)((最大值-最小值)/2.0)。 e.将clone

《学习OpenCV》课后习题解答5

题目:(P104) 为一个图像创建多个图像头。读取一个大小至少为100*100的图像。另创建两个图像头并设置它们的origion,depth,nChannels和widthStep属性同之前读取的图像一样。在新的图像头中,设置宽度为20,高度为30.最后,将imageData指针分别指向像素(5,10)和(50,60)像素位置。传递这两个新的图像头给cvNot()。最后显示最初读取的图像,在那个

《学习OpenCV》课后习题解答3

题目:(P104) 创建一个大小为100*100的三通道RGB图像。将它的元素全部置0.使用指针算法以(20,5)与(40,20)为项点绘制一个绿色平面。 解答: #include "cv.h" #include "highgui.h" int main(int argc, char** argv) {IplImage* img = cvCreateImage(cvSize(100,