How to get average gray level of an image

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?".

How to get average gray level of an image

Postby wizards » 2009-06-24T02:01:31+00:00

Hi

I have two images with difference in brightness. Now i want to set the brightness of first image to the second image.
How to get the "contrast"/average gray level of an entire image.

I am doing this in C++ by using Matick++. How can i get the gray level of an image in C++.

I am able to change the brightness of an entire image with contrast method of IM. Is there any method like this to get contrast value for an entire image.

If i try to get gamma i am getting 0.0. Why i am getting gamma value as ZERO.
Can you please tell me what is the main difference of "gamma" & "contrast"

ThanksInAdvance
wizards
 
Posts: 46
Joined: 2008-12-05T06:31:55+00:00

Re: How to get average gray level of an image

Postby fmw42 » 2009-06-24T08:04:42+00:00

The average graylevel of an image may be found using the string format "%[mean]"

convert image -format "%[mean]" info:

see http://www.imagemagick.org/script/escape.php

The average is a reasonable measure of brightness.

Another way to get the average is to just reduce the image to 1 pixel and get its color:

convert image -scale 1x1! -format "%[pixel:s.p{0,0}]" info:

Contrast is harder to measure. But a reasonable measure is just the standard deviation

convert image -format "%[standard-deviation]" info:


You can change the brightness of an image using -modulate in HSB mode.

see http://www.imagemagick.org/Usage/color/#modulate


I have a script called bcimage that allows you to change the brightness, contrast and/or saturation of an image by percent change. So if you measure the mean and std of the two image, you can calculate the percent change and feed those to bcimage.

Also, if you know how you created the first image, you can use the new -hald-clut to apply the same changes to all other images.

see
http://www.imagemagick.org/script/comma ... #hald-clut
http://www.imagemagick.org/Usage/color/#hald-clut
User avatar
fmw42
 
Posts: 15009
Joined: 2007-07-02T17:14:51+00:00
Location: Sunnyvale, California, USA

Re: How to get average gray level of an image

Postby anthony » 2009-06-24T20:23:47+00:00

Their is a number of ways to change an image so that its average brightness matches, or even its average color matches that of an other image. add the difference, multiply (modulate), gamma adjust, etc...

If you play with this let us know what you find out.



I do know that brightness matching and blending is often needed as part of pamorama image processing, so it may give you some help.

Rick Mabry also have done image average modification work.. His notes are located at
http://www.lsus.edu/sc/math/rmabry/imagemagick/avgdemo/
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
http://www.imagemagick.org/Usage/
User avatar
anthony
 
Posts: 8732
Joined: 2004-05-31T19:27:03+00:00
Location: Brisbane, Australia

Re: How to get average gray level of an image

Postby fmw42 » 2009-06-25T15:07:30+00:00

For what it is worth (and I do not expect too much) I have uploaded a new script to my site, bcmatch, that attempts to match the brightness and contrast of one image to that of another. It is limited as it only uses b/c which are linear effects. Thus it cannot handle such non-linear modifications as gamma. Also once information is lost in an image (perhaps due to being too high contrast), it cannot generally be recovered. Thus this technique, seems to work better on dark, low contrast image than bright high contrast images. But I have only tested with a couple of examples.

I have also uploaded another new script to my site, histmatch, that attempts to match the histogram of one image to that of another. It uses a non-linear transformation. But again, once information is lost in an image (perhaps due to being too high contrast), it cannot generally be recovered.
User avatar
fmw42
 
Posts: 15009
Joined: 2007-07-02T17:14:51+00:00
Location: Sunnyvale, California, USA

Re: How to get average gray level of an image

Postby fmw42 » 2009-06-30T20:21:20+00:00

Please note that my histmatch script has a problem and currently only works correctly for two images the same size. I am working to correct that and will report back later when done.

_____________________

OK. I believe I have it fixed now.
User avatar
fmw42
 
Posts: 15009
Joined: 2007-07-02T17:14:51+00:00
Location: Sunnyvale, California, USA

Re: How to get average gray level of an image

Postby fmw42 » 2009-07-04T10:03:21+00:00

With Anthony's help and some study of awk (and changing while loops to for loops), I have been able to make a 10x speed improvement (on a 128x128 image) in redist and histmatch.
User avatar
fmw42
 
Posts: 15009
Joined: 2007-07-02T17:14:51+00:00
Location: Sunnyvale, California, USA


Return to Users

Who is online

Users browsing this forum: Bing [Bot], Yahoo [Bot] and 13 guests