2000字范文,分享全网优秀范文,学习好帮手!
2000字范文 > svd在matlab中的使用 matlab - 使用SVD在MATLAB中压缩图像 - 堆栈内存溢出

svd在matlab中的使用 matlab - 使用SVD在MATLAB中压缩图像 - 堆栈内存溢出

时间:2023-10-23 05:33:14

相关推荐

svd在matlab中的使用 matlab - 使用SVD在MATLAB中压缩图像 - 堆栈内存溢出

虽然这个问题很老,但它对我理解SVD有很大帮助。 我已修改了您在问题中编写的代码,以使其正常工作。

我相信你可能已经解决了这个问题,但是只是为了访问这个页面的任何人的未来参考,我在这里包含输出图像和图形的完整代码。

以下是代码:

close all

clear all

clc

%reading and converting the image

inImage=imread('fruits.jpg');

inImage=rgb2gray(inImage);

inImageD=double(inImage);

% decomposing the image using singular value decomposition

[U,S,V]=svd(inImageD);

% Using different number of singular values (diagonal of S) to compress and

% reconstruct the image

dispEr = [];

numSVals = [];

for N=5:25:300

% store the singular values in a temporary var

C = S;

% discard the diagonal values not required for compression

C(N+1:end,:)=0;

C(:,N+1:end)=0;

% Construct an Image using the selected singular values

D=U*C*V';

% display and compute error

figure;

buffer = sprintf('Image output using %d singular values', N)

imshow(uint8(D));

title(buffer);

error=sum(sum((inImageD-D).^2));

% store vals for display

dispEr = [dispEr; error];

numSVals = [numSVals; N];

end

% dislay the error graph

figure;

title('Error in compression');

plot(numSVals, dispEr);

grid on

xlabel('Number of Singular Values used');

ylabel('Error between compress and original image');

将其应用于以下图像:

仅使用前5个奇异值给出以下结果,

前30个奇异值,

和前55个奇异值,

随着奇异值数量的增加,误差的变化可以在下图中看到。

在这里您可以注意到图表显示使用大约200个第一个奇异值产生大约零误差。

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。