1

I want to find out PSNR and SNR values of two images A and B of same dimension in Matlab.I used the following code

[peaksnr, snr] = psnr(A,B)

and getting an error

Undefined function 'psnr' for input arguments of type 'uint8'.

Then I converted both the images into double as follows

A = double(A);
B = double(B);  

and again PSNR is calculated.Now I'm getting an error

Undefined function 'psnr' for input arguments of type 'double'.

Why this is happening?Please help me to troubleshoot the error.

Celine
  • 115
  • 1
  • 5
  • 13

3 Answers3

4
         function PSNR = psnr(distImg, origImg)

            origImg = double(origImg);
            distImg = double(distImg);

            [M N] = size(origImg);
            error = origImg - distImg;
            MSE = sum(sum(error .* error)) / (M * N);

            if(MSE > 0)
            PSNR = 10*log(255*255/MSE) / log(10);
            else
            PSNR = 99;
            end 

        end
omid
  • 400
  • 3
  • 19
  • Why do you assign a `PSNR` of 99 if the MSE is negative? By definition, the mean **squared** error will never be negative. Also the `log(10)` division is incorrect. Consult the definition of the PSNR here: http://en.wikipedia.org/wiki/Peak_signal-to-noise_ratio – rayryeng Oct 23 '14 at 20:13
  • Hi in matlab when error occurs MSE is negative thus PSNR=99 is maximum. Also base of log function in matlab is 2 and this formula is correct.Because log(a)/log(b)=log(a) in base b. – omid Nov 16 '14 at 08:51
  • Ah yes, ok. What you should do is use `log10`. It avoids having to divide in order to get it into the right base. However, the MSE will **NEVER** be negative. Sum of squared errors was created to circumvent any negative deviation, so that I don't agree with. The arbitrary 99 is not correct either IMHO. I looked at the PSNR source in MATLAB, and there is no -99 assignment anywhere. – rayryeng Nov 16 '14 at 16:58
  • BTW the base of the log function is **e**, not 2. `log2` is the function that is base 2. Please read up about MATLAB internals before answering any other questions. – rayryeng Nov 17 '14 at 15:03
0
psnr = 10*log10(255/sqrt(mean((image1(:) - image2(:)).^2)))
CubeJockey
  • 2,209
  • 8
  • 24
  • 31
Marco
  • 1
  • 1
0

There is some mistakes with above code where it should consider log10 in Matlab. So the correct code (even for color image) is as below:

function PSNR = MyPsnr(distImg, origImg)
        origImg = double(origImg);
        distImg = double(distImg);

        [M N P] = size(origImg);
        error = origImg - distImg;
        MSE = sum(sum(sum(error.^2))) / (M * N * P);

        if(MSE > 0)
            PSNR = 20*log10(max(max(max(origImg))))-10*log10(MSE);
        else
            PSNR = 99;
        end    
Alex
  • 26
  • 3