Spaces:
Build error
Build error
Commit
·
e4670a9
1
Parent(s):
a453d44
commited
Browse files- SVD.png +0 -0
- app.py +138 -0
- discription.md +14 -0
- requirements.txt +5 -0
SVD.png
ADDED
![]() |
app.py
ADDED
@@ -0,0 +1,138 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import numpy as np
|
2 |
+
import matplotlib.pyplot as plt
|
3 |
+
import math
|
4 |
+
A = np.array([[3, 0], [4, 5]])
|
5 |
+
# A[1][1] = user_input_matrix()[1][1]
|
6 |
+
axis_limit = [-8,8]
|
7 |
+
|
8 |
+
theta = np.linspace(0, 2*np.pi, 100)
|
9 |
+
x = np.cos(theta)
|
10 |
+
y = np.sin(theta)
|
11 |
+
circle = np.vstack((x, y))
|
12 |
+
|
13 |
+
# Perform SVD on the scaling matrix
|
14 |
+
U, s, Vt = np.linalg.svd(A)
|
15 |
+
horizontal_scale_factor,vertical_scale_factor = s[0], s[1]
|
16 |
+
clockwise_angle = math.degrees(math.acos(Vt[0][0]))
|
17 |
+
anticlockwise_angle = math.degrees(math.acos(U[0][0]))
|
18 |
+
s = np.diag(s)
|
19 |
+
|
20 |
+
# Stage 1>>>>> Clockwise rotation
|
21 |
+
right_rot = Vt @ circle
|
22 |
+
|
23 |
+
# Stage 2 >>>>> Horizontal and vertical Scaling
|
24 |
+
scale_right_rot = s @ right_rot
|
25 |
+
|
26 |
+
#Stage 3 >>>>> Anticlock wise rotation
|
27 |
+
ellipse = U @ scale_right_rot
|
28 |
+
|
29 |
+
|
30 |
+
|
31 |
+
fig, ((ax1, ax2),(ax3,ax4)) = plt.subplots(2, 2, figsize=(15, 15), subplot_kw={'aspect': 'equal'})
|
32 |
+
fig.suptitle('Transformation using SVD')
|
33 |
+
|
34 |
+
def circle_plot():
|
35 |
+
|
36 |
+
|
37 |
+
# Plot circle
|
38 |
+
ax1.plot(circle[0],circle[1])
|
39 |
+
ax1.set_xlim(axis_limit)
|
40 |
+
ax1.set_ylim(axis_limit)
|
41 |
+
ax1.set_title('Stage-1-Original Circle')
|
42 |
+
|
43 |
+
# Indicating point P
|
44 |
+
x = circle[0, 30] # x-coordinate of the point
|
45 |
+
y = circle[1, 30] # y-coordinate of the point
|
46 |
+
ax1.plot(x, y, 'ro') # plot the point
|
47 |
+
ax1.text(x+0.05, y+0.05, 'P') # add the label
|
48 |
+
# ax1.axis('equal')
|
49 |
+
|
50 |
+
# Indicating Point Q
|
51 |
+
x = circle[0, 50] # x-coordinate of the point
|
52 |
+
y = circle[1, 50] # y-coordinate of the point
|
53 |
+
ax1.plot(x, y, 'o',color = 'green') # plot the point
|
54 |
+
ax1.text(x+0.1, y+0.1, 'Q') # add the label
|
55 |
+
plt.show()
|
56 |
+
|
57 |
+
|
58 |
+
|
59 |
+
def stage1():
|
60 |
+
# Stage 1>>>> Plot clockwise rotated circle
|
61 |
+
ax2.plot(right_rot[0],right_rot[1])
|
62 |
+
ax2.set_xlim(axis_limit)
|
63 |
+
ax2.set_ylim(axis_limit)
|
64 |
+
# ax2.axis('equal')
|
65 |
+
ax2.set_title('Stage-2 Clockwise rotation')
|
66 |
+
ax2.text(axis_limit[0]+0.4, axis_limit[1]-1, f'Clockwise rotation Angle(in degree) = {clockwise_angle}')
|
67 |
+
|
68 |
+
# Corresponding point P
|
69 |
+
x = right_rot[0, 30]
|
70 |
+
y = right_rot[1, 30]
|
71 |
+
ax2.plot(x, y, 'ro')
|
72 |
+
ax2.text(x+0.05, y+0.05, 'P')
|
73 |
+
# ax1.axis('equal')
|
74 |
+
|
75 |
+
# Corresponding point Q
|
76 |
+
x = right_rot[0, 50]
|
77 |
+
y = right_rot[1, 50]
|
78 |
+
ax2.plot(x, y, 'o',color = 'green')
|
79 |
+
ax2.text(x+0.1, y+0.1, 'Q')
|
80 |
+
plt.show()
|
81 |
+
|
82 |
+
|
83 |
+
|
84 |
+
def stage2():
|
85 |
+
# Stage 2>>>> Horizontal/Verticale scaling
|
86 |
+
ax3.plot(scale_right_rot[0],scale_right_rot[1])
|
87 |
+
ax3.set_xlim(axis_limit)
|
88 |
+
ax3.set_ylim(axis_limit)
|
89 |
+
# ax3.axis('equal')
|
90 |
+
ax3.set_title('Stage-3 Scaling')
|
91 |
+
ax3.text(axis_limit[0]+0.4, axis_limit[1]-1, f'Horizotal Scale factor = { round(horizontal_scale_factor, 2)}')
|
92 |
+
ax3.text(axis_limit[0]+0.4, axis_limit[1]-2, f'Vertical Scale factor = { round(vertical_scale_factor, 2)}')
|
93 |
+
|
94 |
+
# # Corresponding point P
|
95 |
+
x = scale_right_rot[0, 30]
|
96 |
+
y = scale_right_rot[1, 30]
|
97 |
+
ax3.plot(x, y, 'ro')
|
98 |
+
ax3.text(x+0.05, y+0.05, 'P')
|
99 |
+
# ax1.axis('equal')
|
100 |
+
|
101 |
+
# Corresponding point Q
|
102 |
+
x = scale_right_rot[0, 50]
|
103 |
+
y = scale_right_rot[1, 50]
|
104 |
+
ax3.plot(x, y, 'o',color = 'green')
|
105 |
+
ax3.text(x+0.1, y+0.1, 'Q')
|
106 |
+
plt.show()
|
107 |
+
|
108 |
+
|
109 |
+
|
110 |
+
def stage3():
|
111 |
+
# Stage 3>>>>>> Anticlockwise rotation
|
112 |
+
ax4.plot(ellipse[0], ellipse[1])
|
113 |
+
ax4.set_xlim(axis_limit)
|
114 |
+
ax4.set_ylim(axis_limit)
|
115 |
+
# ax4.axis('equal')
|
116 |
+
ax4.set_title('Stage-4 Anticlockwise rotation')
|
117 |
+
ax4.text(axis_limit[0]+0.4, axis_limit[1]-1, f'Anti Clockwise rotation Angle(in degree) = {round(anticlockwise_angle, 2) }')
|
118 |
+
# ax4.ylim(-2,2)
|
119 |
+
|
120 |
+
|
121 |
+
# Corresponding point P
|
122 |
+
x = ellipse[0, 30]
|
123 |
+
y = ellipse[1, 30]
|
124 |
+
ax4.plot(x, y, 'ro')
|
125 |
+
ax4.text(x+0.05, y+0.05, 'P')
|
126 |
+
# ax1.axis('equal')
|
127 |
+
|
128 |
+
# Corresponding point Q
|
129 |
+
x = ellipse[0, 50]
|
130 |
+
y = ellipse[1, 50]
|
131 |
+
ax4.plot(x, y, 'o',color = 'tab:green')
|
132 |
+
ax4.text(x+0.1, y+0.1, 'Q')
|
133 |
+
plt.show()
|
134 |
+
|
135 |
+
# circle_plot()
|
136 |
+
# stage1()
|
137 |
+
# stage2()
|
138 |
+
# stage3()
|
discription.md
ADDED
@@ -0,0 +1,14 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
Image transformation using SVD (Singular Value Decomposition) is a technique for compressing and manipulating images. SVD is a matrix factorization method that factorizes a matrix into three matrices, including a diagonal matrix, a left singular vector matrix, and a right singular vector matrix.
|
2 |
+
|
3 |
+
Given a matrix A, the SVD is a factorization of the form:
|
4 |
+
|
5 |
+
## A = U * S * V^T
|
6 |
+
<br>
|
7 |
+
where U and V are orthogonal matrices, and S is a diagonal matrix of singular values.
|
8 |
+

|
9 |
+
<br>
|
10 |
+
U is called anti-clockwise rotation matrix
|
11 |
+
S is scaling matrix. First element is horizontal scaling factor and the second image is vertical scaling factor.
|
12 |
+
V^T is clockwise totation matrix.
|
13 |
+
|
14 |
+
On matrix multiplication of this matrices with a 2D image will result in corresponding transformation.
|
requirements.txt
ADDED
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
numpy
|
2 |
+
pandas
|
3 |
+
requests
|
4 |
+
matplotlib
|
5 |
+
math
|