📜  Python OpenCV – getRotationMatrix2D()函数

📅  最后修改于: 2022-05-13 01:55:36.784000             🧑  作者: Mango

Python OpenCV – getRotationMatrix2D()函数

cv2.getRotationMatrix2D()函数用于生成将用于旋转图像的变换矩阵 M。

句法:

米 = \begin{bmatrix} \alpha & \beta & (1-\alpha)\cdot c_x-\beta \cdot c_y\\ -\beta & \alpha & \beta\cdot c_x+(1-\alpha) \cdot c_y \end{bmatrix}

在哪里,

\alpha = scale\cdot cos(\theta)\\ \beta = scale\cdot sin(\theta)\\ c_x\ and\ c_y\ are\ the\ coordinates\ of\ the\ center\ of\ the\ image.\\

这是一种仿射变换。仿射变换是保留直线和平行度的变换。这些变换矩阵由 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)


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)


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)


输出-

示例 2:

蟒蛇3

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)

输出-

蟒蛇3

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:

蟒蛇3

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)

输出 -