我是一个Matlab新手,但正在尝试编写一些针对灰度图像的图像压缩代码。
问题
如何使用SVD修剪低值特征值以重构压缩图像?
目前的工作/尝试
到目前为止,我的代码是:
B=imread('images1.jpeg');
B=rgb2gray(B);
doubleB=double(B);
%read the image and store it as matrix B, convert the image to a grayscale
photo and convert the matrix to a class 'double' for values 0-255
[U,S,V]=svd(doubleB);
这使我能够成功地使用存储在变量S中的特征值分解图像矩阵。
如何截断S(大小为167x301,类别为double)?假设我只想取其中前100个(或任何n),我该如何做并重建压缩的图像?
更新的代码/想法
与其在评论部分放置一堆代码,这是我目前拥有的草稿。我已经成功创建了压缩图像,手动更改N的值,但我还想做两件事:
1- 显示各种压缩的图像面板(比如,对于N = 5、10、25等运行一个循环)。
2- 以某种方式计算每个图像与原始图像之间的差异(误差),并将其绘制成图形。
我不擅长理解循环和输出,但这是我尝试过的内容:
B=imread('images1.jpeg');
B=rgb2gray(B);
doubleB=im2double(B);%
%read the image and store it as matrix B, convert the image to a grayscale
%photo and convert the image to a class 'double'
[U,S,V]=svd(doubleB);
C=S;
for N=[5,10,25,50,100]
C(N+1:end,:)=0;
C(:,N+1:end)=0;
D=U*C*V';
%Use singular value decomposition on the image doubleB, create a new matrix
%C (for Compression diagonal) and zero out all entries above N, (which in
%this case is 100). Then construct a new image, D, by using the new
%diagonal matrix C.
imshow(D);
error=C-D;
end
显然存在一些错误,因为我没有得到多张图片,也不知道如何“绘制”误差矩阵。