Challenge

Project 1

Task:

The input image for this challenge can be found in the attachment section. Solve the following problem statements.

  1. Script a function to perform image rotation by deriving the rotation matrix for a 140-degree rotation angle.
  2. Now on the rotated image perform a scaling operation that reduces the size of the image by 50% of the original size.
  3. Take the original image and perform the cropping operation manually which focuses on the subject of the input image.

Deliverables:

  1. The rotated image
  2. The scaled image
  3. Cropped image
  4. Necessary calculations for the scaling operations which fixes the scaling percentage.

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:-

  • cv2 is the library to import the OpenCV Module
  • NumPy is the library for the Python programming language that provides support for large, multi-dimensional arrays.

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

  1. Computer Vision Algorithms and Applications
  2. OpenCV-Python Tutorials Documentation
  3. Practical Python and OpenCV: An Introductory, Example-Driven Guide to Image Processing and Computer Vision
Your portfolio, next

Ship work like this
in your own course.

Talk to a career counsellor

Free 20-minute call. We'll map the right course to where you want to work.

By submitting you agree to our Terms and Privacy Policy.