Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Python OpenCV – getRotationMatrix2D() Function

  • Last Updated : 10 Oct, 2021

cv2.getRotationMatrix2D() function is used to make the transformation matrix M which will be used for rotating a image.

Syntax: 

cv2.getRotationMatrix2D(center, angle, scale)

Parameters: 

  • center: Center of rotation
  • angle(θ): Angle of Rotation. Angle is positive for anti-clockwise and negative for clockwise.
  • scale: scaling factor which scales the image

Return: 2×3 Rotation Matrix M

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}

where,

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

This is a type of affine transformation. An affine transformation is transformation which preserves lines and parallelism. These transformation matrix are taken by warpaffine() function as parameter and the rotated image will be returned.

Image Used:

Example 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)

Output-

Example 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)

Output-

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)

Output-

Example 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)

Output – 


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!