Experiment No. 4

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 14

Bansilal Ramnath Agarwal Charitable Trust’s

Vishwakarma Institute of Technology, Pune-37


(An Autonomous Institute Affiliated to Savitribai Pune University)

Department of Electronics & Telecommunication Engineering

ET3221: Computer Vision

Name of the student:Vedant Bhagwan Roll No. 45


Div: ET-A Batch:2
Date of performance: 13/07/23

Experiment No. 4

Problem Statement:

Geometric Transformations

AIM:

Write a Python Code for following Geometric Transformations:


1. Translation
2. Rotation
3. Scaling
4. Cropping
5. Shearing
6. Flipping

Objective(s) of Experiment:
To perform the geometric transformations of images.

Introduction:

Geometric transformations are widely used for image registration and the removal of
geometric distortion. Scaling is just resizing of the image. Rotation is a concept in
mathematics that is a motion of a certain space that preserves at least one point. Image rotation
is a common image processing routine with applications in matching, alignment, and other
image-based algorithms, it is also extensively in data augmentation, especially when it comes
to image classification.
Image transformation is a coordinate changing function, it maps some (x, y) points in one
coordinate system to points (x', y') in another coordinate system. Shear mapping is a linear
map that displaces each point in a fixed direction, it substitutes every point horizontally or
vertically by a specific value in proportion to its x or y coordinates, there are two types of
shearing effects. Image cropping is the removal of unwanted outer areas from an image, a lot
of the above examples introduced black pixels, you can easily remove them using cropping.
Common applications include construction of mosaics, geographical mapping, stereo and
video.
Bansilal Ramnath Agarwal Charitable Trust’s
Vishwakarma Institute of Technology, Pune-37
(An Autonomous Institute Affiliated to Savitribai Pune University)

Department of Electronics & Telecommunication Engineering

Flowchart:
Bansilal Ramnath Agarwal Charitable Trust’s
Vishwakarma Institute of Technology, Pune-37
(An Autonomous Institute Affiliated to Savitribai Pune University)

Department of Electronics & Telecommunication Engineering

Code and results

Translation

# reads an input image


img = cv2.imread(r"F:\CV LAB 1\dog.jpg")
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)                     #BGR 2 RGB for
plotting using matplotlib
h, w = img.shape[:2]

x = int(input("Enter x-axis shift: "))


y = int(input("Enter y-axis shift: "))

t_matrix = np.float32([[1,0,x], [0,1,y]])                      #Creating


translation matrix
n_img = cv2.warpAffine(img, t_matrix, (w, h))

plt.imshow(img)
plt.show()
plt.imshow(n_img)
plt.show()
Bansilal Ramnath Agarwal Charitable Trust’s
Vishwakarma Institute of Technology, Pune-37
(An Autonomous Institute Affiliated to Savitribai Pune University)

Department of Electronics & Telecommunication Engineering

# Rotation

# reads an input image


img = cv2.imread(r"F:\CV LAB 1\dog.jpg")
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)                     #BGR 2 RGB for
plotting using matplotlib
h, w = img.shape[:2]

center = (w//2, h//2)

ang = int(input("Enter degree of rotation: "))                  #Positive value


for anti-clockwise & negative for clockwise

r_matrix = cv2.getRotationMatrix2D(center, ang, 1.0)            #Generating


rotational matrix
r = cv2.warpAffine(img, r_matrix, (w, h))

plt.imshow(img)
plt.show()
plt.imshow(r)
plt.show()
Bansilal Ramnath Agarwal Charitable Trust’s
Vishwakarma Institute of Technology, Pune-37
(An Autonomous Institute Affiliated to Savitribai Pune University)

Department of Electronics & Telecommunication Engineering

# Scaling
Bansilal Ramnath Agarwal Charitable Trust’s
Vishwakarma Institute of Technology, Pune-37
(An Autonomous Institute Affiliated to Savitribai Pune University)

Department of Electronics & Telecommunication Engineering

import cv2
import numpy as np
import matplotlib.pyplot as plt
# reads an input image
img = cv2.imread(r"F:\CV LAB 1\dog.jpg")
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)                     #BGR 2 RGB for
plotting using matplotlib
h, w = img.shape[:2]

scale = float(input("Enter scaling factor: "))                  #scale>1:Img is


scaled-up , scale<1:Img is scaled-down

h = int(img.shape[0] * scale)                                   #Scaling in y-


axis
w = int(img.shape[1] * scale)                                   #Scaling in x-
axis
re = cv2.resize(img, (w, h))                                    #Resizing img
with scaled values

plt.imshow(img)
plt.show()
plt.imshow(re)
plt.show()
Bansilal Ramnath Agarwal Charitable Trust’s
Vishwakarma Institute of Technology, Pune-37
(An Autonomous Institute Affiliated to Savitribai Pune University)

Department of Electronics & Telecommunication Engineering

# Cropping

# reads an input image


img = cv2.imread(r"F:\CV LAB 1\dog.jpg")
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)                     #BGR 2 RGB for
plotting using matplotlib
h, w = img.shape[:2]

print(img.shape)

cxlow = int(input("Enter lower-x limit: "))                    #Enter cropping


limits
cxup = int(input("Enter upper-x limit: "))
cylow = int(input("Enter lower-y limit: "))
cyup = int(input("Enter upper-y limit: "))

crop = img[cxlow:cxup, cylow:cyup]                             #Only displaying


the selected part of img

plt.imshow(img)
plt.show()
plt.imshow(crop)
plt.show()
Bansilal Ramnath Agarwal Charitable Trust’s
Vishwakarma Institute of Technology, Pune-37
(An Autonomous Institute Affiliated to Savitribai Pune University)

Department of Electronics & Telecommunication Engineering

# Flipping using CV2.flip


Bansilal Ramnath Agarwal Charitable Trust’s
Vishwakarma Institute of Technology, Pune-37
(An Autonomous Institute Affiliated to Savitribai Pune University)

Department of Electronics & Telecommunication Engineering

# reads an input image


img = cv2.imread(r"F:\CV LAB 1\dog.jpg")
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)                     #BGR 2 RGB for
plotting using matplotlib
h, w = img.shape[:2]

flip1 = cv2.flip(img, 1)
flip2 = cv2.flip(img, 0)
flip3 = cv2.flip(img, -1)

plt.imshow(img)
plt.show()
plt.imshow(flip1)
plt.show()

plt.imshow(flip2)
plt.show()

plt.imshow(flip3)
plt.show()
Bansilal Ramnath Agarwal Charitable Trust’s
Vishwakarma Institute of Technology, Pune-37
(An Autonomous Institute Affiliated to Savitribai Pune University)

Department of Electronics & Telecommunication Engineering


Bansilal Ramnath Agarwal Charitable Trust’s
Vishwakarma Institute of Technology, Pune-37
(An Autonomous Institute Affiliated to Savitribai Pune University)

Department of Electronics & Telecommunication Engineering

# Flipping using warperspective transform

# reads an input image


img = cv2.imread(r"F:\CV LAB 1\dog.jpg")
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)                     #BGR 2 RGB for
plotting using matplotlib
h, w = img.shape[:2]

m = int(input("Enter 1 for horizontal, 2 for vertical or 3 for both axes


flipping: "))
if m==1:
    matrix = np.float32([[-1,0,w], [0,1,0], [0,0,1]])         #Creating matrix
for horizontal flipping
    flip = cv2.warpPerspective(img, matrix, (w,h))
elif m==2:
    matrix = np.float32([[1,0,0], [0,-1,h], [0,0,1]])         #Creating matrix
for vertical flipping
    flip = cv2.warpPerspective(img, matrix, (w,h))
elif m==3:
    matrix = np.float32([[-1,0,w], [0,-1,h], [0,0,1]])        #Creating matrix
for flipping both axes
    flip = cv2.warpPerspective(img, matrix, (w,h))

plt.imshow(img)
Bansilal Ramnath Agarwal Charitable Trust’s
Vishwakarma Institute of Technology, Pune-37
(An Autonomous Institute Affiliated to Savitribai Pune University)

Department of Electronics & Telecommunication Engineering

plt.show()
plt.imshow(flip)
plt.show()

# Shearing

# reads an input image


img =
cv2.imread(r"F:\CV
LAB 1\dog.jpg")
img =
cv2.cvtColor(img,
cv2.COLOR_BGR2RGB)  
#BGR 2 RGB for
plotting using
matplotlib
h , w = img.shape[:2]

m = int(input("Enter 1
for horizontal and 2
for vertical shear:
"))

if m==1:
    k = float(input("Enter shearing factor: "))
    sh_matrix = np.float32([[1,k,0], [0,1,0], [0,0,1]])                
#Creating shearing matrix for given factor
    sheared = cv2.warpPerspective(img, sh_matrix, (int(w*(k+1)), h))    
#Increasing width by the same factor

if m==2:
    k = float(input("Enter shearing factor: "))
    sh_matrix = np.float32([[1,0,0], [k,1,0], [0,0,1]])
    sheared = cv2.warpPerspective(img, sh_matrix, (w, int(h*(k+1))))    
#Increasing height by the same factor

plt.imshow(img)
plt.show()
Bansilal Ramnath Agarwal Charitable Trust’s
Vishwakarma Institute of Technology, Pune-37
(An Autonomous Institute Affiliated to Savitribai Pune University)

Department of Electronics & Telecommunication Engineering

plt.imshow(sheared)
plt.show()
Bansilal Ramnath Agarwal Charitable Trust’s
Vishwakarma Institute of Technology, Pune-37
(An Autonomous Institute Affiliated to Savitribai Pune University)

Department of Electronics & Telecommunication Engineering

Conclusion:
The experiment explored essential image processing techniques like translation, scaling,
shearing, reflection, rotation, and cropping. These transformations enabled us to manipulate and
analyze images, gaining valuable insights into their impact on appearance and structure.

You might also like