0% found this document useful (0 votes)
11 views

A4 solution

Transformation Assignment

Uploaded by

Zulfiqar Ali
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

A4 solution

Transformation Assignment

Uploaded by

Zulfiqar Ali
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Expansion of first figure by a factor of 4 along y-axis.

Reference Image:

Code:

from google.colab import files


uploaded = files.upload()

import numpy as np
import matplotlib.pyplot as plt
import cv2
from google.colab.patches import cv2_imshow

# Read the image from the uploaded file


image_path = list(uploaded.keys())[0]
img = cv2.imread(image_path)

# Resize the image for better visualization


img = cv2.resize(img, (250, 250))

# Get the dimensions of the image


rows, cols, _ = img.shape

# Define points to be marked (example points: top-left, top-right, bottom-left,


bottom-right, and center)
points = np.array([
[0, 0],
[cols - 1, 0],
[0, rows - 1],
[cols - 1, rows - 1],
[cols // 2, rows // 2]
])
# Mark the points on the original image
img_with_points = img.copy()
for point in points:
cv2.circle(img_with_points, tuple(point), 5, (0, 255, 0), -1)

# Transformation matrix for expanding by a factor of 4 along the y-axis


transformation_matrix = np.array([[1, 0, 0], [0, 4, 0]], dtype=np.float32)

# Apply the transformation


transformed_img = cv2.warpAffine(img, transformation_matrix, (cols, rows * 4))

# Calculate new positions of the points after transformation


transformed_points = np.array([
[point[0], point[1] * 4] for point in points
])

# Mark the points on the transformed image


transformed_img_with_points = transformed_img.copy()
for point in transformed_points:
cv2.circle(transformed_img_with_points, tuple(point), 5, (0, 255, 0), -1)

# Display the images with matplotlib


fig, axes = plt.subplots(1, 2, figsize=(15, 10))

# Original image with points


axes[0].imshow(cv2.cvtColor(img_with_points, cv2.COLOR_BGR2RGB))
axes[0].set_title('Original Image with Points')
axes[0].set_xlabel('X')
axes[0].set_ylabel('Y')
axes[0].invert_yaxis() # Invert Y-axis to match Cartesian coordinates
for point in points:
axes[0].plot(point[0], point[1], 'go')
axes[0].text(point[0], point[1], f'({point[0]}, {point[1]})', color='red')

# Transformed image with points


axes[1].imshow(cv2.cvtColor(transformed_img_with_points, cv2.COLOR_BGR2RGB))
axes[1].set_title('Transformed Image (Expanded by 4 along y-axis) with Points')
axes[1].set_xlabel('X')
axes[1].set_ylabel('Y')
axes[1].invert_yaxis() # Invert Y-axis to match Cartesian coordinates
for point in transformed_points:
axes[1].plot(point[0], point[1], 'go')
axes[1].text(point[0], point[1], f'({point[0]}, {point[1]})', color='red')
plt.show()

# Save the transformed image with points


output_path = 'Transformed_Image_with_Points.jpg'
cv2.imwrite(output_path, transformed_img_with_points)
print(f'Transformed image with points saved as {output_path}')

Output:
Clockwise Rotation of second figure about the origin with an angle 120.
Reference Image:

Code:

import matplotlib.pyplot as plt


import numpy as np

# Define the rotation matrix for 120 degrees clockwise


theta = np.radians(-120)
rotation_matrix = np.array([
[np.cos(theta), -np.sin(theta)],
[np.sin(theta), np.cos(theta)]
])

# Coordinates for each shape


shapes = {
"Shape 1": [(-1, 9), (-0.5, 10), (0, 11), (1, 12), (2, 13), (3, 13.5), (3.5,
12.5), (2, 11), (1, 10), (1.5, 8.5), (2, 8), (2, 7.5), (1, 7), (0.5, 7), (0, 7),
(-1, 7.5), (-1.5, 8), (-1, 9)],
"Shape 2": [(1.5, 8.5), (3, 8.5), (6, 8.5), (8, 7.5), (9.5, 6.5), (10.5, 5),
(11, 4), (11.5, 2), (12, 0), (11.5, -3), (10.5, -5), (9, -7), (7, -8.5), (5, -
9.5), (3, -10), (1, -10), (-1.5, -10), (-3.5, -10), (-6, -9), (-8.5, -7), (-10, -
5), (-11.5, -2), (-12, 0), (-11.5, 2), (-11, 4), (-10, 6), (-8, 7.5), (-6, 8.5),
(-4, 9), (-2, 9), (-1, 9)],
"Shape 3": [(2, 8), (5, 7.5), (7, 6.5), (8.5, 5), (9.5, 3), (10, 1), (10.5, -
1), (10, -3), (9, -5.5), (7, -8.5)],
"Shape 4": [(2, 8), (4, 7), (6, 5), (7, 3), (7.5, 0), (7.5, -2)],
"Shape 5": [(7.5, -3.5), (7, -5), (6, -7), (5, -9.5)],
"Shape 6": [(0, -5.5), (0.5, -8), (1, -10)],
"Shape 7": [(2, 7.5), (3, 6.5), (4, 4), (6, 1), (4.5, 1), (1.5, 1), (4, 4)],
"Shape 8": [(4.5, 1), (4.5, -1), (4.5, -3)],
"Shape 9": [(4, -6.5), (4, -8), (3, -10)],
"Shape 10": [(0.5, 7), (0, 5), (0, 0), (-1, -1.5), (0, -1.5), (1.5, -1.5),
(0.5, 0.5), (0, 0)],
"Shape 11": [(0, -1.5), (0, -3.5), (1.5, -3.5), (1.5, -4.5), (3.5, -4.5), (3,
-3), (4.5, -3), (7.5, -2), (8.5, -2), (7.5, -3.5), (6, -5), (5, -6), (4, -6.5),
(1.5, -7), (1, -5.5), (0, -5.5), (-1, -5.5), (-1, -7.5), (-3, -6.5), (-5, -5.5),
(-7.5, -1.5), (-3.5, -3), (-4, -4.5), (-2, -4.5), (-1.5, -3), (0, -3.5)],
"Shape 12": [(-1, 7.5), (-2.5, 6), (-3, 5), (-3.5, 4), (-1.5, 1), (-4, 0.5),
(-6, 0.5), (-3.5, 4)],
"Shape 13": [(-4, 0.5), (-4, -1), (-3.5, -3)],
"Shape 14": [(-3, -6.5), (-2.5, -8), (-1.5, -10)],
"Shape 15": [(-1.5, 8), (-2.5, 8), (-4, 7), (-5.5, 6), (-7, 4), (-8, 2), (-8,
0), (-8, -2), (-7.5, -4), (-7, -5.5), (-5.5, -7.5), (-3.5, -10)],
"Shape 16": [(-2, 9), (-4, 8.5), (-6, 7.5), (-8, 6), (-9.5, 3.5), (-10, 2),
(-10.5, 0), (-10.5, -2), (-9.5, -4.5), (-8.5, -7)]
}

# Function to plot shapes


def plot_shapes(ax, shapes, title):
for shape, points in shapes.items():
points = np.array(points)
ax.plot(points[:, 0], points[:, 1], marker='o')
ax.plot(points[:, 0], points[:, 1], label=shape)
ax.legend()
ax.grid(True)
ax.axhline(0, color='black',linewidth=0.5)
ax.axvline(0, color='black',linewidth=0.5)
ax.set_aspect('equal', adjustable='box')
ax.set_title(title)

# Create the rotated shapes


rotated_shapes = {shape: np.dot(points, rotation_matrix.T) for shape, points in
shapes.items()}

# Plotting the original and rotated shapes


fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(15, 7))

plot_shapes(ax1, shapes, "Original Shapes")


plot_shapes(ax2, rotated_shapes, "Rotated Shapes (120° Clockwise)")

plt.show()

Output:

You might also like