Page 1 of 1

How to get average gray level of an image

Posted: 2009-06-24T02:01:31-07:00
by wizards
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

Re: How to get average gray level of an image

Posted: 2009-06-24T08:04:42-07:00
by fmw42
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

Re: How to get average gray level of an image

Posted: 2009-06-24T20:23:47-07:00
by anthony
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/

Re: How to get average gray level of an image

Posted: 2009-06-25T15:07:30-07:00
by fmw42
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.

Re: How to get average gray level of an image

Posted: 2009-06-30T20:21:20-07:00
by fmw42
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.

Re: How to get average gray level of an image

Posted: 2009-07-04T10:03:21-07:00
by fmw42
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.