當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


Python OpenCV getRotationMatrix2D()用法及代碼示例


cv2.getRotationMatrix2D() 函數用於製作用於旋轉圖像的變換矩陣 M。

用法:

cv2.getRotationMatrix2D(中心,角度,比例)

Parameters: 



  • center:旋轉中心
  • 角度(θ):旋轉角度。 anti-clockwise 的角度為正,順時針的角度為負。
  • scale:縮放圖像的縮放因子

返回:2×3 旋轉矩陣 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)

輸出-

範例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)

輸出-




相關用法


注:本文由純淨天空篩選整理自bhavyajain4641大神的英文原創作品 Python OpenCV – getRotationMatrix2D() Function。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。