Challenge

Project 1

import cv2 # importing the important libraries
import numpy as np # importing numpy libraries as np


# creating helper functions for further use
# a read image function
def read_image(image_path): 
    image = cv2.imread(image_path) # Cv2 reads in BGR format 
    return image

# a display image function
def display_image(window_name, read_image):
    cv2.imshow(window_name, read_image)
    cv2.waitKey(0)


# a save_image function
def save_image(image_name, image):
    cv2.imwrite(image_name, image) 

an image roation function:
Here we are trying to develop a image rotaion function that can be used at any position

We try to rotate a function by an angle through defining a transformation matrix M. Her we use openCV libraries and using thius we gain the ability to define the centre of rotation for the image and also the scale factor up to whcih the image can be scaled after rotation.

Here using openCV function getRotationMatrix2D(), we create the transformation matrix M

The syntax for the above function is:

getRotationMatrix2D(center, angle, scale)
The above function takes the following arguments:

centre: the dimension of centre of rotation along with the image is rotated. angle: the angle upto which the image rotates. scale: the factor upto which the image scales up or down.

The process goes as follows:

First we consider the the rotation dimensions

Create the rotation matrix using the openCV function

apply the transformation to the image using the warpAffine() function


def image_rotations(image):
    height, width = image.shape[:2] # it gets the value of the width and height of the image from the shape of the image 
    dimension = (width/2, height/2) # using this we get the dimension for centre of rotation
    # using this function we create the rotation matrix of angle 140 degrees and scale= 1
    rotate_matrix = cv2.getRotationMatrix2D(center=dimension, angle = 140, scale=1) 
    # using thie we create the required roated image. 
    rotated_image = cv2.warpAffine(image, rotate_matrix, dsize=(width, height))
    return rotated_image


image = read_image('challenge_1.png')
rotated_image = image_rotations(image)
display_image('rotated_image',rotated_image) # this function displays the rotated image
save_image('rotated_image.jpg',rotated_image) # the image obtained is saved in as rotated_image.jpg


scaling the rotated image:
For scaling the image we use the openCV resize() function. For scaling the function we need the image file and size of the image.

The funcstion resize is as follows:

resize(src, dsize, fx, fy, interpolation)
here we consider the dsize function as None since we are using same rotated image

fx = the scaling factor along horizontal direction upto which the image can be scaled. As per the requirement we are scaling the image up to 50% so we take the valus as 0.5

fy = the scaling factor along vertical direction and the value is also 0.5.

interpolation = it gives the option to use differnt methods of resizing the image. Here we consider the following methods:

1) cv2.INTER_AREA, 2) cv2.INTER_LINEAR, 3) cv2.INTER_NEAREST, 4) cv2.INTER_CUBIC, 5) cv2.INTER_LANCZOS4

# scaling the rotated image

def scale_image(image,method_number, x, y):
    sc_up = x # scaling the image upto 0.5 along horizontal direction
    sc_down = y # scaling the image upto 0.5 along vertical direction
    
    # from the given methods we choose any one of the resizing method
    method = [cv2.INTER_AREA, cv2.INTER_LINEAR, cv2.INTER_NEAREST, cv2.INTER_CUBIC, cv2.INTER_LANCZOS4]
    
    scale_method = method[method_number]
    
    # using the below resize() function we try to scale the image upto 50% 
    scale_image = cv2.resize(image,None, fx= sc_up,fy=sc_down, interpolation = scale_method)
    return scale_image


image = read_image('rotated_image.jpg')
x = 0.5 # scaling dimension
y = 0.5 # scaling dimension
method = 0 
scaled_image = scale_image(image, method, x, y)
display_image('scale_image',scaled_image) # displays the required image
save_image('scaled_image.jpg',scaled_image # saves the required image


Cropping the Image
We use cropping method by slicing the image form one coordinate to another. To crop an image we need two dimenions:

first will be the no of rows or height second will be no of columns or width

NOw we give the dimension required for slicing in the form of numpy slicing and perform the croping



def crop_image(image,dimensions):
    image_dims = image.shape # it defines the shape of the image
    h_s, h_e, w_s, w_e = dimensions # we define the required values from the dimensions 
    width_dims = [w_s, w_e] # it defines the width required for croping
    height_dims = [h_s, h_e] # it defines the heigth required for cropping
    np.clip(height_dims,a_min = 0, a_max= image_dims[0]) # it creates a cropped image along the given height dimension
    np.clip(width_dims, a_min = 0, a_max = image_dims[1]) # it creates a cropped image along the given width dimension
    crop_image = image[height_dims[0]:height_dims[1], width_dims[0]:width_dims[1]] # this function takes in the slicing commends and does the cropping 
    return crop_image



image = read_image('challenge_1.png')
crop_dimensions = [0,image2.shape[0],0,image2.shape[1]]
cropped_image = crop_image(image, crop_dimensions)
display_image('cropped_image',cropped_image)
save_image('cropped_image.jpg',cropped_image)









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.

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.