Opencv Python 简明教程
OpenCV Python - Resize and Rotate an Image
在本章中,我们将了解如何使用 OpenCVPython 调整图像大小和旋转图像。
In this chapter, we will learn how to resize and rotate an image with the help of OpenCVPython.
Resize an Image
可以使用 cv2.resize() 函数放大或缩小图像。
It is possible to scale up or down an image with the use of cv2.resize() function.
resize() 函数的使用方式如下:
The resize() function is used as follows −
resize(src, dsize, dst, fx, fy, interpolation)
一般来说,插值是在已知数据点之间估计算值的处理过程。
In general, interpolation is a process of estimating values between known data points.
当图形数据包含一个间隙,但在间隙的两侧或间隙内的几个特定点内有数据可用时,插值允许我们估算间隙内的值。
When graphical data contains a gap, but data is available on either side of the gap or at a few specific points within the gap. Interpolation allows us to estimate the values within the gap.
在上 resize() 函数中,插值标记确定用于计算目标图像大小的插值类型。
In the above resize() function, interpolation flags determine the type of interpolation used for calculating size of destination image.
Types of Interpolation
插值类型如下:
The types of interpolation are as follows −
-
INTER_NEAREST − A nearest-neighbor interpolation.
-
INTER_LINEAR − A bilinear interpolation (used by default)
-
INTER_AREA − Resampling using pixel area relation. It is a preferred method for image decimation but when the image is zoomed, it is similar to the INTER_NEAREST method.
-
INTER_CUBIC − A bicubic interpolation over 4x4 pixel neighborhood
-
INTER_LANCZOS4 − A Lanczos interpolation over 8x8 pixel neighborhood
首选插值方法是 cv2.INTER_AREA 用于缩小,cv2.INTER_CUBIC(慢)和 cv2.INTER_LINEAR 用于缩放。
Preferable interpolation methods are cv2.INTER_AREA for shrinking and cv2.INTER_CUBIC (slow) & cv2.INTER_LINEAR for zooming.
Example
以下代码将“messi.jpg”图像缩小到其原始高度和宽度的二分之一。
Following code resizes the ‘messi.jpg’ image to half its original height and width.
import numpy as np
import cv2
img = cv2.imread('messi.JPG',1)
height, width = img.shape[:2]
res = cv2.resize(img,(int(width/2), int(height/2)), interpolation =
cv2.INTER_AREA)
cv2.imshow('image',res)
cv2.waitKey(0)
cv2.destroyAllWindows()
Rotate an image
OpenCV 使用仿射变换函数对图像进行诸如平移和旋转之类的操作。仿射变换是一种变换,可以表示为矩阵乘法(线性变换)后跟向量加法(平移)。
OpenCV uses affine transformation functions for operations on images such as translation and rotation. The affine transformation is a transformation that can be expressed in the form of a matrix multiplication (linear transformation) followed by a vector addition (translation).
cv2 模块提供两个函数 cv2.warpAffine 和 cv2.warpPerspective ,您可以使用它们执行各种变换。cv2.warpAffine 采用 2x3 变换矩阵,而 cv2.warpPerspective 采用 3x3 变换矩阵作为输入。
The cv2 module provides two functions cv2.warpAffine and cv2.warpPerspective, with which you can have all kinds of transformations. cv2.warpAffine takes a 2x3 transformation matrix while cv2.warpPerspective takes a 3x3 transformation matrix as input.
为了找到用于旋转的变换矩阵,OpenCV 提供了一个函数 cv2.getRotationMatrix2D ,如下所示:
To find this transformation matrix for rotation, OpenCV provides a function, cv2.getRotationMatrix2D, which is as follows −
getRotationMatrix2D(center, angle, scale)
然后我们将 warpAffine 函数应用于 getRotationMatrix2D()函数返回的矩阵,以获得旋转后的图像。
We then apply the warpAffine function to the matrix returned by getRotationMatrix2D() function to obtain rotated image.
以下程序将原始图像旋转 90 度,而不改变其尺寸:
Following program rotates the original image by 90 degrees without changing the dimensions −
Example
import numpy as np
import cv2
img = cv2.imread('OpenCV_Logo.png',1)
h, w = img.shape[:2]
center = (w / 2, h / 2)
mat = cv2.getRotationMatrix2D(center, 90, 1)
rotimg = cv2.warpAffine(img, mat, (h, w))
cv2.imshow('original',img)
cv2.imshow('rotated', rotimg)
cv2.waitKey(0)
cv2.destroyAllWindows()