Click here to Skip to main content
13,349,502 members (49,313 online)
Click here to Skip to main content
Add your own
alternative version


39 bookmarked
Posted 2 Jul 2007

Selective Grayscale Filter

, 12 Jul 2007
Rate this:
Please Sign up or sign in to vote.
Convert image to grayscale except predefined color

Screenshot - Selective_Grayscale_Filter_100.jpg


This Visual Basic .NET class implements an image filter that can change an RGB image to a grayscale image whilst leaving a selected range of RGB values unchanged. A small demo project is available to show the filter's use.


When creating and optimizing Hue/Saturation/Lightness filter and Grayscale filter I learned a lot about images and colors. Observing the HSL circle (below), I got an idea to make filter where a certain color would remain intact while others are converted to grayscale.

Screenshot - HS_Circle_100.jpg

As can be seen in the HSL circle, similar colors are grouped within a certain angle. If we want the red color to remain intact and other colors to be converted to grayscale, we should not change pixels with a hue around 0 degrees. Since I had HSL and grayscale conversions already programmed, programming the filter was easy. Most of the time it was used to implement a tolerance calculation. The decision to implement tolerances for saturation and lightness proved to be right when testing the filter. Shadows can be effectively filtered, reducing saturation and lightness tolerance.

Tolerances for saturation and lightness (SL) work in a somewhat unusual way. I took the SL of the color we wish to remain unchanged as zero tolerance. Plus and minus tolerances do not necessary have equal values. If, for example, the saturation of the color we wish to remain unchanged is 0.75, then with tolerance set to 0.1 (10%) the minus tolerance will be 0.075 and the plus tolerance will be 0.025, i.e. 10% of the difference between 0.75 and 1.0.

Using the code

Using the filter is simple. Set the properties and call the execution function. I have provided a small demo project where you can play with both filters, change color factors and colors, and finally save the filtered images. The code for the project is also included.

The base for the demo application is the same as for the other two projects mentioned. With sliders, you can change tolerance for hue (0 to 180 degrees), saturation and lightness (0 to 100%). You can select the color with the color dialog and color picker. Implementation of the color picker is a bit clumsy. You have to click the "Picker" button, select the color and then click same button again to get a normal cursor. Picking the color is working only when the "Picker" cursor is displayed. The color under the cursor is shown in a small square between the buttons.

The demo application uses a cheap trick to look fast. The pictures displayed are resized and all the filtering is done on resized image. The filtering of a real image is done only before saving, so saving can take some more time: approximately 2 seconds on my computer with an image size of 2288x1712 pixels. I've provided two pictures you can play with in the beginning. I get nice results with both images.

Points of interest

When preparing the demo application, I learned how to change the cursor with the custom image. It's simple if you already have cursor file. It's some more work if you are making one.

I needed an HSL circle image for this article and just for fun I programmed a function that creates the required image with a given circle radius and lightness value.


  • 2007-06-25: Version 1.0
  • 2007-07-12: Article edited and moved to the main article base


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Slovenia Slovenia
No Biography provided

You may also be interested in...

Comments and Discussions

GeneralMy vote of 5 Pin
manoj kumar choubey23-Feb-12 22:24
membermanoj kumar choubey23-Feb-12 22:24 
GeneralHI Pin
Member 768566518-Feb-11 7:56
memberMember 768566518-Feb-11 7:56 
GeneralNeeds more info... Pin
Dave Kreskowiak20-Apr-08 5:46
mvpDave Kreskowiak20-Apr-08 5:46 
GeneralAwesome!!!! Pin
sides_dale19-Jul-07 16:24
membersides_dale19-Jul-07 16:24 
GeneralMultiple Colors Pin
Frank W. Wu17-Jul-07 5:35
memberFrank W. Wu17-Jul-07 5:35 
AnswerRe: Multiple Colors Pin
Miran.Uhan24-Jul-07 2:34
memberMiran.Uhan24-Jul-07 2:34 
GeneralApplication crashes Pin
Irwan Hassan10-Jul-07 18:07
memberIrwan Hassan10-Jul-07 18:07 
The application crash if I choose a different image from different folder. It says the *.cur cannot be find. I'm sure this is a simple bug to solve.
AnswerRe: Application crashes Pin
Miran.Uhan16-Jul-07 6:41
memberMiran.Uhan16-Jul-07 6:41 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.180111.1 | Last Updated 12 Jul 2007
Article Copyright 2007 by Miran.Uhan
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid