How to apply SVD to dwt subband
2 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
Afeza Ajirah
il 19 Mar 2017
Commentato: Image Analyst
il 6 Lug 2017
Hi everyone. I am new user to matlab. I found this code from internet, but i cant run the code due some error. The first error encountered was;
Error in SVD_C (line 8) %ERROR [Uh,Sh,Vh]= svd(double(hostimage)); %ERROR
can anyone please debug this code? Thank you
________________________________________________________________
function []= SVD_C() hostimage= imread('lena.bmp'); k=10; % determine size of host image Mc=size(hostimage,1); %Height Nc=size(hostimage,2); %Width
[Uh,Sh,Vh]= svd(double(hostimage));
VhT=transpose(Vh(1:Nc,1:k)); compressed_image= Uh(1:Mc,1:k)*Sh(1:k,1:k)*VhT;
%PSNR
[PSNR_SVD,MSE_SVD]= psnr(hostimage,compressed_image)
imwrite(compressed_image,'lena_svd.jpg','jpg');
figure(1) imshow(hostimage,[]); title('Host Image'); figure(2) imshow(compressed_image,[]) title('Compressed Image');
end
% Function to calculate psnr used above function [p mse]= psnr(original,reconstructed)
Mc= size(original,1); Nc= size(original,2); a1= original(1:Mc,1:Nc); a2= reconstructed(1:Mc,1:Nc);
error=double(a1)-double(a2);
sum=0; for (i=1:Mc) for(j=1:Nc) sum = sum + ((error(i,j))^2); end end
mse = sum/(Mc*Nc); p = 10*log(double((255*255)/mse));
end
Risposta accettata
Image Analyst
il 19 Mar 2017
Two bad things I see right off the bat. You used error and sum as the names for your variables. DON'T DO THAT!!! Those are the names of very important built in functions that you will destroy if you use them.
Why do you have your own psnr function when there is a built in psnr() function? And why, in that function, do you calculate mse when there is a built in function immse()? And why do you compute the MSE (Mean Square Error) but call it SVD (singular value decomposition)?
When I ran your code with a standard demo image:
hostimage= imread('cameraman.tif');
it ran with no errors. I don't know if it did what you want, but at least it ran and finished with no errors.
Finally why did you not give us the entire error message instead of just a snippet from it, omitting the actual error itself???
You aren't putting a color image in for hostimage, are you? I'd bet that you are. Replace your function with this version:
function SVD_C()
hostimage = imread('peppers.png');
% Get the dimensions of the image.
% numberOfColorChannels should be = 1 for a gray scale image, and 3 for an RGB color image.
[rows, columns, numberOfColorChannels] = size(hostimage);
if numberOfColorChannels > 1
% It's not really gray scale like we expected - it's color.
% Use weighted sum of ALL channels to create a gray scale image.
hostimage = rgb2gray(hostimage);
end
k = 10;
[Uh, Sh, Vh] = svd(double(hostimage));
VhT = transpose(Vh(1:columns, 1:k));
compressed_image = Uh(1:rows, 1:k) * Sh(1:k, 1:k) * VhT;
% PSNR
[PSNR_SVD, MSE_SVD] = psnr(hostimage, compressed_image)
% Save to disk.
imwrite(compressed_image, 'lena_svd.jpg');
% Display the images.
subplot(1, 2, 1);
imshow(hostimage, []);
title('Host Image', 'FontSize', 20);
subplot(1, 2, 2);
imshow(compressed_image, [])
title('Compressed Image', 'FontSize', 20);
end
5 Commenti
Più risposte (0)
Vedere anche
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!