Effacer les filtres
Effacer les filtres

Trouble learning loops, sub-plotting, etc. for multiple iterations of SVD for image compression

1 vue (au cours des 30 derniers jours)
I am very new to MATLAB and programming in general, but I am struggling with some concepts regarding multiple images plotted at the same time.
I am trying to read an image into MATLAB, convert it to grayscale, use SVD and zero out the eigenvalues greater than some N, reconstruct the image and graph the difference/error. Thus far, this is what I have:
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=[2,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=mean((doubleB(:)-D(:)).^2);
error;
plot(N,error);
end
As you can probably see I am very new to this, and I am not sure how to do "for" loops very well, even after reading the standard documentation. The desired output I am looking for is a single pannel with 6 images, each one reconstructed for the values of N listed, ideally with a title that shows which example it is, i/e the most out of focus image would be titled N=2, and so on.
Then, I would like to make a simple line plot of the mean squared error (error) and N with appropriate axes so that the curve shows the decrease in error as N increases.
Any help would be greatly appreciated. Self-teaching is not my forte!

Réponses (1)

John Petersen
John Petersen le 29 Nov 2012
Modifié(e) : John Petersen le 29 Nov 2012
N=[2,5,10,25,50,100];
for i = 1:length(N)
C(N(i)+1:end,:)=0;
C(:,N(i)+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);
err(i)=mean((doubleB(:)-D(:)).^2); % error is a reserved word
end
plot(N,err);
  1 commentaire
John Petersen
John Petersen le 29 Nov 2012
Figure out what you need to do for one of the N cases. Once that is right, you can try to continue on with determining an error. Try initializing err
N=[2,5,10,25,50,100];
err = zeros(length(N),1);
for i = 1:length(N) ....

Connectez-vous pour commenter.

Catégories

En savoir plus sur Linear Algebra dans Help Center et File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by