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');
[rows, columns, numberOfColorChannels] = size(hostimage);
if numberOfColorChannels > 1
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_SVD, MSE_SVD] = psnr(hostimage, compressed_image)
imwrite(compressed_image, 'lena_svd.jpg');
subplot(1, 2, 1);
imshow(hostimage, []);
title('Host Image', 'FontSize', 20);
subplot(1, 2, 2);
imshow(compressed_image, [])
title('Compressed Image', 'FontSize', 20);
end
Best Answer