OpenCV入门(C++/Python)- 使用OpenCV进行图像旋转和转换(五)

简介: OpenCV入门(C++/Python)- 使用OpenCV进行图像旋转和转换(五)

近年来,随着手机具有这种内置功能,图像编辑变得越来越流行,它可以让您裁剪、旋转图像并对图像进行更多处理。

在这篇文章中,我们将探索和学习这些图像编辑技术。具体来说,我们将学习如何:

  • 旋转图像
  • 转换或移动图像内容

基本图像转换操作


图像的旋转和平移是图像编辑中最基本的操作之一。两者都属于更广泛的仿射变换类别。因此,在研究更复杂的转换之前,您应该首先学习使用OpenCV中可用的函数旋转和平移图像。查看下面的图片,我们将在这里的所有转换示例中使用它。


先看看下面的代码,这些代码将用于使用OpenCV执行图像旋转


Python


import cv2
# Reading the image
image = cv2.imread('image.jpg')
# dividing height and width by 2 to get the center of the image
height, width = image.shape[:2]
# get the center coordinates of the image to create the 2D rotation matrix
center = (width/2, height/2)
# 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))
cv2.imshow('Original image', image)
cv2.imshow('Rotated image', rotated_image)
# wait indefinitely, press any key on keyboard to exit
cv2.waitKey(0)
# save the rotated image to disk
cv2.imwrite('rotated_image.jpg', rotated_image)


C++


#include <iostream>
#include<opencv2/opencv.hpp>
using namespace cv;
int main(int, char**) 
{
    Mat image = imread("image.jpg");
    imshow("image", image);
    waitKey(0);
    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);
    // using getRotationMatrix2D() to get the rotation matrix
    Mat rotation_matix = getRotationMatrix2D(center, angle, 1.0);
    // we will save the resulting image in rotated_image matrix
    Mat rotated_image;
    // rotate the image using warpAffine
    warpAffine(image, rotated_image, rotation_matix, image.size());
    imshow("Rotated image", rotated_image);
    // wait indefinitely, press any key on keyboard to exit
    waitKey(0);
    // save the rotated image to disk
    imwrite("rotated_im.jpg", rotated_image);
    return 0;
}


使用OpenCV的图像旋转


1687312766770.png


其中c x  和c y 是图像旋转所沿的坐标。


OpenCV提供getRotationMatrix2D()函数来创建上述转换矩阵。

以下是创建二维旋转矩阵的语法:


getRotationMatrix2D(center, angle, scale)


getRotationMatrix2D()函数接受以下参数:


  • center:输入图像的旋转中心
  • angle:以度为单位的旋转角度
  • scale:各向同性比例因子,根据提供的值向上或向下缩放图像


如果角度为正,图像将沿逆时针方向旋转。如果要将图像顺时针旋转相同的角度,则角度需要为负值。


旋转图像的三步操作:


  1. 首先,得到旋转中心。及旋转的图像的中心。
  2. 接下来,创建二维旋转矩阵。OpenCV提供了上面讨论的getRotationMatrix2D()函数。
  3. 最后,使用在上一步中创建的旋转矩阵将仿射变换应用于图像。OpenCV中的warpAffine()函数完成此任务。


warpAffine()函数对图像应用仿射变换。在应用仿射变换之后,原始图像中的所有平行线也将在输出图像中保持平行。


warpAffine()的完整语法如下:


warpAffine(src, M, dsize[, dst[, flags[,borderMode[,borderValue]]]])


以下是函数的参数:

  • src:源图像
  • M: 变换矩阵
  • dsize:输出图像的大小 d
  • dst:输出图像
  • flags:插值方法的组合,如INTER_LINEAR或INTER_NEAREST
  • borderMode:像素外推方法
    borderValue:在常量边框的情况下使用的值,默认值为0


下面举一个具体的例子,并尝试使用OpenCV来实现它


Python


import cv2
# Reading the image
image = cv2.imread('image.jpg')
# Dividing height and width by 2 to get the center of the image
height, width = image.shape[:2]
center = (width/2, height/2)


C++


#include "opencv2/opencv.hpp"
using namespace cv;
# Reading the image
Mat image = imread("image.jpg");
// 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);


获得图像中心的像素坐标后,使用函数getRotationMatrix2D()计算旋转矩阵,如下所示。此函数将以下内容作为输入:


旋转所围绕的中心点

旋转角度,以度为单位(正值,对应于逆时针旋转)

调整图像大小的各向同性比例因子。这可以是一个浮点值。例如,值1.0将保持输出图像与源图像的大小相同。值为2.0将使生成的图像的大小是源图像的两倍

该函数返回2D旋转矩阵,该矩阵将在下一步中用于旋转图像。


Python


# the above center is the center of rotation axis
# use cv2.getRotationMatrix2D() to get the rotation matrix
rotate_matrix = cv2.getRotationMatrix2D(center=center, angle=45, scale=1)


C++


// create the rotation matrix using the image center
Mat rotation_matix = getRotationMatrix2D(center, angle=45, 1.0);


现在,使用warpAffine()函数将计算的旋转矩阵应用于图像。它需要三个输入:

  • 源图像
  • 旋转矩阵
  • 输出图像的大小


Python


# Rotate the image using cv2.warpAffine
rotated_image = cv2.warpAffine(src=image, M=rotate_matrix, dsize=(width, height))


C++


// we will save the resulting image in rotated_image matrix
Mat rotated_image;
// apply affine transformation to the original image using the 2D rotation matrix
warpAffine(image, rotated_image, rotation_matix, image.size());


现在,使用imshow()函数显示旋转后的图像。


Python


# visualize the original and the rotated image
cv2.imshow('Original image', image)
cv2.imshow('Rotated image', rotated_image)
# wait indefinitely, press any key on keyboard to exit
cv2.waitKey(0)
# write the output, the rotated image to disk
cv2.imwrite('rotated_image.jpg', rotated_image)


C++



imshow("Rotated image", rotated_image);
waitKey(0);
// save the rotated image to disk
imwrite("rotated_im.jpg", rotated_image);


使用OpenCV转换图像


1687312964945.png


代码如下:

Python


import cv2 
import numpy as np
# read the image 
image = cv2.imread('image.jpg')
# get the width and height of the image
height, width = image.shape[:2]


C++


#include "opencv2/opencv.hpp"
using namespace cv
// read the image 
Mat image = imread("image.jpg");
// get the height and width of the image
int height = image.cols;
int width = image.rows;


创建转换矩阵

Python


# get tx and ty values for translation
# you can specify any value of your choice
tx, ty = width / 4, height / 4
# 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)


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);


在本例中,您将宽度和高度的四分之一作为转换值。

使用warpAffine()函数将平移矩阵应用于图像,与旋转原理相同。

Python


# apply the translation to the image
translated_image = cv2.warpAffine(src=image, M=translation_matrix, dsize=(width, height))


C++


// save the resulting image in translated_image matrix
Mat translated_image;
// apply affine transformation to the original image using the translation matrix
warpAffine(image, translated_image, translation_matrix, image.size());


注意:warpAffine()是一个通用函数,可用于对图像应用任何类型的仿射变换。只需适当地定义矩阵M。


最后,使用imshow()函数显示旋转后的图像。


Python


# display the original and the Translated images
cv2.imshow('Translated image', translated_image)
cv2.imshow('Original image', image)
cv2.waitKey(0)
# save the translated image to disk
cv2.imwrite('translated_image.jpg', translated_image)


C++


//display the original and the Translated images
imshow("Translated image", translated_image);
imshow("Original image", image);
waitKey(0);
// save the translated image to disk
imwrite("translated_image.jpg", translated_image);


相关文章
|
29天前
|
人工智能 算法 计算机视觉
【01】opencv项目实践第一步opencv是什么-opencv项目实践-opencv完整入门以及项目实践介绍-opencv以土壤和水滴分离的项目实践-人工智能AI项目优雅草卓伊凡
【01】opencv项目实践第一步opencv是什么-opencv项目实践-opencv完整入门以及项目实践介绍-opencv以土壤和水滴分离的项目实践-人工智能AI项目优雅草卓伊凡
135 62
【01】opencv项目实践第一步opencv是什么-opencv项目实践-opencv完整入门以及项目实践介绍-opencv以土壤和水滴分离的项目实践-人工智能AI项目优雅草卓伊凡
|
10天前
|
算法 Serverless 数据处理
从集思录可转债数据探秘:Python与C++实现的移动平均算法应用
本文探讨了如何利用移动平均算法分析集思录提供的可转债数据,帮助投资者把握价格趋势。通过Python和C++两种编程语言实现简单移动平均(SMA),展示了数据处理的具体方法。Python代码借助`pandas`库轻松计算5日SMA,而C++代码则通过高效的数据处理展示了SMA的计算过程。集思录平台提供了详尽且及时的可转债数据,助力投资者结合算法与社区讨论,做出更明智的投资决策。掌握这些工具和技术,有助于在复杂多变的金融市场中挖掘更多价值。
39 12
|
4月前
|
计算机视觉
Opencv学习笔记(三):图像二值化函数cv2.threshold函数详解
这篇文章详细介绍了OpenCV库中的图像二值化函数`cv2.threshold`,包括二值化的概念、常见的阈值类型、函数的参数说明以及通过代码实例展示了如何应用该函数进行图像二值化处理,并展示了运行结果。
1010 0
Opencv学习笔记(三):图像二值化函数cv2.threshold函数详解
|
1月前
|
XML 机器学习/深度学习 人工智能
使用 OpenCV 和 Python 轻松实现人脸检测
本文介绍如何使用OpenCV和Python实现人脸检测。首先,确保安装了OpenCV库并加载预训练的Haar特征模型。接着,通过读取图像或视频帧,将其转换为灰度图并使用`detectMultiScale`方法进行人脸检测。检测到的人脸用矩形框标出并显示。优化方法包括调整参数、多尺度检测及使用更先进模型。人脸检测是计算机视觉的基础技术,具有广泛应用前景。
70 10
|
5月前
|
算法 计算机视觉
opencv图像形态学
图像形态学是一种基于数学形态学的图像处理技术,它主要用于分析和修改图像的形状和结构。
82 4
|
1月前
|
Serverless 编译器 C++
【C++面向对象——类的多态性与虚函数】计算图像面积(头歌实践教学平台习题)【合集】
本任务要求设计一个矩形类、圆形类和图形基类,计算并输出相应图形面积。相关知识点包括纯虚函数和抽象类的使用。 **目录:** - 任务描述 - 相关知识 - 纯虚函数 - 特点 - 使用场景 - 作用 - 注意事项 - 相关概念对比 - 抽象类的使用 - 定义与概念 - 使用场景 - 编程要求 - 测试说明 - 通关代码 - 测试结果 **任务概述:** 1. **图形基类(Shape)**:包含纯虚函数 `void PrintArea()`。 2. **矩形类(Rectangle)**:继承 Shape 类,重写 `Print
51 4
|
6月前
|
算法框架/工具 C++ Python
根据相机旋转矩阵求解三个轴的旋转角/欧拉角/姿态角 或 旋转矩阵与欧拉角(Euler Angles)之间的相互转换,以及python和C++代码实现
根据相机旋转矩阵求解三个轴的旋转角/欧拉角/姿态角 或 旋转矩阵与欧拉角(Euler Angles)之间的相互转换,以及python和C++代码实现
513 0
|
5月前
|
存储 计算机视觉
Opencv的基本操作(一)图像的读取显示存储及几何图形的绘制
本文介绍了使用OpenCV进行图像读取、显示和存储的基本操作,以及如何绘制直线、圆形、矩形和文本等几何图形的方法。
Opencv的基本操作(一)图像的读取显示存储及几何图形的绘制
|
4月前
|
Ubuntu Linux 编译器
Linux/Ubuntu下使用VS Code配置C/C++项目环境调用OpenCV
通过以上步骤,您已经成功在Ubuntu系统下的VS Code中配置了C/C++项目环境,并能够调用OpenCV库进行开发。请确保每一步都按照您的系统实际情况进行适当调整。
1058 3
|
4月前
|
C++ Python
探索Python与C/C++混合编程的艺术
探索Python与C/C++混合编程的艺术
96 1

热门文章

最新文章