Opencv 基础(四):使用OpenCV进行图像旋转和平移 您所在的位置:网站首页 函数图像翻转问题有哪些 Opencv 基础(四):使用OpenCV进行图像旋转和平移

Opencv 基础(四):使用OpenCV进行图像旋转和平移

#Opencv 基础(四):使用OpenCV进行图像旋转和平移| 来源: 网络整理| 查看: 265

如今,图像编辑变得越来越流行,因为手机有内置的功能,可以让你裁剪、旋转和更多的操作你的图像。

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

旋转图像移动图像 基本图像变换操作

图像旋转和平移是图像编辑中最基本的操作之一。两者都属于广义的仿射变换。因此,在学习更复杂的转换之前,你应该首先学习旋转和平移图像,使用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 #include 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进行图像旋转

通过定义一个变换矩阵M,你可以将图像旋转一个特定的角度θ。这个矩阵通常是这样的: OpenCV提供了为图像定义旋转中心的能力,以及一个缩放因子来调整图像的大小。在这种情况下,变换矩阵如下。 在上述矩阵中: 其中 c x c_x cx​和 c y c_y cy​是图像旋转所沿的坐标。

OpenCV提供了getRotationMatrix2D()函数来创建上面的转换矩阵。 以下是创建2D旋转矩阵的语法:

getRotationMatrix2D(center, angle, scale)

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

center:图像的旋转中心:angle: 旋转角度:scale :一个各向同性的比例因子,根据提供的值将图像向上或向下缩放

如果angle是正的,图像将逆时针方向旋转。如果你想顺时针旋转图像相同的量,那么角度需要是负的。

旋转分为三步操作: 1. 首先,你需要得到旋转的中心。这通常是你要旋转的图像的中心。 2. 接下来,创建2d旋转矩阵。OpenCV提供了我们在上面讨论过的getRotationMatrix2D()函数。 3. 最后,使用在上一步中创建的旋转矩阵对图像应用仿射变换。OpenCV中的warpAffine()函数完成这项工作。

warpAffine()函数的作用是:对图像应用一个仿射变换。在进行仿射变换后,原图像中所有的平行线在输出图像中也保持平行。

warpAffine()的完整语法如下所示

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

下面是函数的参数:

src:原图M:变换矩阵dsize:输出图像的大小dst:输出图像flags: 插值方法的组合如INTER_LINEAR或INTER_NEARESTborderMode:像素扩展方法borderValue:在边界不变的情况下使用的值,默认值为0

注意:你可以在这里了解更多关于OpenCV仿射转换的信息

旋转后的图片 在这里插入图片描述

使用OpenCV平移图像

在计算机视觉中,图像的平移意味着沿着x轴和y轴移动指定数量的像素。设图像需要移动的像素为tx和ty,则可以定义一个平移矩阵M: 现在,在通过tx和ty值移动图像时,有几点你应该记住。

为tx提供正值将使图像向右移动,而负值将使图像向左移动。同样,ty值为正值时,图像会向下平移,而ty值为负值时,图像会向上平移。

按照以下步骤使用OpenCV平移图片:

首先读取图像,获得图像的宽度和高度。接下来,像旋转一样,创建一个变换矩阵,这是一个2D数组。这个矩阵包含沿x轴和y轴移动图像所需的信息。同样,在旋转中,在最后一步中使用warpAffine()函数来应用仿射转换。

仔细阅读这段代码,看看它有多简单:

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

对于平移矩阵,如上所述,您将需要tx和ty。在本例中,您使用宽度和高度的四分之一作为平移值。我们建议您尝试不同的值并研究它们的输出。

现在,使用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。

最后的代码块将显示转换后的图像,并将其写入磁盘。

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

下图显示了平移后的图像。 在这里插入图片描述



【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有