Compute the difference between two images but with different dimensions

Questions and postings pertaining to the usage of ImageMagick regardless of the interface. This includes the command-line utilities, as well as the C and C++ APIs. Usage questions are like "How do I use ImageMagick to create drop shadows?".
Post Reply
aigo
Posts: 29
Joined: 2017-06-26T12:02:47-07:00
Authentication code: 1151

Compute the difference between two images but with different dimensions

Post by aigo » 2017-08-17T07:27:25-07:00

Hi all,

I am trying to compare two similar images but with different dimensions. I have tried using the compare function in IM but it requires both images to be of same dimension.

Is there a way to go around this?

My IM version is 7.0.6-0. Mac OS Sierra 10.12.6

Thanks.

Aigo

snibgo
Posts: 9379
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Compute the difference between two images but with different dimensions

Post by snibgo » 2017-08-17T07:31:00-07:00

What do you want to measure? IM compares corresponding pixels in the two images.

The obvious method would resize one to the size of the other. Or resize them both to the average size of the two images. But perhaps you want to measure something different.
snibgo's IM pages: im.snibgo.com

aigo
Posts: 29
Joined: 2017-06-26T12:02:47-07:00
Authentication code: 1151

Re: Compute the difference between two images but with different dimensions

Post by aigo » 2017-08-17T07:46:22-07:00

Hi Snibgo,

Thanks for responding so fast. yes, I have done that but I worry that the resizing messes with the pixels in the images further.

I am looking to measure the difference between two images, hoping it will give measurable insight into how good they will look in comparison to the original image.

Also, kindly explain what the two values outputted mean.

Thanks!

Aigo

snibgo
Posts: 9379
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Compute the difference between two images but with different dimensions

Post by snibgo » 2017-08-17T08:20:37-07:00

There are many ways of comparing two images. IM works by doing some arithmetic on each pair of pixels (one per image). For example, RMSE is the square root of the mean error (where "error" really means "difference"). So the differences in values from the two pixels are squared, then all added together, then divided by the number of pixels to get the mean, then the square root is taken.

I generally work with RMSE. Some people prefer PSNR.

Code: Select all

f:\web\im>%IM%convert rose: -level 0,90% r.png

f:\web\im>%IM%compare -metric RMSE rose: r.png NULL:
2966.54 (0.0452665)
The first number is on a scale of zero to QuantumRange. For Q16, QuantumRange is 65535. The second number is the same, but on a scale of 0.0 to 1.0. I often multiply the second number by 100 to get a percentage, 4.52665%.

As your images are different sizes, another possibility is to extend one to match the size of the other. But then you have these extra "invented" pixels.

If you can post samples, perhaps we can advise better.
snibgo's IM pages: im.snibgo.com

User avatar
fmw42
Posts: 22081
Joined: 2007-07-02T17:14:51-07:00
Location: Sunnyvale, California, USA

Re: Compute the difference between two images but with different dimensions

Post by fmw42 » 2017-08-17T09:58:37-07:00

You can compare two different size images with histogram comparison metrics. See my bash unix script, histcompare, at my link below.

aigo
Posts: 29
Joined: 2017-06-26T12:02:47-07:00
Authentication code: 1151

Re: Compute the difference between two images but with different dimensions

Post by aigo » 2017-08-17T13:41:04-07:00

snibgo wrote:
2017-08-17T08:20:37-07:00
There are many ways of comparing two images. IM works by doing some arithmetic on each pair of pixels (one per image). For example, RMSE is the square root of the mean error (where "error" really means "difference"). So the differences in values from the two pixels are squared, then all added together, then divided by the number of pixels to get the mean, then the square root is taken.

I generally work with RMSE. Some people prefer PSNR.

Code: Select all

f:\web\im>%IM%convert rose: -level 0,90% r.png

f:\web\im>%IM%compare -metric RMSE rose: r.png NULL:
2966.54 (0.0452665)
The first number is on a scale of zero to QuantumRange. For Q16, QuantumRange is 65535. The second number is the same, but on a scale of 0.0 to 1.0. I often multiply the second number by 100 to get a percentage, 4.52665%.

As your images are different sizes, another possibility is to extend one to match the size of the other. But then you have these extra "invented" pixels.

If you can post samples, perhaps we can advise better.
Hi snibgo,

Thanks for your response.

Is it possible to retrieve just the second value?

Thanks!

Aigo.

aigo
Posts: 29
Joined: 2017-06-26T12:02:47-07:00
Authentication code: 1151

Re: Compute the difference between two images but with different dimensions

Post by aigo » 2017-08-17T13:43:41-07:00

fmw42 wrote:
2017-08-17T09:58:37-07:00
You can compare two different size images with histogram comparison metrics. See my bash unix script, histcompare, at my link below.
Hi Fred,

Thanks for your response.

With regards to the metrics used in the scripts, which do you recommend will be a better indicator of difference between two images...(a measure of how different they are)?

Thanks!

Aigo.

snibgo
Posts: 9379
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Compute the difference between two images but with different dimensions

Post by snibgo » 2017-08-17T14:16:08-07:00

aigo wrote:Is it possible to retrieve just the second value?
In bash:

Code: Select all

$ value=`compare -metric rmse rose: r.png NULL: 2>&1 | sed -n 's/^.*[(]\(.*\)[)]$/\1/p'`

$ echo $value
0.0452665
("sed" baffles me, so I guess I got that snippet from Fred.)
snibgo's IM pages: im.snibgo.com

User avatar
fmw42
Posts: 22081
Joined: 2007-07-02T17:14:51-07:00
Location: Sunnyvale, California, USA

Re: Compute the difference between two images but with different dimensions

Post by fmw42 » 2017-08-17T15:09:41-07:00

aigo wrote:
2017-08-17T13:43:41-07:00
fmw42 wrote:
2017-08-17T09:58:37-07:00
You can compare two different size images with histogram comparison metrics. See my bash unix script, histcompare, at my link below.
With regards to the metrics used in the scripts, which do you recommend will be a better indicator of difference between two images...(a measure of how different they are)?
I would try them against a set of test images to determine for your images what might be best. But as a guess, I would suggest correlation or chi-squared.

Post Reply