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


Then the code for the track bar.


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.


The main filter part. Please check this reference link/documentation ( ). I have done the same with a  little UI.


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 :

Step 3: Apply filter


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.


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


Wow, the red is completely removed.

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


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

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


Green filter test..

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.


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


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.

Webcam using AForge.NET (Part II)


Now to use webcam and I will go with the USB camera. I know the name of the camera (from the tutorial Part I, pop up box) as “USB2.0 Camera” and it is index as number 4 (from 0). You need to draw a picture box and a button to start the camera in the design view like this


Double click on the “Start Camera” button and we go to the code side. I am posting the code with comments. This will probably help you if you read the code.


And it works !!!!!!!!!!!!!!!!!!!!!!!!!

 Ok, here is another video from a different site. I am not the author. But just a crash programming.

C# Tutorial - Getting Images from Webcam using Aforge from shivam on Vimeo.

Here is the link to the source of this video and author


Up coming tutorials will be on image processing. I will be posting as I am learning.



Webcam using AForge.NET (Part I)


Last tutorial we saw about setting up the environment to use AForge.NET. That was not a necessary tutorial, but it was my first post to see how to use windows live writer tool. And also that will give a sense satisfaction for people who have used OpenCV and trying AForge for the first time.

So now we have a window look like this. ( you can refer the previous tutorial to see how we set this environment).



Now the fun part. Programming !!!!!!!!!!!!!!!!!!!!!!!!!!

I am an Electrical guy, therefore according to records I am not a good programmer.

Press the “F7” function key to go the see the code of this form.

[or] right click on the form and select “View Code”


             It is the same.

We will see a page like this


This is called the “code behind”. The previous one is called the “design view”. These are non standard terminologies used in programming environment. Just keep yourself updated because we’ll be using this in the upcoming tutorials.

Add these two namespaces


Before we use the webcam the code has to see the webcam (or ) the program has to know that there is a web cam attached to the computer. So here is our first aim

Aim : To list the USB camera attached to my computer


Put a button in the form. So when we click on the button, it is going to popup box for each camera.


Double click on the button and it will create a function. They call it the event handler. So when the applications is running and we click on the button, it will go to this function.


Now we are going to add code into this function so when we click the button it should list the USB camera attached to this computer (or laptop in my case). I will add the code and then explain the code with inline comments. I don’t want to add more screenshots and make this boring. So look at the following screenshot and try to read and understand the inline comments which will explain what is what.


Now lets compile the code using the shortcut key (im sorry, im used to it. Sort of fun) [ Ctrl + Shift + b ] or you can directly run it by pressing the function key “F5”. But “F5” will properly execute only if the code has no errors.

Lets see what result I got when I ran this program. I got 2 Camera’s in my laptop. So lets see if it lists them all. The following screenshots are my outputs.

I got 2 outputs !!!

1st Camera - HP Webcam



2nd Camera – USB camera



Awesome!!!!!!!!!!!!!!!!!!! AForge.NET got the list of camera’s connected to my computer.

 Next tutorial is how to read video from a USB camera (a webcam in my case).



AForge.NET <> Visual Studio 2010 Setup (Tutorial)


First we have to download AForge.NET from here ---> AForge.NET library Download. Assuming we are using a Windows OS, download the latest .exe. After downloading install it.

Once installed we will be able to see the binaries in here

C:\Program Files (x86)\AForge.NET\Framework\Release   - For 64 bit OS

C:\Program Files\AForge.NET\Framework\Release  - For 32 bit OS


Now I assume we already have Visual studio installed in you computer. I will be demoing with VS2010 (even 2008 is compatible).

File –> New –> Project

Select “Windows Forms Application”



Then we get a window like this



Add AForge.NET libraries as reference. To do this right click on Reference (in Solution explorer).



The browse to the folder where we installed the AForge.NET libraries

C:\Program Files (x86)\AForge.NET\Framework\Release   - For 64 bit OS

C:\Program Files\AForge.NET\Framework\Release  - For 32 bit OS



Since our next tutorial is going to be on using AForge.NET with Webcam, we are going to add only the required binaries (.dll) namely Aforge.Video.dll and Aforge.DirectShow.dll.


On click “OK” we should be able to see the binaries (.dll) listed in the solution explorer under the reference folder like this



Done !!!! Now we have our environment ready to do some basic webcam stuff.

See you in next tutorial.