Click here to Skip to main content
15,390,701 members
Articles / Desktop Programming / Windows Forms
Posted 6 Jan 2011

Tagged as


101 bookmarked

Detect and Track Objects in Live Webcam Video Based on Color and Size Using C#

Rate me:
Please Sign up or sign in to vote.
4.90/5 (43 votes)
17 Dec 2013CPOL4 min read
Detect and track objects in live webcam video based on color and size using C#.


You can select a color in real time and it tracks that color object and gives you the position. I use the Aforge library for that. I also used .NET Framework 4.0. It is a C# desktop application, it can take up to 25 frames per second. You can change color size any time you want, the color of drawing point will also change.

Image 1


I saw a very interesting project in CodeProject named Making of Lego pit camera. With the help of this project, I thought a real time tracker could be made where the color and object's size could also be changed in real time. and can draw the movement of that object in a bitmap with that color. I used some part of their code, although I used a separate color filter for more accuracy.

Using the Code 

The steps are very simple:

  1. Take videoframe from webcam
  2. Use filtering by given color (here Euclidian filtering is used)  
  3. Make greyscale
  4. Find objects by given size
  5. Find biggest object
  6. Draw object position in bitmap

First, I will explain how my software works. It can start tracking by the default color black and a color range of 120, but you can change that in real time. To change color, use the color dialog to select a color, remember to press OK and check in the solid color box if your required color is present or not.

Image 2

To select the range and define the minimum size of the object, see the next image, there are three numeric updown lists for that. objectsize is calculated in pixels. Now about the views:

Image 3

There are four views, three of them are Aforge video source controls and the other is a PictureBox. The first one shows a normal video, the second one shows all the detected objects, the third box shows only the biggest object, and the fourth one just draws a 0 sign in the biggest object location. See the below image for a clearer view:

Image 4

I also added a RichTextBox which shows the pixel of the biggestrectangle position. Now about the code for connection, I use normal Aforge connection code. 

For that, you need to download the Aforge DLL, and also add a very good video control for showing video. You can add it to your toolbox. Just click a free space on toolbox, choose items-> browse the DLL -> add the control.

using AForge;
using AForge.Video.DirectShow;

Then initialize:

videoDevices = new FilterInfoCollection(FilterCategory.VideoInputDevice);
if (videoDevices.Count == 0)
    throw new ApplicationException();
foreach (FilterInfo device in videoDevices)
    VideoCaptureDevice videoSource = new VideoCaptureDevice(device.MonikerString);
    videoSource.DesiredFrameSize = new Size(320, 240);
    videoSource.DesiredFrameRate = 15;
    videoSourcePlayer1.VideoSource = videoSource;

For videoplayercontrol, add an event in its misc named:

 private void videoSourcePlayer1_NewFrame(object sender, ref Bitmap image)
//work with image

For understanding how filtering works, I will explain using a picture of a jellyfish. Here, I take a red center color for understanding. In my provided software, the user can choose his requested color and size. 

In that function, I apply my color detection code by writing Euclidean filtering and then use blob counter to extract data. I will explain it in detail here. 

To see EuclideanColorFiltering work, first see:


We are going to apply a color filter. It is a very simple code to use euclideanfiltering:

// create filter
EuclideanColorFiltering filter = new EuclideanColorFiltering( );
// set center colol and radius
filter.CenterColor = Color.FromArgb( 215, 30, 30 );
filter.Radius = 100;
// apply the filter
filter.ApplyInPlace( image ); 

Now see the effect:

Image 6

Well, to understand how it works, look closely at the code: 

filter.CenterColor = Color.FromArgb( 215, 30, 30 );
filter.Radius = 100

The first line selects the selected color value. You all know that color has a value 0 to 255. Using filter.CenterColor = Color.FromArgb( 215, 30, 30 ); I specify my center color will be a red effected color because here the value of red is 215, green and blue is 30, and filter.Radius = 100 means that all color values near 100 are in my specified color. Now my filter filters the pixels, and decides which color is inside/outside of RGB sphere with the specified center and radius – it keeps pixels with colors inside/outside of the specified sphere and fills the rest with the specified color. 

Now for detecting objects, I use bitmap data and use the lockbits method. To clearly understand this method, see here. Then, we make it a greyscale algorithom, then unlock it.

BitmapData objectsData = image.LockBits(new Rectangle(0, 0, 
   image.Width, image.Height),ImageLockMode.ReadOnly, image.PixelFormat);
// grayscaling
UnmanagedImage grayImage = 	grayscaleFilter.Apply(new UnmanagedImage(objectsData));
// unlock image

Image 7

Now for the object, we use a blobcounter. It is a very strong class that Aforge provides.

blobCounter.MinWidth = 5;
blobCounter.MinHeight = 5;
blobCounter.FilterBlobs = true;
Rectangle[] rects = blobCounter.GetObjectRectangles();
foreach(Rectangle recs in rects)
if (rects.Length > 0)
    foreach (Rectangle objectRect in rects)
        Graphics g = Graphics.FromImage(image);
        using (Pen pen = new Pen(Color.FromArgb(160, 255, 160), 5))
            g.DrawRectangle(pen, objectRect);


blobCounter.MinWidth and blobCounter.MinHeight define the smallest size of the object in pixels, and blobCounter.GetObjectRectangles() returns all the objects rectangle position, and using the graphics class, I draw a rectangle over the images.

Image 8

Now, if you want to take only the biggest object, there is a method for that:

blobCounter.MinWidth = 5;
blobCounter.MinHeight = 5;
blobCounter.FilterBlobs = true;
blobCounter.ObjectsOrder = ObjectsOrder.Size;
Rectangle[] rects = blobCounter.GetObjectRectangles();
foreach(Rectangle recs in rects)
    if (rects.Length > 0)
       Rectangle objectRect = rects[0];
       Graphics g = Graphics.FromImage(image);
       using (Pen pen = new Pen(Color.FromArgb(160, 255, 160), 5))
           g.DrawRectangle(pen, objectRect);

Now the image is like this:

Image 9

But if you want to extract, you can use the following code:

Bitmap bmp = new Bitmap(rects[0].Width, rects[0].Height);
Graphics g = Graphics.FromImage(bmp);
g.DrawImage(c, 0, 0, rects[0], GraphicsUnit.Pixel);

So you will get your object like that:


For drawing the bitmap, I had to use threading. The thread was called in the videosurecplayer event.

void p(object r)
   {//create image
   Bitmap b = new Bitmap(pictureBox1.Image);
   Rectangle a = (Rectangle)r;
   Pen pen1 = new Pen(Color.FromArgb(160, 255, 160), 3);
   Graphics g2 = Graphics.FromImage(b);
   pen1 = new Pen(color, 3);
   SolidBrush b5 = new SolidBrush(color);
    Font f = new Font(Font, FontStyle.Bold);
   g2.DrawString("o", f, b5, a.Location);//draw the position
   pictureBox1.Image = (System.Drawing.Image)b;
           richTextBox1.Text = a.Location.ToString() + 
			"\n" + richTextBox1.Text + "\n"; ;
   catch (Exception faa)

I have to use invoke to write in the textbox because of cross threading. To send the rectangle object, I use a parameterized thread. The thread aa calls the function p() to draw on the bitmap and write in the textbox.

if (rects.Length > 0)
    Rectangle objectRect = rects[0];

    // draw rectangle around derected object
    Graphics g = Graphics.FromImage(image);

    using (Pen pen = new Pen(Color.FromArgb(160, 255, 160), 5))
        g.DrawRectangle(pen, objectRect);
    int objectX = objectRect.X + objectRect.Width / 2 - image.Width / 2;
    int objectY = image.Height / 2 - (objectRect.Y + objectRect.Height / 2);
    ParameterizedThreadStart t = new ParameterizedThreadStart(p);
    Thread aa = new Thread(t);

Points of Interest

Parameterized threading, cross threading has to be implemented here, also there is a need to apply the invoke method for writing using good threading, and we can increase the framerate. About filtering, Euclidian filtering is more accurate than HSLfiltering or Colorfiltering, yrbrcr filtering. 

Thanks to Andrew Kirillov for his help.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Student University of Memphis
United States United States
Kishor Datta Gupta

Research assistant of University of Memphis

Comments and Discussions

QuestionDetect and Track Objects in Live Webcam Video Based on Color and Size Using C# Pin
Member 1464666414-Nov-19 21:32
MemberMember 1464666414-Nov-19 21:32 
QuestionHelp Pin
Member 1334815913-Aug-17 3:53
MemberMember 1334815913-Aug-17 3:53 
AnswerRe: Help Pin
dr*white27-Aug-18 0:46
Memberdr*white27-Aug-18 0:46 
QuestionControl the servo through color Pin
Member 131708864-May-17 1:09
MemberMember 131708864-May-17 1:09 
QuestionObject tracking Pin
Member 1235279125-Feb-16 21:34
MemberMember 1235279125-Feb-16 21:34 
Questioncs508 Pin
Member 1181264628-Oct-15 14:44
MemberMember 1181264628-Oct-15 14:44 
Questionquestion Pin
Member 115850146-Apr-15 10:00
MemberMember 115850146-Apr-15 10:00 
Questiontwo color at a time Pin
Member 1133883929-Dec-14 7:06
MemberMember 1133883929-Dec-14 7:06 
QuestionStoring the Data Pin
Member 1133195428-Dec-14 12:52
MemberMember 1133195428-Dec-14 12:52 
GeneralMy vote of 5 Pin
csharpbd19-Aug-14 20:37
professionalcsharpbd19-Aug-14 20:37 
QuestionVery good article! Pin
Volynsky Alex17-Dec-13 10:16
professionalVolynsky Alex17-Dec-13 10:16 
GeneralMy vote of 5 Pin
Shohel Ahsan3-Sep-13 21:09
MemberShohel Ahsan3-Sep-13 21:09 
QuestionGot a question.. Pin
ValentinoK.14-Feb-13 14:32
MemberValentinoK.14-Feb-13 14:32 
QuestionI have an urgent question. Pin
ValentinoK.31-Jan-13 19:45
MemberValentinoK.31-Jan-13 19:45 
GeneralMy vote of 5 Pin
ValentinoK.30-Jan-13 18:11
MemberValentinoK.30-Jan-13 18:11 
Questionthe graduation project Pin
Sheboist9-Oct-12 23:03
MemberSheboist9-Oct-12 23:03 
QuestionHow to increase the speed Pin
Ram Dayal Vaishnav2-Jul-12 1:24
MemberRam Dayal Vaishnav2-Jul-12 1:24 
Question(160, 255, 160), 5)) Pin
chamara5425-May-12 21:42
Memberchamara5425-May-12 21:42 
AnswerRe: (160, 255, 160), 5)) Pin
ValentinoK.30-Jan-13 17:40
MemberValentinoK.30-Jan-13 17:40 
GeneralMy vote of 5 Pin
Member 857368824-Apr-12 6:28
MemberMember 857368824-Apr-12 6:28 
GeneralMy vote of 5 Pin
smejko8017-Mar-12 0:06
Membersmejko8017-Mar-12 0:06 
QuestionError Pin
Member 792126825-Feb-12 5:26
MemberMember 792126825-Feb-12 5:26 
AnswerRe: Error Pin
Prasanna170823-Mar-12 3:02
MemberPrasanna170823-Mar-12 3:02 
GeneralRe: Error Pin
Member 792126823-Mar-12 13:32
MemberMember 792126823-Mar-12 13:32 
Questionfunction of unlock bit? Pin
mermaid0911-Nov-11 21:20
Membermermaid0911-Nov-11 21:20 

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.