Project 1
import cv2
import numpy as np
import os
#READ AND DISPLAY IMG FUNCTION
def read_img (image_path):
img = cv2.imread(image_path) #reads in bgr fromat
return img
def disp_img (img_window,read_img):
image = cv2.imshow(img_window,read_img)
cv2.waitKey(0)
cv2.destroyAllWindows()
#CROPPING FUCNTION
def crop_img(image,crp_dimension,): #function is defined
img_shape = image.shape #shape of input image is obtained
hs,he,ws,we = crp_dimension #cropping dimensions for height and width
width_dims = [ws,we] #width dimensions
height_dims = [hs,he] #height dimensions
np.clip(height_dims,a_min=0, a_max=img_shape[0]) #both the dimensions are #restricted to the height and width of the image
np.clip(width_dims,a_min=0, a_max=img_shape[1])
crop_image = image[height_dims[0]:height_dims[1],width_dims[0]:width_dims[1]]
return crop_image
#ROTATION FUNCTION
def img_rotation(image,rot_angle,req_scale,trans_w,trans_h): #rotations fucntion is #defined
img_center = (image.shape[1]/2,image.shape[0]/2) #image centre is #obtained from where theimage is rotated at the rotation anglr which is taken as #argument in to the function
rot_matrix = cv2.getRotationMatrix2D(center=img_center ,angle=rot_angle
,scale=req_scale) #this returns rotation matrix
rot_img = cv2.warpAffine(src=image ,M=rot_matrix ,dsize=(trans_w,trans_h)) #here #the warping operation takes place where the roation matrix is kind of multiplied by the #image to ge the desired o/p
return rot_img
#SCALING OPERATION
#It is kind of resizing operation ,
#increasing / decreasing the image size = scaling UP/DOWN of the image
#this can be done either by specifying dimensions or by scale factor which is matrix #which multplied by the image performs scaling
# og width = scaling factor X new width
#the function below also iterates over the different interpolation methods and saves the #o/p by each method into a list which is returned.
def scaling(image ,dimension) :
width ,height = image.shape[0],image.shape[1]
s_width ,s_height =dimension
sfactor_x = s_width / width
sfactor_y = s_height / height
print(f'SCALE FACTOR x ={sfactor_x} \n SCALE FACTOR y ={sfactor_y}\n')
print(f'SCALE FACTOR x percentage ={sfactor_x*100} \n SCALE FACTOR y =
{sfactor_y*100}')
scaled_img_list = []
methods =[
cv2.INTER_AREA, cv2.INTER_NEAREST, cv2.INTER_CUBIC, cv2.INTER_LANCZOS4
]
for i in range(len(methods)):
resize_method = methods[i]
img_resized = cv2.resize(image, None, fx=sfactor_x,fy=sfactor_y
,interpolation=resize_method)
scaled_img_list.append(img_resized)
return scaled_img_list
#MAIN EXECUTION
image = read_img('Challenge1_.png')
disp_img('original image',image)
print(image.shape)
img_center = (image.shape[0]/2,image.shape[1]/2)
print(img_center)
#CROP IMAGE
crop_dimension = [300,400,530,610]
cropped_img = crop_img(image,crop_dimension)
disp_img('cropped_img',cropped_img)
#ROTATION OF IMAGE
rotation_angle = 30
required_scale = 1
required_width = 0
required_height = 0
rotated_img = img_rotation(image,rotation_angle,required_scale,required_width,required_height)
disp_img('Rotated Image',rotated_img)
#SCALE IMAGE
dimension = (900,1000)
scale = scaling(image,dimension)
for j in range(len(scale)):
disp_img(f'Image {j}',scale[j])
print(scale[j].shape)
ORIGINAL IMAGE
CROPPED IMAGE
ROTATED IMAGE
SCALED UP IMAGE
SCALED DOWN IMAGE
ALL THE OUTPUT FILES ARE UPLOADED ALONGWITH THE JUPYTER NOTEBOOK FOR THE CODE OF SCALING,ROTATION,CROPPING OPERATIONS