SVD奇异值分解源码处理图片

本文介绍了一种使用奇异值分解(SVD)进行图像压缩和重构的方法。通过将图像转换为多个特征的线性组合,可以减少存储空间并保持图像质量。文章详细展示了如何通过Python实现这一过程,并提供了完整的代码示例。

简介

用svd将图片转变为多个特征的线性加和

源码

def restore1(sigma,u,v,k):#奇异值,左特征向量,右特征向量
	print k
	m=len(u)
	n=len(v[0])
	a=np.zeros((m,n))
	for k in range(k+1):
		for i in range(m):
			a[i]+=sigma[k]*u[i][k]*v[k]
	b=a.astype('uint8')
	Image.fromarray(b).save("svd_"+str(k)+".png")
A=Image.open("test.png",'r')
print A
output_path=r'.\svd1'
if not os.path.exits(output_path):
	os.mkdir(output_path)
a=np.array(A)
print a.shape
K=50
u_r ,sigma_r,v_r=np.linalg.svd([:,:,0])
u_g ,sigma_g.v_g=np.linalg.svd([:,:,1])
u_b ,sigma_b.v_b=np.linalg.svd([:,:,2])
pit.figure(figsize=(10,10),facecolor='w')
mpl.rcParams['font.sans-serif']=[u'simHei']
mpl.rcParams['axes.unicode_minus']=False
for k in range(1,K+1):
	print k
	R=restore1(sigma_r,u_r,v_r,k)
	G=restore1(sigma_g,u_g,v_g,k)
	B=restore1(sigma_b,u_b,v_b,k)
	I=np.stack((R,G,B).axis=2)
	Image.fromarray(I).save('%s\\svd_%d.png'%(output_path,k))
	if k<=12:
		plt.subplot(3,4,k)
		plt.imshow(I)
		plt.axis('off')
		plt.title(u'奇异值个数:%d'%k)
plt.suptitle(u'SVD与图像分解',fontsize=20)
plt.tight_layout(0.3,rect=(0,0,1,0.92))
plt.show()

在这里插入图片描述

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值