【线代&NumPy】第十二章 - 奇异值课后练习(附加代码记录)
2023-09-14 09:15:59 时间
import numpy as np
import matplotlib.pyplot as plt
import imageio as im
src = im.imread('flower3.jpg')
A = src[:,:,0]
def reconstruct(U, Sigma, VT, n_elements):
Sigma = Sigma[:, :n_elements]
VT = VT[:n_elements, :]
B = np.matmul(U, np.matmul(Sigma, VT))
return B
plt.rcParams.update({'xtick.major.width': 0,
'xtick.labelsize': 0,
'ytick.major.width': 0,
'ytick.labelsize': 0,
'axes.linewidth': 0})
U, s, VT = np.linalg.svd(A)
m, n = A.shape
Sigma = np.zeros((m, n))
k = np.size(s)
Sigma[:k, :k] = np.diag(s)
plt.subplot(3,3,1)
plt.title('original')
plt.imshow(A)
plt.subplot(3,3,2)
plt.title('rank 1')
B = reconstruct(U, Sigma, VT, 1)
plt.imshow(B, cmap = 'gray')
plt.subplot(3,3,3)
plt.title('upto rank 5')
B = reconstruct(U, Sigma, VT, 5)
plt.imshow(B, cmap = 'gray')
plt.subplot(3,3,4)
plt.title('upto rank 20')
B = reconstruct(U, Sigma, VT, 20)
plt.imshow(B, cmap = 'gray')
plt.subplot(3,3,5)
plt.title('upto rank 30')
B = reconstruct(U, Sigma, VT, 30)
plt.imshow(B, cmap = 'gray')
plt.subplot(3,3,6)
plt.title('upto rank 50')
B = reconstruct(U, Sigma, VT, 50)
plt.imshow(B, cmap = 'gray')
plt.subplot(3,3,7)
plt.title('upto rank 100')
B = reconstruct(U, Sigma, VT, 100)
plt.imshow(B, cmap = 'gray')
plt.subplot(3,3,8)
plt.title('upto rank 150')
B = reconstruct(U, Sigma, VT, 150)
plt.imshow(B, cmap = 'gray')
plt.subplot(3,3,9)
plt.title('upto rank 200')
B = reconstruct(U, Sigma, VT, 200)
plt.imshow(B, cmap = 'gray')
plt.show()
import cv2
import numpy as np
import matplotlib.pyplot as plt
def compute_xform(corners1, corners2):
A = [ ]
for i in range(4):
x1, y1 = corners1[i]
x2, y2 = corners2[i]
A.append([x1, y1, 1, 0, 0, 0, -x2*x1, -x2*y1, -x2])
A.append([0, 0, 0, x1, y1, 1, -y2*x1, -y2*y1, -y2])
A = np.asarray(A)
U, S, V = np.linalg.svd(A)
xform = V[-1, :]
xform = np.reshape(xform, (3, 3))
return xform
def transform_image(xform, image1, corners1, corners2):
warped = cv2.warpPerspective(image1, xform, (500, 500))
h1, w1 = image1.shape[:2]
h2, w2 = warped.shape[:2]
out_image = np.zeros((max(h1, h2), w1+w2, 3), dtype=np.uint8)
out_image[:h1, :w1, :3] = image1
out_image[:h2, w1:w1+w2, :3] = warped
for i in range(4):
c1 = corners1[i]
c2 = (corners2[i][0] + w1, corners2[i][1])
cv2.circle(out_image, c1, radius = 2, color = (0, 255, 0),
thickness = 2)
cv2.circle(out_image, c2, radius = 2, color = (30, 255, 255),
thickness = 2)
cv2.line(out_image, c1, c2, color = (0, 0, 255), thickness = 1)
cv2.imshow('original and transformed images', out_image)
cv2.waitKey(0)
def main():
img_path1 = 'cameraman1.png'
img1 = cv2.imread(img_path1, cv2.IMREAD_COLOR)
corners1 = [(122, 51), (26, 300), (454, 131), (330, 414)]
corners2 = [(50, 50), (50, 450), (450, 50), (450, 450)]
xform = compute_xform(corners1, corners2)
print(xform)
transform_image(xform, img1, corners1, corners2)
if __name__ == '__main__':
main()
参考文献
Introduction to Linear Algebra, International 4 th Edition by Gilbert Strang, Wellesley Cambridge Press.
百度百科[EB/OL]. []. https://baike.baidu.com/
本篇完。
相关文章
- 大端模式&小端模式 主机序&网络序
- .gitignore 语法 & 注意事项: abc,abc/,/abc, /abc/ , abc/* 和 /abc/* 的区别, 你分得清吗
- & | ^ ~ << >> 按位运算符
- [Angular & Web] Retrieve user data from Session
- select * from table where 1<>1
- 华为OD机试 - 计算数组中心位置(Java & JS & Python)
- 音视频开发(十):GLSurfaceView源码解析&EGL环境
- xmu 1125 越野车大赛(公式推导&&三分)(中等)
- 【Python-Numpy】numpy.expand_dims()的解析与使用
- 已解决mysql shell 中 没有numpy & pandas module
- 【线代&NumPy】第五章 - 行列式课后练习 | 伴随矩阵求逆 | Cramer公式求联立方程 | 简述并提供代码