Modify Automatic contrast command and Levels tool

Ideas for improvements and requests for new features in XnView Classic

Moderators: XnTriq, xnview

Post Reply
Konstantin
Posts: 8
Joined: Sun Dec 28, 2008 11:07 pm
Location: Argentina

Modify Automatic contrast command and Levels tool

Post by Konstantin » Mon Jan 12, 2009 2:09 pm

XnView has an impressive set of Image Editing tools, which was the main reason for me to switch from IrfanView. However, these tools are poorly documented, so that I decided to try to figure out their precise working, using Photoshop as a reference (actually I used Photoshop Elements but I think that the different tools work the same in both programs). Here are my findings so far.

The XnView Image/Map/Automatic levels and the PhotoShop Auto Levels commands have precisely the same effect. They linearly and independently stretch the Red, Green, and Blue histograms so that the darkest pixel of the final image is always pure black while the lightest one is pure white. This maximizes the overall image contrast but usually changes the color balance. In some cases this last effect can be beneficial since it can remove unwanted color casts.

The XnView Image/Map/Automatic contrast command has precisely the same effect as moving the BlackPoint slider up to the leftmost non zero histogram value and the WhitePoint slider up to the rightmost non zero value in the Image/Adjust/Levels tool. This also applies to PhotoShop. However, and rather surprisingly, the effect of the XnView Automatic contrast and that of the PhotoShop Auto Contrast commands on an image can be quite different, as discussed below.

The XnView Image/Adjust/Levels tool displays a histogram that is precisely the same as the Luminosity histogram in Photoshop, where:
Luminosity = 0.3 * Red + 0.59 * Green + 0.11 * Blue.
However, the Levels tool in Photoshop does not display the Luminosity histogram but rather the "RGB" histogram, which is a superposition of individual histograms for the Red, Green, and Blue channels.

Therefore, if the BlackPoint slider is moved up to the leftmost non zero histogram value and the WhitePoint slider up to the rightmost non zero value, the results obtained with XnView and with PhotoShop are usually different. With XnView it is quite possible that the darkest and the lightest pixels with different color values transform to have the same colors. However, this does not happen with PhotoShop. Consider the following example:

The darkest pixels of an image have the following RGB and Luminosity values:
000,063,000, Luminosity=037
031,087,023, Luminosity=063
063,111,047, Luminosity=090
while the lightest include pure white (255,255,255, Luminosity=255).
In XnView the Levels histogram would extend from 037 to 255, so that the Automatic contrast command would map the input Luminosity 037 value to an output value of zero. Therefore, the considered pixels would transform to:
000,030,000, Luminosity=018
000,058,000, Luminosity=034
030,086,012, Luminosity=061
Note that in the first two input pixels the Red and Blue values are different while they are equal in the first two output pixels.

This shows that the XnView Automatic contrast command (and the Levels tool) can often lead to color posterization (different input color values become equal in the output). This does not happen in the corresponding PhotoShop tools since, for the considered example, the RGB histogram extends from 000 (there are 000 Red and Blue values in the first considered pixels) to 255, so that the Auto Contrast command has no effect whatsoever on the image.

Conclusion: I think that it would be advisable to modify the Image/Map/Automatic contrast command and the Image/Adjust/Levels tool showing the RGB histogram (as in PhotoShop) rather than the Luminosity histogram, since the present implementation tends to lose information in the darkest and lightest parts of the image by means of color posterization.

User avatar
JohnFredC
XnThusiast
Posts: 2010
Joined: Wed Mar 17, 2004 8:33 pm
Location: Sarasota Florida

Post by JohnFredC » Mon Jan 12, 2009 4:14 pm

This is a valuable post. I learned from it.
John


Konstantin
Posts: 8
Joined: Sun Dec 28, 2008 11:07 pm
Location: Argentina

Post by Konstantin » Tue Jan 13, 2009 8:21 pm

XnTriq wrote:
JohnFredC wrote:This is a valuable post. I learned from it.
Yes, very interesting! Thanks & welcome aboard, Konstantin :-)
Thank you JohnFredC and XnTric for your kind words.
I just read the previous Forum threads on related topics and would like to add the following comments:

In Harder "Autolevels" (like Photoshop), brotondi states that the Photoshop Autolevels command tends to stronger change an image than the XnView Automatic levels command. This seems to contradict my statement that both commands work precisely in the same fashion. I think that the reason for this difference is the default clipping value of 0.5% used in Photoshop: the histogram is stretched starting from a point such that the number of pixels to its left is 0.5% of the total number of pixels. This default value is very high but it can be modified (I changed it to 0.1% long time ago). I do not know what the clipping value used in XnView is, but it seems to be lower than 0.5%.

In "Automatic levels" adjustment wrongly implemented, stephan_g states that the XnView Automatic levels command was intended to have the same effect as the Levels tool but actually behaves differently. As stated in my note, this difference is intentional but has nothing to do with non linear transformations. For some misterious reason, the automatic command that does the same thing as the Levels tool is Automatic contrast, not Automatic levels. This nomenclature is consistent with other image editing programs such as Photoshop but leads to endless confusions.

Chengran
Posts: 2
Joined: Wed Apr 01, 2009 8:10 am
Location: Munich, Germany
Contact:

Re: Modify Automatic contrast command and Levels tool

Post by Chengran » Wed Apr 01, 2009 8:26 am

Hey, guys, thank you for this nice software. I used this software for some data analysis and the automatic contrast function helps me a lot. However, I am curious about the algorithm of this function. Since the graphic which I deal with is pure grey scale picture, what I did to improve the contrast is simply:

(I-I(min))/(I(max)-I(min))*255, where I signifies the intensity of the pixel, I(max) and I(min) are the maximum and minimum of the pixel intensity of the whole picture.

The above procedure is executed IDL but the outcome is not so nice as what is done in XnView. Thus I wonder whether there is special trick in the software or I made mistake in the algorithm stated above. Thank you.

Konstantin
Posts: 8
Joined: Sun Dec 28, 2008 11:07 pm
Location: Argentina

Re: Modify Automatic contrast command and Levels tool

Post by Konstantin » Wed Apr 01, 2009 6:30 pm

Chengran wrote:Hey, guys, thank you for this nice software. I used this software for some data analysis and the automatic contrast function helps me a lot. However, I am curious about the algorithm of this function. Since the graphic which I deal with is pure grey scale picture, what I did to improve the contrast is simply:

(I-I(min))/(I(max)-I(min))*255, where I signifies the intensity of the pixel, I(max) and I(min) are the maximum and minimum of the pixel intensity of the whole picture.

The above procedure is executed IDL but the outcome is not so nice as what is done in XnView. Thus I wonder whether there is special trick in the software or I made mistake in the algorithm stated above. Thank you.
As far as I understand your algorithm is fine but you are using a clipping value of zero. If your image has a low contrast but happens to have just one purely black pixel, I(min)=0, and one purely white pixel, I(max)=255, then your algorithm does nothing to the image. The trick implemented in XNview is to use your formula but to change the interpretation given to I(min) and I(max):
I(min)=intensity such that 0.1% of all pixels are darker,
I(max)=intensity such that 0.1% of all pixels are brighter.
The value 0.1% is typical but I do not know for sure what is the actual value used by XNview.

Chengran
Posts: 2
Joined: Wed Apr 01, 2009 8:10 am
Location: Munich, Germany
Contact:

Re: Modify Automatic contrast command and Levels tool

Post by Chengran » Fri Apr 03, 2009 11:45 am

Konstantin wrote:
Chengran wrote:Hey, guys, thank you for this nice software. I used this software for some data analysis and the automatic contrast function helps me a lot. However, I am curious about the algorithm of this function. Since the graphic which I deal with is pure grey scale picture, what I did to improve the contrast is simply:

(I-I(min))/(I(max)-I(min))*255, where I signifies the intensity of the pixel, I(max) and I(min) are the maximum and minimum of the pixel intensity of the whole picture.

The above procedure is executed IDL but the outcome is not so nice as what is done in XnView. Thus I wonder whether there is special trick in the software or I made mistake in the algorithm stated above. Thank you.
As far as I understand your algorithm is fine but you are using a clipping value of zero. If your image has a low contrast but happens to have just one purely black pixel, I(min)=0, and one purely white pixel, I(max)=255, then your algorithm does nothing to the image. The trick implemented in XNview is to use your formula but to change the interpretation given to I(min) and I(max):
I(min)=intensity such that 0.1% of all pixels are darker,
I(max)=intensity such that 0.1% of all pixels are brighter.
The value 0.1% is typical but I do not know for sure what is the actual value used by XNview.
Thanks a lot. I will check it out. It is true that I might have extreme bright pixel due to the high speed ccd camera.

Post Reply