Jump to content
• Advertisement

#### Archived

This topic is now archived and is closed to further replies.

# Enhancing the contrast of a 2d image

This topic is 5957 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

## Recommended Posts

Im trying to write a contrast filter for 2d images, ive put together filters for brightness, several blurs, sharpen, and so on, but i havent figured this one out. It should, given a parameter ranging from 0 to 255, adjust the contrast of an image. When the value is 0, the entire image is grey(128,128,128), when its 255, all pixels with a value above 128 becomes 255, and those below 128 becomes 0. My first thought was: Easy! thats just a linear interpolation, either towards 0 or 255. Or is it? I looked at how photoshop does this, and it seems that some values reach 0, or 255, faster than others. And in a linear interpolation variant that wouldnt happen. Is there some algorithm for this that I (and google) hasnt seen yet?

#### Share this post

##### Share on other sites
Advertisement
I would imagine its proportional related to how much contrast the image had before the filter. Positive Percent in this case would be an increase in the contrast, a negative value would bring the contrast towards 0.

EG if 128 is the median contrast for a given pixel.

Over 128 = Value + (Value/100*Percent);
Under 128 = Value - ((128-Value)/100*Percent);

,Jay

Edit: Perhaps work out the effect 1% at a time, like compound interest. I made a mistake in the calculation (not small), Percent is in the range -100 to +100.

[edited by - Jason Zelos on June 30, 2002 5:27:56 PM]

#### Share this post

##### Share on other sites
look into histograms. contrast is not measured on a per pixel basis. its meausred by neighboring pixels, thus strecting a histogram should work well.

though a quick search on google provides:
http://visca.com/ffactory/archives/5-99/msg00021.html

which is a method that 99& matches photoshop. is for brightness and contrast.

warnign the code is confusing sine its written in filter factory langauge. hopefully you can make sense of some of the math that is going on though, if not look up a filter factory reference since it dont use too many oddball things.

[edited by - a person on July 1, 2002 2:09:08 AM]

#### Share this post

##### Share on other sites
Filter factory was an ugly language indeed, I found a reference over here: http://privat.schlund.de/f/filter-factory/files/ffpg.pdf and translated the filter, here''s what i came up with:

void contrast( UCHAR *outbuffer, UCHAR *inbuffer, int brightness, int contrast )
{
int color = *inbuffer++;

if(brightness>0)
color = rescale( color+brightness, 127*contrast/100, 255-127*contrast/100, 0, 255 );
else
color = rescale( color+brightness, 0, 255, -contrast*128/100, 256+contrast*128/100 );

*outbuffer++ = color;
}

inline int rescale( float value, float ol, float oh, float nl, float nh )
{
return nl+(value-ol)/(oh-ol)*(nh-nl);
}

As you can see this simple scales the input value to a larger range than 0->255, for instance when contrast is 100 the range is extended to -128->384. The values are then clipped down to 0,255.

This works and looks pretty good. however i have also come across some discussions and some horrible filter factory source for constrast enhancement using histograms. could you give me a little more detail on this approach?

#### Share this post

##### Share on other sites
I recall using histograms to find key frames in video. As I remember it a histogram gives a chart of the colour frequency. So I would separate this chart into a number of ranges, give each range a weighting and apply. I havn''t got the code anymore (a few years ago) and it was in MatLab anyway.

,Jay

#### Share this post

##### Share on other sites

• Advertisement
• Advertisement

• ### Popular Contributors

1. 1
Rutin
46
2. 2
3. 3
4. 4
5. 5
• Advertisement

• 13
• 10
• 12
• 10
• 13
• ### Forum Statistics

• Total Topics
632993
• Total Posts
3009761
• ### Who's Online (See full list)

There are no registered users currently online

×

## Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!