ubuntu22.04@laptop OpenCV Get Started: 005_rotate_and_translate_image

ubuntu22.04@laptop OpenCV Get Started: 005_rotate_and_translate_image

  • [1. 源由](#1. 源由)
  • [2. translate/rotate应用Demo](#2. translate/rotate应用Demo)
  • [3 translate_image](#3 translate_image)
    • [3.1 C++应用Demo](#3.1 C++应用Demo)
    • [3.2 Python应用Demo](#3.2 Python应用Demo)
    • [3.3 平移图像过程](#3.3 平移图像过程)
  • [4. rotate_image](#4. rotate_image)
    • [4.1 C++应用Demo](#4.1 C++应用Demo)
    • [4.2 Python应用Demo](#4.2 Python应用Demo)
    • [4.3 旋转图像过程](#4.3 旋转图像过程)
  • [5. 总结](#5. 总结)
  • [6. 参考资料](#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.cpp

2 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.py

0 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.cpp

2 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.py

0 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二维数组操作,对图像进行旋转和移动。

  • center: 旋转中心点
  • angle: 旋转角度
  • scale: 缩放尺寸
  • 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定制化安装

相关推荐
小蒋观天下2 分钟前
【安防AI工程实战二】AI摄像头帧率抖动根治:ISP曝光时序与AI推理时序冲突深度定位
人工智能·深度学习·算法·安全·机器学习·ai大模型
前端.火鸡6 分钟前
大模型概念地图:所有术语分别站在哪一层
人工智能
打工仔折腾 AI6 分钟前
鸿蒙平板远控电脑实测:ToDesk蓝牙键鼠、双模式鼠标与跨端剪贴板落地记录
人工智能·后端·python·性能优化·计算机外设·电脑·harmonyos
云和数据.ChenGuang13 分钟前
trace 是RAG检索链路的**全链路追踪日志对象
人工智能·深度学习·算法·排序算法
鼓掌MVP14 分钟前
决定全案的智能Agent
大数据·人工智能·机器学习
lank_M14 分钟前
Word裁剪图片转HTML走样,该在渲染层修还是入口修
前端·图像处理·人工智能·计算机视觉·html·word
IT_陈寒15 分钟前
深入理解Redis:核心原理与最佳实践
前端·人工智能·后端
来包番茄沙司20 分钟前
【网盘资源分享】2026新版尚硅谷AI Agent智能体极速版|全套课程资料+面经
人工智能·学习
拉你进_教堂29 分钟前
小龙虾技能之【Contactless Health Risk Screening Tool | 非接触式健康风险检测分析工具】简介
人工智能·目标检测·计算机视觉·多模态大模型·openclaw小龙虾技能
梦想的初衷~31 分钟前
从通量数据到SCI论文——AI-R语言生态系统碳水热通量及其机制分析与论文实战
人工智能·机器学习·r语言·r 语言·fluxnet·涡度·生态系统碳通量