Assignment 1.2 - Transformation

Images: https://drive.google.com/drive/folders/1yCfFEXnEinsWBNejbDgEsziCHdESol1D?usp=share_link

Code

import numpy as np

import matplotlib.pyplot as plt

import cv2

grayImage = r'albert-einstein_gray.jpg'

colourImage = r'tulips.jpg'

I_gray = cv2.imread(grayImage,cv2.IMREAD_GRAYSCALE)

I_BGR = cv2.imread(colourImage)

plt.imshow(I_gray,cmap='gray')

plt.imshow(I_BGR[:,:,::-1])

#scaling

I_gray_resized = cv2.resize(src=I_gray,fx=2,fy=2,dsize=None)

plt.imshow(I_gray_resized,cmap='gray')

# plt.xticks([])

# plt.yticks([])

I_gray_resized.shape

I_gray.shape

I_BGR_resized = cv2.resize(src=I_BGR,fx=0.5,fy=2,dsize=None)

plt.imshow(I_BGR_resized[:,:,::-1])

#Rotation 

image = cv2.rotate(I_gray , cv2.ROTATE_90_CLOCKWISE)
plt.imshow(I_gray_rot,cmap='gray')

# access height and width of the image
height, width = I_gray.shape

# define center of rotation
cr = (width/2,height/2)

# get the rotation matrix
M = cv2.getRotationMatrix2D(cr,30,1)

# apply warpAffine() method to perform image rotation
dst = cv2.warpAffine(I_gray,M,(width,height))

# display the rotated image
cv2.imshow('Image',dst)
cv2.waitKey(0)
cv2.destroyAllWindows()

#Translation 

tx = 100
ty = 0

T = np.float32([[1, 0, tx], [0, 1, ty]])
  
# We use warpAffine to transform
# the image using the matrix, T
img_translation = cv2.warpAffine(I_gray, T, (width, height))
  
cv2.imshow("Originalimage", I_gray)
cv2.imshow('Translation', img_translation)
cv2.waitKey()
  
cv2.destroyAllWindows()

# Reflection 

rows, cols = I_gray.shape
M = np.float32([[1,  0, 0],
                [0, -1, rows],
                [0,  0, 1]])
reflected_img = cv2.warpPerspective(I_gray, M,
                                   (int(cols),
                                    int(rows)))
cv2.imshow("Originalimage", I_gray)
cv2.imshow('Reflection', reflected_img)
cv2.waitKey(0)

#To flip the image horizontally:
#M = np.float32([[1, 0, 0], [0, -1, rows],[0,  0, 1]])
#To flip the image vertically:
#M = np.float32([[-1, 0, cols], [0, 1, 0], [0, 0, 1]])

#Shear 

shx = 0.5
M = np.float32([[1, shx, 0], [0, 1, 0], [0, 0, 1]])
sheared_img = cv2.warpPerspective(I_gray, M, (int(cols), int(rows)))
cv2.imshow('sheared_x-axis_out', sheared_img)
cv2.waitKey(0)
cv2.destroyAllWindows()

shy = 0.5
M = np.float32([[1, 0, 0], [shy, 1, 0], [0,   0, 1]])
sheared_img = cv2.warpPerspective(I_gray, M, (int(cols*1.5), int(rows*1.5)))
cv2.imshow('sheared_y-axis_out', sheared_img)
cv2.waitKey(0)
cv2.destroyAllWindows()




###################### SAMPLE CODE FOR CREATING FUNCTION #################

import numpy as np
import matplotlib.pyplot as plt
import cv2

def translation(img,inp):
    if inp=='1':
        height, width = img.shape
    if inp =='2':
        height, width, _ = img.shape
    
    tx = int(input("Enter TX\n"))
    ty = int(input("Enter TY\n"))

    T = np.float32([[1, 0, tx], [0, 1, ty]])
  
    return cv2.warpAffine(img, T, (width, height))


def Rotation(img,inp):
    if inp=='1':
        height, width = img.shape
    if inp =='2':
        height, width, _ = img.shape
    
    angle = int(input("Enter angle of rotaion \n"))
    cr = (width/2,height/2)
    M = cv2.getRotationMatrix2D(cr,angle,1)
    return cv2.warpAffine(img,M,(width,height))

def main():
    inp = input("Enter 1 for Gray Scale Image \n Enter 2 for Clour Image \n")
    if inp == '1':
        I = cv2.imread(r'C:\Users\SOE_IT\Downloads\albert-einstein_gray (2).jpg',cv2.IMREAD_GRAYSCALE)
    elif inp == '2':
        I_BGR = cv2.imread(r'C:\Users\SOE_IT\Downloads\tulips (2).jpg')
        I = I_BGR[:,:,::-1]
    else: 
        main()
    
    c = input("Enter 1 Translation \n Enter 2 Rotation")
    
    if c =='1':
        I_T = translation(I,inp)
        if inp == '1':
            plt.imshow(I_T,cmap="gray")
        if inp == '2':
            plt.imshow(I_T)
    if c =='2':
        I_T = Rotation(I,inp)
        if inp == '1':
            plt.imshow(I_T,cmap="gray")
        if inp == '2':
            plt.imshow(I_T)

main()

Comments

Popular posts from this blog

Assignment 4: Canny Edge Detection

Assignment 1.1: Image Transformation in Python