All Courses
All Courses
Courses by Software
Courses by Semester
Courses by Domain
Tool-focused Courses
Machine learning
POPULAR COURSES
Success Stories
Task: The input image for this challenge can be found in the attachment section. Solve the following problem statements. Script a function to perform image rotation by deriving the rotation matrix for a 140-degree rotation angle. Now on the rotated image perform a scaling operation that reduces the size of the image…
mallikarjun patil
updated on 16 Mar 2023
Task:
The input image for this challenge can be found in the attachment section. Solve the following problem statements.
Deliverables:
Index
Topic 1 - Initial Code Snippet and its explaination
Topic 2 - Task 1
Topic 3 - Task 2
Topic 4 - Task 3
Reference
Topic 1
Initial code Snippet
import cv2
import numpy as np
Explanation:-
Why is Numpy Important?
Here we can express images as multi-dimensional arrays
Topic 2
TASK 1
Script a function to perform image rotation by deriving the rotation matrix for a 140 degree rotation angle
Many common transformations can be described using the affine transform, which is defined as follows
So for Rotation
Rotation by angle 𝜙 about the origin. This is sometimes required to align an image with the axes to simplify further processing.
Now Let's Write a function/code for rotating an image:
def rotate_image(image_path):
img = cv2.imread(image_path) # Load the image
(h, w) = img.shape[:2] # Get the image height and width
center = (w // 2, h // 2) # Define the center of the image
M = cv2.getRotationMatrix2D(center, 140, 1) # Define the rotation matrix
rotated_image = cv2.warpAffine(img, M, (w, h)) # Apply the rotation to the image
return rotated_image # Return the rotated image
Explanation
def rotate_image(image_path): This line defines a function called rotate_image
that takes a single argument, image_path
, which is the path to the image file we want to rotate.
img = cv2.imread(image_path): This line loads the image from the specified file path using the cv2.imread
function, which returns a NumPy array representing the image.
(h, w) = img.shape[:2] = This line gets the height and width of the image by accessing the first two elements of the shape
attribute of the image array.
center = (w // 2, h // 2) = This line defines the center of the image as a tuple of the x and y coordinates at the center of the image, using integer division (//
) to round down to the nearest whole number.
M = cv2.getRotationMatrix2D(center, 140, 1) = This line defines the rotation matrix M
using the cv2.getRotationMatrix2D
function, which takes the center of the image, the rotation angle in degrees (here, 140 degrees), and the scale (here, 1).
rotated_image = cv2.warpAffine(img, M, (w, h)) = This line applies the rotation to the image using the cv2.warpAffine
function, which takes the image, the rotation matrix, and the output image size (in this case, the same size as the original image).
return rotated_image: This line returns the rotated image array. Overall, the rotate_image
function loads an image from a file, calculates the center of the image, creates a rotation matrix with the specified rotation angle and scale, applies the rotation to the image, and returns the rotated image array.
rotated_image = rotate_image('carbody.PNG')
cv2.imshow("Rotated Image", rotated_image)
cv2.waitKey(0)
Result
Topic 3
TASK 2
Now on the rotated image perform a scaling operation that reduces the size of the image by 50% of the original size.
Now Let's write the function for resizing the image
def rotate_and_scale_image(image_path):
img = cv2.imread(image_path) # Load the image
(h, w) = img.shape[:2] # Get the image height and width
center = (w // 2, h // 2) # Define the center of the image
M = cv2.getRotationMatrix2D(center, 140, 1) # Define the rotation matrix
rotated_image = cv2.warpAffine(img, M, (w, h)) # Apply the rotation to the image
scaling_factor = 0.5 # Define the scaling factor
new_image_size = (int(w * scaling_factor), int(h * scaling_factor)) # Calculate the new image size
scaled_image = cv2.resize(rotated_image, new_image_size) # Resize the rotated image
return scaled_image # Return the scaled image
scaled_image = scale_image('carbody.PNG') # Call the rotate_and_scale_image function with the path to the image
cv2.imshow("Scaled Image", scaled_image) # Display the scaled image
cv2.waitKey(0)
scaling_factor = 0.5 = This line defines a scaling factor of 0.5, which represents a 50% reduction in size.
new_image_size = (int(w * scaling_factor), int(h * scaling_factor)) = This line calculates the new size of the scaled image by multiplying the original width and height by the scaling factor and converting the result to integers. This gives us a new width and height that are half the original size.
scaled_image = cv2.resize(rotated_image, new_size) = This line resizes the rotated image using the cv2.resize
function, which takes the image to be resized and the new size as arguments. The function scales the image to the new size using interpolation, which is a method for estimating new pixel values based on the existing ones.
Result
Topic 4
Task 3
Take the original image and perform cropping operation manually which focuses on the subject of the input image.
def crop_image(image,dimensions): #2 args, 1 is image to be cropped and dimensions of image to be cropped
image_dims = image.shape
h_s, h_e, w_s, w_e = dimensions
width_dims = [w_s,w_e]
height_dims = [h_s,h_e]
np.clip(height_dims,a_min = 0, a_max = image_dims[0])
np.clip(width_dims,a_min = 0, a_max = image_dims[1])
crop_image = image[height_dims[0]:height_dims[1],width_dims[0]:width_dims[1]]
return crop_image
h_s = Start of Height
h_e = End of Height
w_s = Start of width
w_e = End of Width
image = read_image('carbody.PNG')
crop_dimensions = [0,500,0,700]
cropped_image = crop_image(image,crop_dimensions)
cv2.imwrite('displayed image',cropped_image)
Reference
Leave a comment
Thanks for choosing to leave a comment. Please keep in mind that all the comments are moderated as per our comment policy, and your email will not be published for privacy reasons. Please leave a personal & meaningful conversation.
Other comments...
Week 5 Challenge : Surface wrap on Automotive Assembly
For the given models, check for the geometrical errors and delete surfaces that are unwanted for Surface wrap as shown in the videos. After clearing geometry, Merge all 3 models and perform surface wrap. The target length for Wrap = 3 mm Write a detailed report on the steps followed to finish this challenge. ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------…
06 Jan 2024 01:18 PM IST
Project 1
Task: The input image for this challenge can be found in the attachment section. Solve the following problem statements. Script a function to perform image rotation by deriving the rotation matrix for a 140-degree rotation angle. Now on the rotated image perform a scaling operation that reduces the size of the image…
16 Mar 2023 02:45 PM IST
Week 2 - Flow over a Cylinder.
AIM :- Simulate the flow over a cylinder and explain the phenomenon of Karman vortex street. PART-I Simulate the flow with the steady and unsteady case and calculate the Strouhal Number for Re= 100. PART-II Calculate the coefficient of drag and lift over a cylinder by setting the Reynolds number…
02 Jan 2023 05:09 PM IST
Week 5 - Rayleigh Taylor Instability
AIM:- What are some practical CFD models that have been based on the mathematical analysis of Rayleigh Taylor waves? In your own words, explain how these mathematical models have been adapted for CFD calculations. Perform the Rayleigh Taylor instability simulation for 2 different mesh sizes with the base mesh being…
16 Sep 2022 05:33 AM IST
Related Courses
0 Hours of Content
Skill-Lync offers industry relevant advanced engineering courses for engineering students by partnering with industry experts.
© 2025 Skill-Lync Inc. All Rights Reserved.