Project 1
code:
import numpy as np
import cv2
def display_img(disp_img, title ='img_display'):
cv2.imshow(title,disp_img)
cv2.waitKey(0)
def read_img(path):
img_matrix = cv2.imread(path)
display_img(img_matrix)
return img_matrix
def rotate_img(img,angle,scale):
height,width = img.shape[:2]
img_center = (width/2, height/2) # rotate image at its center
# returns Transformation Matrix M
transformation_matrix = cv2.getRotationMatrix2D(img_center, angle, scale)
# returns transformed matrix (rotated image)
transformed_matrix = cv2.warpAffine(img, transformation_matrix, (width, height))
return transformed_matrix
def crop_img(img,h1,h2,w1,w2):
cropped = img[h1:h2,w1:w2]
return cropped
input_img = read_img('assignment_1/Challenge_1.PNG')
# Rotated Image
img_rotated = rotate_img(input_img,angle = 140,scale = 1)
display_img(img_rotated,'rotated_img')
# 50% scale reduced image
height,width = input_img.shape[:2]
x = int(width/2) #scaling 50% of x
y = int(height/2) #scaling 50% of y
resized_img = cv2.resize(img_rotated,(x,y))
display_img(resized_img,'resized_img')
# cropped image
cropped_img = crop_img(input_img,90,570,160,880)
display_img(cropped_img,'cropped_img')
Input_Image

Rotated_image

Resized_Image

Cropped_Image
