Exp 04 Dip Lab
Exp 04 Dip Lab
Aim:
(i) To study and perform image translation operation
(ii) To study and perform image rotation operation
(iii) To study and perform image shearing operation Software Required: Google
colaboratory.
Theory:
(i) Translation Image translation refers to the rectilinear shift of an object i.e. an image
from one location to another. If we know the amount of shift in horizontal and the vertical
direction, say (t x, ty) then we can make a transformation matrix e.g. where tx denotes
the shift along the x-axis and ty denotes shift along the y-axis i.e. the number of pixels by
which we need to shift about in that direction. Now, we can use the cv2.wrapAffine()
function to implement these translations. This function requires a 2×3 array. The Numpy
array should be of float type.
(ii) Rotation Images can be rotated to any degree clockwise or otherwise. We just need
to define rotation matrix listing rotation point, degree of rotation and the scaling factor. •
The cv2.getRotationMatrix2D() function is used to create a rotation matrix for an image.
It takes the following arguments:
• The center of rotation for the image.
• The angle of rotation in degrees.
• The scale factor.
(iii) Image Shearing The shear() function is an inbuilt function in the Python Wand Image
Magick library which is used to slide one edge of an image along the X or Y axis to create
a parallelogram. The X-direction shear slides an edge along the X-axis, while a Y direction
shear slides an edge along the Y-axis. The shear angle is used to set shear of the image.
Shearing deals with changing the shape and size of the 2D object along x-axis and y-
axis. It is similar to sliding the layers in one direction to change the shape of the 2D object.
It is an ideal technique to change the shape of an existing object in a two dimensional
plane. In a two dimensional plane, the object size can be changed along X direction as
well as Y direction.
import cv2
import matplotlib.pyplot as plt
from PIL import Image
import numpy as np
img1=cv2.imread("/content/Arslan img.jpg")
img1=cv2.resize(img1,(224,224))
H,W=img1.shape[:2]
h_quarter,w_quarter=H/4,W/4;
T=np.float32([[1,0,h_quarter],[0,1,w_quarter]])
trans_img=cv2.warpAffine(img1,T,(H,W))
fig=plt.figure(figsize=(10,7))
fig.add_subplot(1,2,1)
plt.imshow(img1)
plt.axis("off")
plt.title("Original Image")
fig.add_subplot(1,2,2)
plt.imshow(trans_img)
plt.axis("off")
plt.title("translated Image")
2.Rotation
import cv2
import matplotlib.pyplot as plt
from PIL import Image
import numpy as np
img2=cv2.imread("/content/mypickota.jpg")
img2=cv2.resize(img2,(224,224))
H,W=img2.shape[:2]
h_half,w_half=H/2,W/2
rot_img=cv2.warpAffine(img2,cv2.getRotationMatrix2D((h_half,w_half),60,.7),(H,W))
fig=plt.figure(figsize=(10,7))
fig.add_subplot(1,2,1)
plt.imshow(img2)
plt.axis("off")
plt.title("Original Image")
fig.add_subplot(1,2,2)
plt.imshow(rot_img)
plt.axis("off")
plt.title("Rotated Image")
3. Shearing
import numpy as np
import matplotlib.pyplot as plt
import cv2
img3 = cv2.imread('/content/Arslan img.jpg')
img3=cv2.resize(img3,(224,224))
H, W = img3.shape[:2]
T = np.float32([[1, .5, 0], [0.5, 1, 0], [0, 0, 1]])
sheared_img = cv2.warpPerspective(img3, T, (int(H*1.5), int(W*1.5)))
fig=plt.figure(figsize=(10, 3))
fig.add_subplot(1,2,1)
plt.imshow(img3)
plt.axis("off")
plt.title("Original image")
fig.add_subplot(1,2,2)
plt.imshow(sheared_img)
plt.axis("off")
plt.title("Sheared image")
Result: We have performed and varified the experiment of Translation, Rotation and
Searing of Image on google colab.
1. Searing along Y-axis