cv2.getRotationMatrix2D() 函数用于制作用于旋转图像的变换矩阵 M。
用法:
cv2.getRotationMatrix2D(中心,角度,比例)
Parameters:
- center:旋转中心
- 角度(θ):旋转角度。 anti-clockwise 的角度为正,顺时针的角度为负。
- scale:缩放图像的缩放因子
返回:2×3 旋转矩阵 M
米=
其中,
这是一种仿射变换。仿射变换是保留线和平行度的变换。 warpaffine() 函数将这些变换矩阵作为参数,并返回旋转后的图像。
使用的图片:
范例1:
Python3
import cv2
# Reading the image
image = cv2.imread('image.jpeg')
# Extracting height and width from
# image shape
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=90, scale=1)
# rotate the image using cv2.warpAffine
# 90 degree anticlockwise
rotated_image = cv2.warpAffine(
src=image, M=rotate_matrix, dsize=(width, height))
cv2.imshow("rotated image:", rotated_image)
cv2.imwrite('rotated_image.jpg', rotated_image)
输出-
范例2:
Python3
import cv2
# Reading the image
image = cv2.imread('image.jpeg')
# Extracting height and width from
# image shape
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=-90, scale=1)
# rotate the image using cv2.warpAffine 90
# degree clockwise
rotated_image = cv2.warpAffine(
src=image, M=rotate_matrix, dsize=(width, height))
cv2.imshow("rotated image:",rotated_image)
cv2.imwrite('rotated_image.jpg', rotated_image)
输出-
Python3
import cv2
# Reading the image
image = cv2.imread('image.jpeg')
# Extracting height and width from image shape
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=180, scale=1)
# rotate the image using cv2.warpAffine 180
# degree anticlockwise
rotated_image = cv2.warpAffine(
src=image, M=rotate_matrix, dsize=(width, height))
cv2.imshow("rotated image:", rotated_image)
cv2.imwrite('rotated_image.jpg', rotated_image)
输出-
范例4:
Python3
import cv2
# Reading the image
image = cv2.imread('image.jpeg')
# Extracting height and width from image shape
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=-180, scale=1)
# rotate the image using cv2.warpAffine 180
# degree clockwise
rotated_image = cv2.warpAffine(
src=image, M=rotate_matrix, dsize=(width, height))
cv2.imshow("rotated image:", rotated_image)
cv2.imwrite('rotated_image.jpg', rotated_image)
输出-
相关用法
- Python OpenCV setWindowTitle()用法及代码示例
- Python OpenCV resizeWindow()用法及代码示例
- Python OpenCV waitKey()用法及代码示例
- Python OpenCV waitKeyEx()用法及代码示例
- Python OpenCV destroyAllWindows()用法及代码示例
- Python OpenCV namedWindow()用法及代码示例
- Python OpenCV selectroi()用法及代码示例
- Python OpenCV imdecode()用法及代码示例
- Python OpenCV getTrackbarPos()用法及代码示例
- Python OpenCV Filter2D()用法及代码示例
- Python OpenCV Canny()用法及代码示例
- Python OpenCV setTrackbarPos()用法及代码示例
- Python OpenCV getgaussiankernel()用法及代码示例
- Python OpenCV haveImageReader()用法及代码示例
注:本文由纯净天空筛选整理自bhavyajain4641大神的英文原创作品 Python OpenCV – getRotationMatrix2D() Function。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。