Click here to Skip to main content
12,885,268 members (42,953 online)
Rate this:
Please Sign up or sign in to vote.
See more: C#

I've written a code to determine the total number of cars in a road in terms of it's lights and I've succeeded!

but the result with some pictures not exactly true!! there is unwanted blobs such as reflections on the road or on the car it self !!

how can I calculate the distance between blobs and how to solve this problem ??


the original image[]

the processed image[]


the original image []

the processed image[]

The Code :

    private void Count_Click(object sender, EventArgs e)
            // create filters
            AForge.Imaging.Filters.ContrastCorrection Contrast = new ContrastCorrection(int.MaxValue);
            AForge.Imaging.Filters.BrightnessCorrection Brightness = new BrightnessCorrection(-200);
            // apply the filters
            AForge.Imaging.UnmanagedImage UnManagedImg = AForge.Imaging.UnmanagedImage.FromManagedImage((Bitmap)TargetPic.Image);
            TargetPic.Image = UnManagedImg.ToManagedImage();
            // creat instance of BlobCounter
            BlobCounter LightsCounter = new BlobCounter();
            LightsCounter.MinHeight = 5;
            LightsCounter.MinWidth = 5;
            LightsCounter.MaxHeight = 20;
            LightsCounter.MaxWidth = 20;
            LightsCounter.FilterBlobs = true;
            Blob[] Lights = LightsCounter.GetObjectsInformation();
            // create Graphics object to draw on the picture
            Graphics graphics = Graphics.FromImage((Bitmap)TargetPic.Image);
            // creat a pen
            Pen BluePen = new Pen(Color.Blue, 1);
            // check the lights
            foreach (Blob Light in Lights)
                    List<IntPoint> EP = LightsCounter.GetBlobsEdgePoints(Light);
                    graphics.DrawPolygon(BluePen, ToPointsArray(EP));
                catch (Exception) { MessageBox.Show("Error"); }

            label.Text = "The Total # Of Cars Is :" + (Lights.Length) / 2;
        catch (Exception) { MessageBox.Show("Load an image !!"); }

      private System.Drawing.Point[] ToPointsArray(List<IntPoint> points)
    System.Drawing.Point[] array = new System.Drawing.Point[points.Count];
    for (int i = 0, n = points.Count; i < n; i++)
        array[i] = new System.Drawing.Point(points[i].X, points[i].Y);
    return array;



How to improve this code to work with any image and give true result ??
Posted 30-Jan-13 7:14am

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

You can do it in two ways: before blob recognition and after.

Before — just use yes another filter:[^]

In response to your first question on blob recognition, I advised you to use brightness and contrasting filters:
image processing using >> I need the help[^].

If you use the blob filtering using the filter referenced above, you should apply those brightness and contrasting filters before blob filtering. Note that this blob filtering has nothing to do with blob enumeration you already know.

Now, after:

So, you can use this blob enumeration. You should do it after all other filters are applied. From the AForge.NET demo application you can see the properties of each blob:[^].

So, you can always traverse the collection of all blobs and remove the blobs you don't want for further considerations, based on whatever criteria you need.

Finally, you can combine this approach with filtering. Basically, filtering criteria can only be pretty simple (typically, by X/Y size of a block), but final selection can be based on the criteria you want, so using both approaches can be useful.

Sergey Alexandrovich Kryukov 30-Jan-13 14:44pm
You are very welcome to come back, of course, but you should at least start finding and reading AForge.NET documentation.

You see, I started to use this library for my real work just a week or two before you asked your questions on the topic; and it worked very well for me. I never had to ask anyone. If I could do it, you can, too.

lovely phantom 30-Jan-13 14:48pm

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

    Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web02 | 2.8.170424.1 | Last Updated 30 Jan 2013
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100