Set brightness and Contrast of 2 Images similar

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
Wanderer
Posts: 12
Joined: 2014-05-04T14:45:11-07:00
Authentication code: 6789

Set brightness and Contrast of 2 Images similar

Post by Wanderer »

I am developing an application which requires me to compare 2 images for similarity. Before comparison I want to set the brightness and contrast of the image to be compared similar to the reference image. I can compare both images using the compare command however I am stuck on how to set the brightness and contrast of one image same with that of the reference image before comparing.

I am currently working through command line. Any help appreciated.. :)
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Set brightness and Contrast of 2 Images similar

Post by fmw42 »

You can try to use -equalize on both images. See http://www.imagemagick.org/script/comma ... p#equalize

Or you can try using my histmatch script to match one image's histogram to the other's if you are on Linux, Mac OSX or Windows/with Cygwin. See the line below.

Or use the NCC metric, which is supposed to normalize mean and standard deviation so that relative brightness/contrast is not significant.
Wanderer
Posts: 12
Joined: 2014-05-04T14:45:11-07:00
Authentication code: 6789

Re: Set brightness and Contrast of 2 Images similar

Post by Wanderer »

Hello fmw42 ,

Can you please tell me how to run your script.. I am quite new to imagemagick so couldn't figure out how to run the equalize command as well. Please provide me with a sample -equalize command.

Thanks..
Wanderer
Posts: 12
Joined: 2014-05-04T14:45:11-07:00
Authentication code: 6789

Re: Set brightness and Contrast of 2 Images similar

Post by Wanderer »

Hello ,

I ran the equalize command on both the images a.png and b.png and got 2 new images a1.png and b1.png. Now I compared a1.png and b1.png using compare command and saw the rgb difference to be more than what it was when I compared a.png and b.png. I am not sure whether equalize would be beneficial in my case. Please advice..

Compare Command :

Code: Select all

compare -verbose -metric RMSE "a1.png" "b1.png" "a1b1diff.png"
Equalize Command :

Code: Select all

convert "a.png" -set colorspace RGB -equalize "a1.png
"
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Set brightness and Contrast of 2 Images similar

Post by fmw42 »

What version of IM and platform are you using. If current, then

Code: Select all

convert "a.png" -equalize "a1.png
Perhaps you should provide your two input images. If they are quite different sources, then compare may not work well. Compare does not do well if there are any rotation or scale differences.

You can upload your images to some free hosting service such as dropbox.com (public folder) and put links here.

For my script, histmatch, you need to download the script and modify as per the instructions on my home page.

Code: Select all

histmatch image1 image2 newimage2
will try to match the histogram (grayscale) of the second image to match that of the first.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Set brightness and Contrast of 2 Images similar

Post by snibgo »

Wanderer: You should say what platform you are using.

Depending on the application, doing "-equalize" on both images seems the obvious, easiest and quickest solution. If the images are not identical, then doing any operation on both images might increase or decrease the reported difference.

To change the mean of one image (image1) to match the mean of another (imageRef), you can use "-evaluate Pow ZZ" where ZZ = ln(mean_imageRef) / ln(mean_image1).

For ordinary photographs, I use standard deviation as a simple measure of an image's contrast. "identify -verbose" shows this. "sigmoidal-contrast" can change it. I have a Windows BAT script sigSetSd.bat that calculates (by iteration) the sigmoidal-contrast for a desired standard-deviation. See my page "Adding zing to photographs".
snibgo's IM pages: im.snibgo.com
Wanderer
Posts: 12
Joined: 2014-05-04T14:45:11-07:00
Authentication code: 6789

Re: Set brightness and Contrast of 2 Images similar

Post by Wanderer »

Thanks guys. I wasn't using the equalize command properly. Once I did it as you people suggested I got better results however still there is some difference between images. Would histogram match as suggested reduce the difference further...?
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Set brightness and Contrast of 2 Images similar

Post by snibgo »

If you put "-evaluate set 0" before the "equalize" for each image, you will find a zero difference between the images.

Sorry, that was a joke.
Wanderer wrote:Would histogram match as suggested reduce the difference further...?
How would we know? You haven't shown us any sample images. Or said what platform you are on. Or even what version of IM you are using. We try to help but we are not mind readers.
snibgo's IM pages: im.snibgo.com
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Set brightness and Contrast of 2 Images similar

Post by fmw42 »

Did you try compare -metric ncc on both sets of images?

For new users, please read viewtopic.php?f=1&t=9620
Wanderer
Posts: 12
Joined: 2014-05-04T14:45:11-07:00
Authentication code: 6789

Re: Set brightness and Contrast of 2 Images similar

Post by Wanderer »

I am sorry I didn't answer that earlier. I am working on windows using the latest version - ImageMagick-6.8.9-Q16.

For now I m just trying this experiment with any random .png images. I am just increasing contrast and brightness of an image and saving it as a new image and then running the equalize command on the old and new image. Then I compare the 2 to check the difference. I have provided below sample ballon image urls that I tested with.

Image

Image
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Set brightness and Contrast of 2 Images similar

Post by fmw42 »

You can see from this that using RMSE shows a great deal of improvement from the original to the equalized versions. But NCC shows rather little change since it normalizes the mean and standard deviations between them. For NCC a perfect match is 1. For RMSE a perfect match is 0.

Note that NCC has a bug if the images have alpha channels, even if both are fully opaque. I have reported this. So just turn alpha off, since both your images have a fully opaque alpha channel.

Code: Select all

convert balloon2.png -equalize balloon2_eq.png
convert balloon3.png -equalize balloon3_eq.png


compare -metric ncc -alpha off balloon2.png balloon3.png null:
0.928874

compare -metric ncc -alpha off balloon2_eq.png balloon3_eq.png null:
0.99302

compare -metric rmse balloon2.png balloon3.png null:
12703.9 (0.19385)

compare -metric rmse balloon2_eq.png balloon3_eq.png null:
3027.34 (0.0461942)
Relative change original vs equalized:

RMSE = 4.2x

NCC = 1.07x

Thus NCC mostly does the job already without having to equalize.


Using my script, histmatch, I get ever better matching of the two images and the rmse score is reduced further, while the NCC score does not change much.

Code: Select all

histmatch -c rgb balloon3.png balloon2.png balloon2_hm_rgb.png

compare -metric rmse balloon3.png balloon2_hm_rgb.png null:
917.182 (0.0139953)

compare -metric ncc -alpha off balloon3.png balloon2_hm_rgb.png null:
0.999806
Histmatch: balloon2 matched to look like balloon3

Image
Post Reply