0% found this document useful (0 votes)
9 views

Lab Program 8

ssss

Uploaded by

Vinutha H M
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

Lab Program 8

ssss

Uploaded by

Vinutha H M
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 17

• Write a program to show rotation, scaling, and translation

on an image

import cv2 // Import the cv2 module


import numpy as np // import numpy library

# Load the image


image = cv2.imread('images.jpg')

imread() function loads an image from the specified


file and returns it.
# Define rotation angle (in degrees), scaling factor, and
translation values
rotation_angle = 45
scaling_factor = 1.5
translation_x = 50

translation_y = 50
height, width = image.shape[:2]
• Slicing operator [:2] extracts the first two elements from the shape
tuple. i.e height and width
• Ignores the third element (‘channels’) if present.

• image.shape is used to get the dimensions (height, width,


number of channels) of an image.
1) Height
2) Width
3) Number of channels (3 for color image, 1 for grey scale
image)
# Define the rotation matrix
rotation_matrix = cv2.getRotationMatrix2D((width / 2,
height / 2), rotation_angle, 1)
# Perform rotation
rotated_image = cv2.warpAffine(image,
rotation_matrix, (width, height))
• The getRotationMatrix2D function in OpenCV
(cv2.getRotationMatrix2D) is used to compute a 2D
rotation matrix that can be applied to an image to
perform a rotation around a specified center point.
Syntax:
cv2.getRotationMatrix2D(center, angle, scale)
• center: This parameter specifies the center of rotation
(center_x, center_y).
• The rotation will be performed around this point.
• angle: This parameter specifies the rotation angle in
degrees.
• Positive values indicate counter-clockwise rotation
• scale: This parameter specifies a scaling factor for the
rotation. It is optional and defaults to 1.0.

• The function returns a 2x3 rotation matrix (M) which


can be applied to the image using cv2.warpAffine()
to perform the rotation.
rotation_matrix = cv2.getRotationMatrix2D((width / 2,
height / 2), rotation_angle, 1)
cv2.getRotationMatrix2D(center, angle, scale)
center= (center_x, center_y)
• cv2.warpAffine() is a function used to apply an affine
transformation to an image.
Syntax:
dst = cv2.warpAffine(src, M, dsize)
src:

• This parameter is the input image (source image) on


which the affine transformation will be applied.

• It should be a NumPy array representing the image.


• M: This is the 2x3 transformation matrix
• dsize: This parameter specifies the size (width, height)
of the output image.
• It is a tuple containing the dimensions of the output
image in pixels.

dst = cv2.warpAffine(src, M, dsize)

rotated_image = cv2.warpAffine(image, rotation_matrix,


(width, height))
• scaled_image = cv2.resize(image, None,
fx=scaling_factor, fy=scaling_factor)
cv2.resize(): This function resizes the image based on the
scaling factors fx and fy.
• image: This is the input image that you want to resize.
• None: This parameter specifies the output image size. When
None is passed, it means that the output image size is
determined based on the scaling factors fx and fy.
• fx=scaling_factor: This specifies how much to scale the
image along the horizontal axis.
• fy=scaling_factor: This specifies how much to scale the
image along the vertical axis.
• translation_matrix = np.float32([[1, 0, translation_x], [0,
1, translation_y]])
np.float32:
• This is a NumPy data type specifier, indicating that the
matrix elements will be 32-bit floating point numbers.

[[1, 0, translation_x], [0, 1, translation_y]]:

• This is a 2x3 matrix (2 rows, 3 columns) that


represents the translation transformation.
translation_matrix = np.float32([[1, 0, translation_x],
[0, 1, translation_y]])
• translated_image = cv2.warpAffine(image,
translation_matrix, (width, height))
image: This is the input image that you want to
transform.
translation_matrix:
• This is a 2x3 transformation matrix (np.float32) that
defines the translation operation.
(width, height):
• This tuple specifies the dimensions (width and height) of
the output image after the transformation.
cv2.imshow('Original Image', image)

cv2.imshow('Rotated Image', rotated_image)


cv2.imshow('Scaled Image', scaled_image)
cv2.imshow('Translated Image', translated_image)
cv2.waitKey(0)

cv2.destroyAllWindows()
waitkey()

• waitkey() function of Python OpenCV allows users to


display a window for given milliseconds or until any
key is pressed.

• If 0 is passed in the argument it waits till any key is


pressed.

cv2.waitKey(0)
cv2.destroyAllWindows()
• The function cv2.destroyAllWindows() is used in
OpenCV (cv2) to close all the OpenCV windows that
were created with cv2.imshow().

You might also like