探索奇异值分解(SVD)对于图像的影响

探索奇异值分解(SVD)对于图像的影响

参考https://www.cnblogs.com/endlesscoding/p/10033527.html

图像中比较基础的一种方法:利用矩阵的奇异值分解方法,可以用更少、更大的特征值与特征矩阵做运算代替这个矩阵,从而代替这幅图像,完成图像的压缩过程。特征值的大小对应着信息量,那么选择最大一组特征值,就可以相当程度的去代替、压缩图像。

import cv2

import matplotlib.pyplot as plt # plt 用于显示图片
import matplotlib.image as mpimg # mpimg 用于读取图片
 
import sys
import numpy as np
import glob
img_eg = cv2.imread("imagenew/camL/left01.jpg")
plt.imshow(imgL)
print(img_eg.shape)
weight,height = img_eg.shape[0:2]
print(weight,height)
(480, 640, 3)
480 640

output_2_1

img_temp = img_eg.reshape(weight, height * 3)
U,Sigma,VT = np.linalg.svd(img_temp)
# 取前60个奇异值
sval_nums = 4
img_restruct1 = (U[:,0:sval_nums]).dot(np.diag(Sigma[0:sval_nums])).dot(VT[0:sval_nums,:])
img_restruct1 = img_restruct1.reshape(weight, height,3)
 
# 取前120个奇异值
sval_nums = 120
img_restruct2 = (U[:,0:sval_nums]).dot(np.diag(Sigma[0:sval_nums])).dot(VT[0:sval_nums,:])
img_restruct2 = img_restruct2.reshape(weight, height,3)
fig, ax = plt.subplots(1,3,figsize = (24,32))
 
ax[0].imshow(img_eg)
ax[0].set(title = "src")
ax[1].imshow(img_restruct1.astype(np.uint8))
ax[1].set(title = "nums of sigma = 30")
ax[2].imshow(img_restruct2.astype(np.uint8))
ax[2].set(title = "nums of sigma = 120")

从这里看到:仅用4个特征值便可以有效取代这张图片了。

[Text(0.5, 1.0, 'nums of sigma = 120')]

output_5_1

Sigma.shape
plt.plot(Sigma)

output_7_1

猜你喜欢

转载自blog.csdn.net/chenshiming1995/article/details/106550702