如何使用OpenCV和imutils库在Python中旋转图像的角度

736 阅读3分钟

简介

在本教程中,我们将向你展示如何在OpenCV和imutils库的帮助下将图像旋转到任何角度。我们将在文章中通过实例介绍以下函数

  1. OpenCV函数cv2.rotate()可以将图像旋转90度和180度
  2. imutils函数rotate_bound()和rotate()可以将图像旋转任何角度。

使用OpenCV旋转图像:cv2.rotate()

语法

cv2.rotate(src, rotateCode)

  • src - 要应用旋转的输入图像
  • rotateCode- 用于指定如何旋转图像的代码

Python OpenCV中cv2.rotate()的例子

首先,让我们导入OpenCV库,如下所示

在[0]中:

import cv2

读取样本图像并显示

接下来,我们将为我们的例子读取一个样本图像并显示它

在[1]中:

window_name='Dog Image'
cv2.namedWindow(window_name, cv2.WINDOW_NORMAL)
cv2.imshow(window_name,img)
cv2.waitKey(0)
cv2.destroyAllWindows()

输出

OpenCV Python cv2.rotate() Example

例子 - 1: 用cv2.rotate()将图像顺时针旋转90度

要将图像顺时针旋转90度,使用rotateCode = cv2.ROTATE_90_CLOCKWISE,如下所示。

在[3]中。

rotate_image = cv2.rotate(img, cv2.ROTATE_90_CLOCKWISE)

window_name='OpenCV Rotate Image 90 degree Clockwise'
cv2.namedWindow(window_name, cv2.WINDOW_NORMAL)
cv2.imshow(window_name,rotate_image)
cv2.waitKey(0)
cv2.destroyAllWindows()

输出

OpenCV Python cv2.rotate() Example Rotate Image 90 degree clockwise

例子 - 2: 用cv2.rotate()将图像旋转180度

要将图像顺时针旋转90度,使用rotateCode = cv2.ROTATE_180,如下面的例子所示。这里没有顺时针或逆时针旋转图像的概念,因为在这两种情况下,图像旋转180会使转换后的图像达到相同的状态。

在[4]中。

rotate_image = cv2.rotate(img, cv2.ROTATE_180)

window_name='OpenCV Rotate Image 180 degree'
cv2.namedWindow(window_name, cv2.WINDOW_NORMAL)
cv2.imshow(window_name,rotate_image)
cv2.waitKey(0)
cv2.destroyAllWindows()

输出

OpenCV Python cv2.rotate() Example Rotate Image 180

例子 - 3: 将图像逆时针旋转90度或顺时针旋转270度

要将图像逆时针旋转90度或顺时针旋转270度(这是同一件事),我们使用rotateCode= ROTATE_90_COUNTERCLOCKWISE,如下例所示。

在[5]中。

rotate_image = cv2.rotate(img, cv2.ROTATE_90_COUNTERCLOCKWISE)

window_name='OpenCV Rotate Image 90 degree anticlockwise'
cv2.namedWindow(window_name, cv2.WINDOW_NORMAL)
cv2.imshow(window_name,rotate_image)
cv2.waitKey(0)
cv2.destroyAllWindows()

输出

Example Rotate Image 90 degree anticlockwise

使用imutils库在Python中旋转图像的角度

开箱即用的OpenCV函数cv2.rotate()只能让我们以90度的倍数旋转图像。然而,在OpenCV中没有直接的函数来旋转图像的任何程度,它是一个多步骤的过程。这就是imutils库的用处。

imutils库由OpenCV上面的许多方便的封装函数组成。它包含两个有用的函数imutils.rotate()和imutils.rotate_bound(),用于将图像旋转到任何指定的角度。

这两个函数之间的区别是,imutils.rotate()在旋转图像时可能最终会裁剪图像,而imutils.rotate_bound()不会裁剪图像,并在边界内保留整个图像。

imutils.rotate()的语法

imutils.rotate(image, angle)

  • image - 要应用旋转的输入图像
  • angle- 图像需要旋转的角度

imutils.rotate_bound()的语法

imutils.rotate_bound(image, angle)

  • image - 输入需要旋转的图像
  • angle- 要旋转的图像的角度

例子:通过角度来旋转图像 imutils.rotate()

在下面的例子中,我们使用imutils.rotate()将图像旋转了32度。可以注意到,旋转后的图像被裁剪了。

在[6]中:

import imutils

rotate_image = imutils.rotate(img, 32)
window_name='Rotate Image by Angle in Python'
cv2.namedWindow(window_name, cv2.WINDOW_NORMAL)
cv2.imshow(window_name,rotate_image)
cv2.waitKey(0)
cv2.destroyAllWindows()

输出

Rotate Image by Angle in Python OpenCV imutil.rotate()

例子:使用imutils.rotate_boundary()将图像旋转一个角度 imutils.rotate_bound()

在下面的例子中,我们使用了imutils.rotate_bound()将图像旋转32度。这一次可以看到,图像没有被裁剪,而是在边界内被完整地保留下来。

在[7]中:

import imutils

rotate_image = imutils.rotate_bound(img, 32)
window_name='Rotate Image by Angle in Python'
cv2.namedWindow(window_name, cv2.WINDOW_NORMAL)
cv2.imshow(window_name,rotate_image)
cv2.waitKey(0)
cv2.destroyAllWindows()

输出

Rotate Image by Angle in Python OpenCV imutil.rotate_bound()