Professional Documents
Culture Documents
Experiment No. 4
Experiment No. 4
Experiment No. 4
Experiment No. 4
Problem Statement:
Geometric Transformations
AIM:
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)
Flowchart:
Bansilal Ramnath Agarwal Charitable Trust’s
Vishwakarma Institute of Technology, Pune-37
(An Autonomous Institute Affiliated to Savitribai Pune University)
Translation
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)
# Rotation
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)
# Scaling
Bansilal Ramnath Agarwal Charitable Trust’s
Vishwakarma Institute of Technology, Pune-37
(An Autonomous Institute Affiliated to Savitribai Pune University)
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]
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)
# Cropping
print(img.shape)
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)
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)
plt.imshow(img)
Bansilal Ramnath Agarwal Charitable Trust’s
Vishwakarma Institute of Technology, Pune-37
(An Autonomous Institute Affiliated to Savitribai Pune University)
plt.show()
plt.imshow(flip)
plt.show()
# Shearing
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)
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)
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.