Ronakk Exp1
Ronakk Exp1
COURSE NAME: Image Analysis and Computer Vision Laboratory CLASS: IT-I2
EXPERIMENT NO. 1
CO/LO: Describe the fundamentals of computing on images.
AIM / OBJECTIVE:
To perform Geometric Transformations using OpenCV/Pillow Library.
DESCRIPTION OF EXPERIMENT:
Perform following geometric transformations on any Image for using PIL/OpenCV:
1. Read Image
2. Perform the following operations individually:
a. Scaling
b. Translation
c. Rotation
3. Perform composite transformations on an image (scale then rotate by an angle)
QUESTIONS:
• Describe any application where geometric transformations can be used.
SOURCE CODE:
import numpy as np
def translate(points, tx, ty):
translation_matrix = np.array([[1, 0, tx],
[0, 1, ty],
[0, 0, 1]])
# Append ones to the points to convert them to homogeneous coordinates
homogeneous_points = np.column_stack((points, np.ones(len(points))))
translated_points = np.dot(translation_matrix, homogeneous_points.T).T
return translated_points[:, :-1]
def rotate(points, angle):
angle_rad = np.radians(angle)
rotation_matrix = np.array([[np.cos(angle_rad), -np.sin(angle_rad), 0],
[np.sin(angle_rad), np.cos(angle_rad), 0],
Academic Year 2023-24 SAP ID: 60003210210
[0, 0, 1]])
homogeneous_points = np.column_stack((points,
np.ones(len(points)))) rotated_points =
np.dot(rotation_matrix, homogeneous_points.T).T return
rotated_points[:, :-1]
def scale(points, sx,
sy):
scaling_matrix = np.array([[sx, 0, 0],
[0, sy, 0], [0, 0, 1]])
homogeneous_points = np.column_stack((points,
np.ones(len(points)))) scaled_points = np.dot(scaling_matrix,
homogeneous_points.T).T return scaled_points[:, :-1]
original_points =
np.array([[1, 1],
[2, 2],
[3, 1]])
translated_points = translate(original_points,
2, 3)
rotated_points =
rotate(original_points, 45)
scaled_points = scale(original_points,
2, 0.5)
print("Original Points:")
print(original_points)
print("\nTranslated
Points:")
print(translated_points)
print("\nRotated Points:")
print(rotated_points)
print("\nScaled Points:")
print(scaled_points)
OUTPUT:
Original Points:
[[1 1]
[2 2]
[3 1]]
Translated Points:
[[3. 4.]
[4. 5.]
[5. 4.]]
Rotated Points:
[[0. 1.41421356]
[0. 2.82842712]
[1.41421356 2.82842712]] Scaled Points:
Academic Year 2023-24 SAP ID: 60003210210
[[2.
0.5]
[4. 1. ]
[6. 0.5]]
COMPOSITE CODE
import cv2 import numpy as
np import matplotlib.pyplot
as plt
image = cv2.imread("E:\\DRIVE D\\SOHAM ENG COLL\\ENGINEERING
NOTES\\Sem 6\\IACV\\Practicals\\Exp1\\img1.png")
height, width =
image.shape[:2]
angle =
30 scale =
0.5 tx =
50 ty =
30
rotation_matrix = cv2.getRotationMatrix2D((width / 2, height / 2),
angle, scale)
plt.figure(figsize=(15, 5)) plt.subplot(131),
plt.imshow(cv2.cvtColor(image, cv2.COLOR_BGR2RGB)),
plt.title('Original Image')
rotated_image = cv2.warpAffine(image, rotation_matrix, (width,
height)) plt.subplot(132), plt.imshow(cv2.cvtColor(rotated_image,
cv2.COLOR_BGR2RGB)), plt.title('Rotated Image')
scaled_image = cv2.resize(image, None, fx=scale,
fy=scale)
plt.subplot(133), plt.imshow(cv2.cvtColor(scaled_image,
cv2.COLOR_BGR2RGB)), plt.title('Scaled Image')
rotation_matrix[0, 2]
+= tx rotation_matrix[1,
2] += ty
transformed_image = cv2.warpAffine(image, rotation_matrix, (width,
height)) plt.figure(figsize=(15, 5)) plt.subplot(131),
plt.imshow(cv2.cvtColor(transformed_image, cv2.COLOR_BGR2RGB)),
plt.title('Rotated and Translated Image')
scaled_transformed_image = cv2.resize(transformed_image, None, fx=scale,
fy=scale)
Academic Year 2023-24 SAP ID: 60003210210
plt.subplot(132), plt.imshow(cv2.cvtColor(scaled_transformed_image,
cv2.COLOR_BGR2RGB)), plt.title('Rotated, Translated, and Scaled Image')
plt.show()
OUTPUT:-
REFERENCES:
Website References:
Academic Year 2023-24 SAP ID: 60003210210
[1].
Gonzalez, Rafael C., and Richard E. Woods. “Digital image processing” (2017).
[2]. Felix Liu, “Understanding Transformations in Computer Vision: An Overview of Affine
Transformations and Homographies”. Available at: https://towardsdatascience.com/understanding-
transformations-in-computer-vision-b001f49a9e61
Academic Year 2023-24 SAP ID: 60003210210
[3].
Image Processing in Python (Scaling, Rotating, Shifting and Edge Detection) Available at:
https://www.geeksforgeeks.org/image-processing-in-python-scaling-rotating-shifting-and-edge-
detection/?ref=lbp
[4]. Geometric Transformations of Images, Available at:
https://docs.opencv.org/3.4/da/d6e/tutorial_py_geometric_transformations.html
[5]. https://docs.scipy.org/doc/scipy/reference/generated/scipy.ndimage.affine_transform.html
[6]. https://homepages.inf.ed.ac.uk/rbf/HIPR2/affine.htm