Click here to Skip to main content
Click here to Skip to main content

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

Introduction

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.

Background

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.

History

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

License

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

Miran.Uhan
Engineer
Slovenia Slovenia
No Biography provided

Comments and Discussions

 
GeneralMy vote of 5 Pinmembermanoj kumar choubey23-Feb-12 21:24 
GeneralHI PinmemberMember 768566518-Feb-11 6:56 
GeneralNeeds more info... PinmvpDave Kreskowiak20-Apr-08 4:46 
GeneralAwesome!!!! Pinmembersides_dale19-Jul-07 15:24 
GeneralMultiple Colors PinmemberFrank W. Wu17-Jul-07 4:35 
AnswerRe: Multiple Colors PinmemberMiran.Uhan24-Jul-07 1:34 
GeneralApplication crashes PinmemberIrwan Hassan10-Jul-07 17: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 PinmemberMiran.Uhan16-Jul-07 5:41 

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

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

| Advertise | Privacy | Mobile
Web02 | 2.8.140718.1 | Last Updated 12 Jul 2007
Article Copyright 2007 by Miran.Uhan
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid