Page 1 of 1

PSNR no longer returns inf

Posted: 2017-02-26T19:16:19-07:00
by dksayers
The result of a 'compare -metric PSNR' for identical images has changed between
6.9.3 and 6.9.5 (I dont have a 6.9.4 to test). This problem is also in the
7 series. Instead of the result being 'inf' it has now become '0'. Is this a bug or done on purpose???

> compare --version
Version: ImageMagick 6.9.3-4 Q16 amd64 2016-03-15 http://www.imagemagick.org
Copyright: Copyright (C) 1999-2016 ImageMagick Studio LLC
License: http://www.imagemagick.org/script/license.php
Features: Cipher DPC Modules
Delegates (built-in): bzlib cairo fftw fontconfig freetype fpx jbig jng jp2 jpeg lcms lqr ltdl png rsvg tiff webp wmf x xml zlib


> convert -size 100x100 xc:white canvaswhite.jpg
> compare -metric PSNR canvaswhite.jpg canvaswhite.jpg /dev/null
inf

-----

> compare --version
Version: ImageMagick 6.9.5-10 Q16 amd64 2016-11-01 http://www.imagemagick.org
Copyright: Copyright (C) 1999-2016 ImageMagick Studio LLC
License: http://www.imagemagick.org/script/license.php
Features: Cipher DPC Modules
Delegates (built-in): bzlib cairo fftw fontconfig fpx freetype jbig jng jp2 jpeg lcms lqr ltdl lzma png rsvg tiff webp wmf x xml zlib


> convert -size 100x100 xc:white canvaswhite.jpg
> compare -metric PSNR canvaswhite.jpg canvaswhite.jpg /dev/null
0

-----
> compare --version
Version: ImageMagick 7.0.2-9 Q16 amd64 2017-02-26 http://www.imagemagick.org
Copyright: Copyright (C) 1999-2016 ImageMagick Studio LLC
License: http://www.imagemagick.org/script/license.php
Features: Cipher DPC HDRI Modules
Delegates (built-in): bzlib cairo fftw fontconfig fpx freetype jbig jng jp2 jpeg lcms lqr ltdl lzma png rsvg tiff webp wmf x xml zlib

> convert -size 100x100 xc:white canvaswhite.jpg
> compare -metric PSNR canvaswhite.jpg canvaswhite.jpg /dev/null
0

Re: PSNR no longer returns inf

Posted: 2017-02-26T20:46:46-07:00
by fmw42
It seems to have changed for me between 6.9.5.3 (inf) and 6.9.5.4 (0)

Possibly related to this entry in the changelog:
2016-07-35 6.9.5-4 Cristy <quetzlzacatenango@image...>
Prevent buffer overflow (bug report from Ibrahim el-sayed).

Re: PSNR no longer returns inf

Posted: 2017-02-27T05:32:41-07:00
by magick
We corrected for a possible divide by zero:

Code: Select all

   distortion[RedChannel]=20.0*MagickLog10((double) 1.0/sqrt(distortion[RedChannel]));
If distortion[channel] is zero, we no longer divide by zero and instead return zero. If our analysis / patch is incorrect, let us know.

Re: PSNR no longer returns inf

Posted: 2017-02-27T16:51:57-07:00
by fmw42
From http://www.imagemagick.org/Usage/compare/#statistics
PSNR .... Peak Signal to noise ratio (used in image compression papers)
The ratio of mean square difference to the maximum mean square
that can exist between any two images, expressed as a decibel
value.

The higher the PSNR the closer the closer the images are, with
a maximum difference occurring at 1. A PSNR of 20 means
differences are 1/100 of maximum
Since the mean squared difference is zero and the maximum mean squared difference is not 0, then the ratio is 0 and the log(0) is inf. So properly this metric should be returning inf.

Re: PSNR no longer returns inf

Posted: 2017-02-27T17:56:59-07:00
by magick
If we use an infinitesimal small value instead of 0, it returns 300 rather than inf. This avoids the divide by zero and returns a maximal PSNR. We can't properly perform a divide by zero so the alternatively we could check for 0 and if found return inf otherwise return 20.0*MagickLog10(1/sqrt(distortion[channel])). What's your preference?

Re: PSNR no longer returns inf

Posted: 2017-02-27T18:03:31-07:00
by fmw42
alternatively we could check for 0 and if found return inf

Re: PSNR no longer returns inf

Posted: 2017-02-27T18:47:48-07:00
by magick
Thanks for the problem report. We can reproduce it and will have a patch to fix it in GIT master branch @ https://github.com/ImageMagick/ImageMagick later today. The patch will be available in the beta releases of ImageMagick @ http://www.imagemagick.org/download/beta/ by sometime tomorrow.