Intel® Integrated Performance Primitives
Deliberate problems developing high-performance vision, signal, security, and storage applications.

Auto Contrast in Grey Images

SharathKum_D_Intel
3,683 Views

Hello All, 

I just implemented the histogram equalization technique using intel ipp functions. Since Histogram equalization equalizes within the two extreem values, I was thinking of implementing other methods for auto contrast. 

Is there any way that I can perform auto contrast and not histogram equlaiztion? 

Thanks, 
Sharath 

0 Kudos
29 Replies
Thomas_Jensen1
Beginner
3,030 Views

I'm also interested in discussing automatic contrast algorithms.
Histogram Equalization, Histogram Stretching, Gamma Correction etc.

The problem is, you start with a bad grayscale image, and how do you present it perfectly, automatically?
The bad image is full of problems: under/over exposed, artifacts etc.

0 Kudos
SergeyKostrov
Valued Contributor II
3,030 Views
>>...Is there any way that I can perform auto contrast and not histogram equlaiztion? I would try to do it using a simple algorithm to calculate Center of Gravity of a Histogram represented as a small image. I know that in Point-and-Shot digital cameras and webcams it is done in a very simple way. Which one? I do not know.
0 Kudos
Thomas_Jensen1
Beginner
3,030 Views

Center of gravity is an area (with a fixed width) in the histogram, where the Percentile is highest.

0 Kudos
SharathKum_D_Intel
3,030 Views

I learnt that "Center of Gravity" of a histogram is the mean of the histogram values. I am not knowing if "mean" would be a good factor in performing the auto contrast. Any thoughts on this? 

0 Kudos
SergeyKostrov
Valued Contributor II
3,030 Views
>>...I learnt that "Center of Gravity" of a histogram is the mean of the histogram values... Take a look at a generic description of an algorithm... Imaging a histogram of some Source Image ( SI ) as an Histogram Image ( HI ), for example, with dimensions 128x128 pixels ( _int8 data type ). Now, 3 cases are possible and If: - The histogram is "shifted" to the left part of the HI than the SI is underexposed - The "summit" of the histogram is in the "middle" of the HI than the SI is properly exposed - The histogram is "shifted" to the right part of the HI than the SI is overexposed A simple calculation of the Center of Gravity for HI could tell you how you should adjust parameters of a camera in order to take a properly exposed image. The same rules could be applied for Auto-Contrast processing.
0 Kudos
SharathKum_D_Intel
3,030 Views

Thanks Sergey for that quick response. It totally makes sense to take the center of gravity now. I will try it out and let you know about it. 

I was also curious to know if there are any IPP functions to calcualte the "Center of Gravity". If yes, kindly let me know. 

Thanks, 

Sharath

0 Kudos
SergeyKostrov
Valued Contributor II
3,030 Views
The Center of Gravity is a very simple algorithm and here it is from my test-cases: RTvoid CalcCenterOfGravityF( RTfloat *pfMatrix, RTint iR, RTint iC, RTint *piCoGX, RTint *piCoGY ); RTvoid CalcCenterOfGravityF( RTfloat *pfMatrix, RTint iR, RTint iC, RTint *piCoGX, RTint *piCoGY ) { RTfloat fCoGX = 0.0f; RTfloat fCoGY = 0.0f; RTfloat fTotalI = 0.0f; RTfloat fPxValue = 0.0f; for( RTint r = 0; r < iR; r++ ) { RTint iRTemp = ( r * iC ); for( RTint c = 0; c < iC; c++ ) { fPxValue = *( pfMatrix + ( iRTemp + c ) ); fCoGX += ( fPxValue * c ); fCoGY += ( fPxValue * r ); fTotalI += fPxValue; } } if( fTotalI != 0.0f ) { *piCoGX = ( RTint )( fCoGX / fTotalI ); *piCoGY = ( RTint )( fCoGY / fTotalI ); } else { *piCoGX = 0; *piCoGY = 0; } } Please do all the rest clean ups. Note: a template-based implementation of the function is more flexible.
0 Kudos
SharathKum_D_Intel
3,030 Views

Sergey, Thanks for posting the code snippet with your test cases. Since I am fairly new to image processing, It took me sometime to understand your code. It would be more helpful if you had comments to walk me through. Nevertheless It is very helpful. 

0 Kudos
SergeyKostrov
Valued Contributor II
3,030 Views
Hi everybody, >>... It would be more helpful if you had comments to walk me through. Nevertheless It is very helpful... I'll post a complete test-case. >>...I'm also interested in discussing automatic contrast algorithms. Thomas, I looked at it and some technical details will be provided. By the way, do you have any issues / problems with Auto Contrast algorithm?
0 Kudos
Thomas_Jensen1
Beginner
3,030 Views

Sergey, I have lots of grayscale images that just cannot be displayed "perfectly" automatically,

The problem is always that the histogram of the grayscale image has large parts that must be avoided before doing automatic this-and-that.
I call those part artifacts. One artifact can be a wide tall peak in the white part, or a narrow high peak in the medium gray part. Those must be removed before applying autmatic algorithms. One way of "removing" is to mask the image before the histogra is computed, another way is to patch the histogram, supressing an easy to detect artifact.

I already use "stretching", wherein I move zero or very low black point and zero or very low white point, using percentiles to determine when to stop. To find the grayscale of greatest interest, I use what I described above, sliding a wide band over the histogram, choosing the location having the higest percentile.

Suggestions are welcome.

0 Kudos
SergeyKostrov
Valued Contributor II
3,030 Views
Could you attach an example of one of these grayscale images with artifacts? Just description is not enough. Also, I was going to talk about a generic algorithm for a such correction. Thanks.
0 Kudos
SergeyKostrov
Valued Contributor II
3,030 Views
>>I have lots of grayscale images that just cannot be displayed "perfectly" automatically, >> >>The problem is always that the histogram of the grayscale image has large parts that must be avoided before >>doing automatic this-and-that... Thomas, How do you calculate contrast? What method or formula do you use? Note: I wonder if you use a classic one widelly used in Machine Vision applications for greyscale ( monochrome ) images: %Contrast = ( Imax - Imin ) / MaxValueOfDynamicRange For example, for 8-bit images MaxValueOfDynamicRange is equal to 255.
0 Kudos
Thomas_Jensen1
Beginner
3,030 Views

Contrast is not a property of the image but is a controlling parameter of an image manipulation algorithm. For instance, you can increase the contrast of an image by adjusting a pixel values such that the difference between light and dark is bigger.

In the real world there are images which need adjustment before being displayed perfectly, and I'm looking for automatic methods for this problem.

I uploaded some test images.

0 Kudos
SergeyKostrov
Valued Contributor II
3,030 Views
Thomas, You don't need to explain what Image Processing is and I didn't ask for the explanation of a widely known things about Image Processing, right? I have a very good experience with X-Ray imaging and MRI ( 2-D & 3-D / Volume Rendering ). My simple question was: >>...How do you calculate contrast? What method or formula do you use?
0 Kudos
SergeyKostrov
Valued Contributor II
3,030 Views
Sorry for a small deviation because this is Not related directly to IPP. >>...I uploaded some test images... Thomas, Thank you for uploading a set of images which demonstrate some post image processing and I see now that attempt to use a classic formula: %Contrast = ( Imax - Imin ) / MaxValueOfDynamicRange will fail ( actually, I already did a quick test ). Note: Source images look like MRI images, Not X-Ray ( however I could be wrong... ), with quality issues, like they are too overexposed ( captured structure of bones and, at the same time, soft tissues (!) / to capture soft tissues very low doses of radiation are needed ). I think a more complex filtering techique needs to be used based on IIR or FIR ( with some Windowing function ), or some Convolution based filtering, or something else. It is clear that some software completed a very good correction of source images. I will try a Wallis Statistical Filter but I'm Not sure that it will improve overall quality of the source image. I'll let you know as soon as my tests are completed.
0 Kudos
Thomas_Jensen1
Beginner
3,030 Views

My sample images all have lightness problems since we discussed that. Applying filtering (and with that I understand structure/sharpness filtering) will not "fix" the lightning problem, unless you can tell me a nice idea of how.

Further, my images are reduced for presentation here. I could upload real 16 or 12 bpp grayscale originals, if you want.

I do not know about Wallis Statistical Filters, but I googled it, and it seems to also have use in my field, and indeed can possible help with simultanious dark and bright areas.
Can IPP perform Wallis?

I also spotted noise reduction using Wavelets, do you have an IPP code sample that demonstrates this?

0 Kudos
SergeyKostrov
Valued Contributor II
3,030 Views
>>...My sample images all have lightness problems since we discussed that. Applying filtering (and with that I understand >>structure/sharpness filtering) will not "fix" the lightning problem... Did you do a Manual correction to get '_wanted.png' images? These images look significantly better. Please provide details.
0 Kudos
Thomas_Jensen1
Beginner
3,030 Views

Yes, my sample images have original and manually adjusted lightning versions. No filtering is done (structure/sharpening), only contrast/brightness/gamma.

I'm very curious for a Wallis implementation, to test with...  can be outside IPP...

0 Kudos
Thomas_Jensen1
Beginner
3,030 Views

By the way, my sample images are not MRI, just simple digital 2D x-ray.

0 Kudos
SergeyKostrov
Valued Contributor II
2,816 Views
>>...I'm very curious for a Wallis implementation, to test with... can be outside IPP... Just checked IPL ( version 2.5 ) and IPP ( versions from 3.x to 7.x ) and they don't have it.
0 Kudos
Reply