I'm using compare
to diff two similar color PNG files. They get a PSNR value of ~27.
The images contain a lot of white areas that will always match between the two images. Correct me if i'm wrong, but these white areas are increasing the PSNR values (making the images more similar as an average). Therefore I don't want to take white pixels into account, IF they match.
Is there a way to do this using compare
or by factoring in some other tool?
I tried setting -transparent-color
to white, but this had no effect.
Here is my command:
compare -compose src -metric PSNR img1.png img2.png diff.png
Edit:
compare --version
Version: ImageMagick 6.6.9-7 2014-03-06 Q16 http://www.imagemagick.org
convert --version
Version: ImageMagick 6.6.9-7 2014-03-06 Q16 http://www.imagemagick.org
My idea about ignoring white pixels may be rubbish, but I still want to achieve a better comparison.
To clarify:
Imagine this situation: you compare two images, they differ a bit. You then add a big white border to those images. You diff again and find that the resultant PSNR value has risen some amount. What I want is a way to negate this. Obviously the white borders are the same in both images...
Updated
Ok, how about we make a mask of the areas you want to be compared first? So, if you want to ignore areas where both images are white, you can do this:
Then, when you do your comparison, mask the images beforehand:
or with
compare
like this in bash:I still don't see what you are trying to do, and I would still like to see your images and what result you expect... however, I have taken your advice and made two similar images from this page with the two
s
letters from the wordmissing
blanked out.a.png
b.png
So, if I now compare them, I get this:
and this image:
or if I do as I said, I get this:
and this image:
But I am still none the wiser about what you want because I have done all the work and you have not answered me.
Original Answer
It would be better if you could post your images so we can see what you mean, but try using
convert
like this to do the comparison:then you can add in pre-processing like this to make all white areas black before comparing. You can also add
-fuzz 10%
to catch near-white colours.Like Mark Setchell, I do not fully understand what your real intention is. You do not provide any (links to) sample images which may help us to grasp what you are up to.
Preparation
That's why I created 4 sample images myself. Here is the first pair:
Differences are minimal -- you'll hardly be able to spot them with the naked eye when reproduced here. Both images are 482x642 pixels:
Here comes the second pair. Again,
These are both enlarged to 1002x1002 pixels by adding a white frame around the initial pair. In the following reproduction they appear smaller because of the different scaling inside this web page:
So the "important" parts of the second pair are what is contained in the black frames. The white frames on the outer bound is identical.
Comparison
Now lets compare both pairs. But I do not want to limit the comparisons to just the PSNR metric. I want to see all available metrics. We can list available metrics with this command:
My command to return the metrics for the first pair is this:
Here is the result (slightly re-formatted) for the first pair ("important" part only):
My command to return the metrics for the second pair is this:
Here is the result (slightly re-formatted) for the second pair ("watered down" differences by added massive white frame):
Here are both results in a common table:
Note: comparing two identical images with the PSNR metric would result in an
inf
(infinitiv) value.Now draw your own conclusions...
Discussion
Understanding comparison metrics is not a straight forward affair.
Your own understanding of the PSNR seems to be a bit off, from how I interpret your 'Edit:'
Because a rising value for PSNR means that the two compared images have become a bit more identical to each other! (Of course, your intention to remove white (or otherwise colored) frames/borders around images before comparing them is still a reasonable approach. To see how to do that, look at the end of my answer...)
In order to get a better feeling for image comparison metrics, you should create a few simple "images" first. Then start experimenting with these.
Here is a suggestion how to create a series of mono-colored "patches", sized 100x100 pixels each:
Experimental Task: Compare each of the 100x100 pixels patches against each other.
Questions:
Now do the same for patches with 200x200 pixels:
Experimental Task: Compare each of the 200x200 pixels patches against each other.
Questions:
Now add a 50 pixels wide red frame around each of the 100x100 pixel patches. The resulting images will also be of size 200x200 pixels:
Experimental Task: Make up your own comparison pairs. (You know can also compare 200x200 pixel 'mono-color' patches against 200x200 pixels 'redframed' patches...)
Questions:
-metric phash
is the only one which allows you to compare images using different dimensions (width x height) ?How to 'trim' a mono-colored frame around images
You can remove any 'frame' around an image that is composed of identically colored pixels. The image operator
-trim
will achieve this automatically for you. (It works for colors different than white too.)