Project 1
Computer Vision Week 1 Challenge:
In [ ]:
'''
since we are going to use opencv and numpy libraries to perform the
required operations import the opencv and numpy libraries
'''
import cv2
import numpy as np
In [ ]:
'''
This function reads the original image on which any operation need to be performed
image to be read is patssed to this function
'''
def read_image(image_path):
image = cv2.imread(image_path)
return image
In [ ]:
" As name indicates this functions saves the image"
def save_image(image_name,image):
cv2.imwrite(image_name,image) # save image
Task 1: Rotate Image
In [ ]:
'''
Perform image rotation of the loaded image
rotate_image(image,dimensions,angle,scale)
image - image to be rotated
dimensions - to find the centre of image
angle - angle by which the image to be rotated
scale - scaling factor for image ( for this program we are considering it as 1)
'''
def rotate_image(image,dimensions,angle,scale):
height_img = dimensions[0]
width_img = dimensions[1]
centre_height = height_img/2
centre_width = width_img/2
rotation_matrix = cv2.getRotationMatrix2D((width_img/2,height_img/2),angle,scale)
rotated_image = cv2.warpAffine(image,rotation_matrix,(width_img,height_img))
return rotated_image
Task 2: Scale the rotated image
In [ ]:
''' This function scales up/ down the image by resizing and
returns resized image. This function uses resize function of open CV
it takes imageto resized,dimension to which image to be resized and interpolation method
'''
def resize_image(image,dimensions,method):
resize_method = method
#resized_image = cv2.resize(image,dimensions)
resized_image = cv2.resize(image,dimensions,interpolation = resize_method)
return resized_image
Task 3: Crop the original image
In [ ]:
'''
This function crops the image to desired cropping size and
returns cropped image
crop_image(<image to be croped>,<cropping dimensions>)
'''
def crop_image(image,dimensions):
#image_dims = image.shape
h_s,h_e,w_s,w_e = dimensions # extract individual components croping dimension
width_dims = [int(w_s),int(w_e)]
height_dims = [int(h_s),int(h_e)]
np.clip(height_dims,a_min = 0,a_max = image.shape[0]) # restrict cropping height with image height
np.clip(width_dims,a_min = 0,a_max = image.shape[1]) # restrict cropping width with image width
crop_image = image[height_dims[0]:height_dims[1],width_dims[0]:width_dims[1]] # perform cropping operation
return crop_image
Call each function to perform the respective operations
In [ ]:
'''
The script in this cell performs rotation operation on orginal image
No need to mention the full path if image is placed in same folder where we have python code
rotate the image by 140 degree
'''
test_image = read_image('Challenge1.png') # read the image
rotation_angle = 140 # 140 degree angle of rotation
scale = 1 # maintain the original size of image
# extract the dimension of image as this would be required to find the centre to rotate the image
dimensions = (test_image.shape[0],test_image.shape[1])
rotated_image = rotate_image(test_image,dimensions,rotation_angle,scale)
save_image("rotated_image1.png",rotated_image) # save the rotated image
In [ ]:
'''
The script in this cell performs scaling operation on rotated image
load the rotated image to perform scaling operation'''
rotated_image = read_image("rotated_image.png") # load the rotated image
method = cv2.INTER_AREA # use INTER_AREA method for resizing(scaling) the image
# reduced_dimensions = (reduced_width,reduced_height)
reduced_dimensions = (rotated_image.shape[1]//2,rotated_image.shape[0]//2) # reduce the dimension of rotated image by 50%
resized_image = resize_image(rotated_image,reduced_dimensions,method) # resize the image
save_image("scaled_image.png",resized_image) # save the resized image
In [ ]:
'''
The script in this cell performs cropping operation on original image
This program crops the image by ~30% in width and ~30% in height
'''
image = read_image('Challenge1.png')
# crop the original image in height and width to focus on the suject of image
crop_dimension = (image.shape[0]*0.17,image.shape[0]*0.89,image.shape[1]*0.18,image.shape[1]*0.86)
#crop_dimension = (h_s,h_e,w_s,w_e)
cropped_image = crop_image(image,crop_dimension) # crop_image(<image to be croped>,<cropping dimensions>)
save_image("cropped_image.png",cropped_image)