필터 지우기
필터 지우기

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

조회 수: 1 (최근 30일)
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!

답변 (1개)

John Petersen
John Petersen 2012년 11월 29일
편집: John Petersen 2012년 11월 29일
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
John Petersen
John Petersen 2012년 11월 29일
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) ....

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Linear Algebra에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by