PSNR no longer returns inf

Post any defects you find in the released or beta versions of the ImageMagick software here. Include the ImageMagick version, OS, and any command-line required to reproduce the problem. Got a patch for a bug? Post it here.
Post Reply
dksayers
Posts: 3
Joined: 2015-04-22T22:20:08-07:00
Authentication code: 6789

PSNR no longer returns inf

Post 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
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: PSNR no longer returns inf

Post 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).
User avatar
magick
Site Admin
Posts: 11064
Joined: 2003-05-31T11:32:55-07:00

Re: PSNR no longer returns inf

Post 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.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: PSNR no longer returns inf

Post 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.
User avatar
magick
Site Admin
Posts: 11064
Joined: 2003-05-31T11:32:55-07:00

Re: PSNR no longer returns inf

Post 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?
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: PSNR no longer returns inf

Post by fmw42 »

alternatively we could check for 0 and if found return inf
User avatar
magick
Site Admin
Posts: 11064
Joined: 2003-05-31T11:32:55-07:00

Re: PSNR no longer returns inf

Post 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.
Post Reply