Search This Blog

Sunday, July 4, 2010

AForge.NET color filter C#

We are going to filter the colors of the video feed from a webcam. We will be using AForge.NET image processing framework for this and C#. You can check my previous posts for some basic video capture from webcam.

We have some steps as follows…

  1. Acquire image from webcam (check previous posts - click here )
  2. Create a color filter
  3. Apply filter

 

Step 1: Check previous posts - click here

Step 2: Create a color filter

First a little modification to the UI

image_thumb33

Then the code for the track bar.

image_thumb2

Now as we move the slider, the variable for the RGB has to change accordingly. We have given the incremental/decremental value of 3. So when we move the slider, the value of RGB will change +3 or –3. You can change to required value by changing the TickFrequency value.

image_thumb4

The main filter part. Please check this reference link/documentation ( http://www.aforgenet.com/framework/docs/html/35bd90e3-4e35-8f5f-e255-26c5d8d4b927.htm ). I have done the same with a  little UI.

image_thumb41

I am changing the value from 0 to the value in the track bar. You can add one more track bar for the lower value and can get a very good results.

Here is the reference and source for this step 2

Links : http://www.aforgenet.com/framework/docs/html/35bd90e3-4e35-8f5f-e255-26c5d8d4b927.htm

Step 3: Apply filter

image_thumb27

Now run it !!!!!!!!!!!!!!!!!!!!!!!

It works !!!

Here are my results

The Original Screen (Before Processing)

Note the trackbar at full. Move your trackbar to maximum initially. Else you will not see anything. Because all the filter will be at 0. Which means block everything.

image_thumb43

After applying value of red filter (look at the red trackbar)

image_thumb45

Wow, the red is completely removed.

Now lets try Blue. (bring back red trackbar to maximum)

image_thumb48

Light source is natural light (sun light) through window.

Same blue filter with indoor lightings. Yes. Lights do affect the result a lot.

image_thumb50

Green filter test..

image_thumb52
Ok, but not impressive. Because the lower limit of the filter is 0. If we move both upper and lower, we’ll get impressive results.

 

------------------------------------------- Using Lower Value for filter -----------------------------------------------------------------------

I just modified the UI so that I can change the lower value of the filter too. And I started playing to do some object isolation. Pretty nice. Here are some screen shots. Note the slide bar too.

image_thumb55

Isolated the orange cup !!!!!!!!!!!!!!!1 success

image_thumb58



Note :

  • Lightings play a major part in image processing. We need to add more things in the future to make it work under any lightings.
  • Camera also plays a major role. Due to some reasons, my camera shows green color in the background. Using good quality cameras, you can do miracles.

have a good day.
~Prem


14 comments:

  1. A great tutorial u have here !
    any idea wat do i have to change if mine is just a picture that i browsed from my computer and is show on a pictureBox ?

    ReplyDelete
  2. Change the source of the image to the read from a file instead of webcam.

    Reference : http://www.aforgenet.com/framework/docs/html/35bd90e3-4e35-8f5f-e255-26c5d8d4b927.htm

    Use the given above link.

    ReplyDelete
  3. my pictureBox2.Image = pic2;
    So i just put it as Bitmap sourceImage = pic2; ?
    Do i need to change anything else ? like the newframe thing . THANKS ALOT !

    ReplyDelete
  4. Yes, make sure the 'pic2' is an image.

    eg:

    pictureBox2.Image = (Bitmap)pic2 //Just cast it

    ReplyDelete
  5. First of all really thank u for the quick reply cuz i really need get this work asap.

    here is my code, can u tell me wat is wrong ?
    http://i1191.photobucket.com/albums/z464/louz90/2010-12-08_205224.png

    ReplyDelete
  6. Please tell me what problem you are getting.

    ReplyDelete
  7. When i drag the trackbar nothing happen on the image. i suppose the filter is not applied on the image when the trackbar is being drag

    ReplyDelete
  8. Thanks for the useful post hope to be in touch i'm working now with Aforge in my graduation project ....

    :D

    ReplyDelete
  9. I get an error on ColorFiltering..seems like it is not found. :S

    ReplyDelete
  10. Sorry for the late reply. Kind of busy with the PhD qualifying exam. Here is my solution.... You can download it and use it.

    I wrote it in Visual Studio 2010. But you can copy the code even into your older versions till .NET 2.0

    http://webpages.eng.wayne.edu/~dx8289/AForge.zip

    ~Prem

    ReplyDelete
  11. thanks,
    i have a small doubt...
    how to apply erosion filter on video feed images.

    ReplyDelete
  12. Here is how

    http://www.aforgenet.com/framework/docs/html/90a69d73-0e5a-3e27-cc52-5864f542b53e.htm

    Just a smilple google search will help.

    ~Prem

    ReplyDelete
  13. hi this tutorial is nice. and also i want entire source code for this color filter.. which will use full to work with that like me as electrical student

    ReplyDelete