Detect object from image based on object color by C#

To detect a object based on its color , there is an easy algorithm for that .u have to choose a filtering method . Steps normally are

  • Take the image
  • Apply ur filtering
  • Apply greyscalling
  • subtract background and get ur objects
  • find all the objects position
  • mark the objects

First u have to choose a filtering method , there are many filtering method provided for c#. mainly i prefer aforge filters , for this purpose they have few filter they are

  • ColorFiltering
  • ChannelFiltering
  • HSLFiltering
  • YCbCrFiltering
  • EuclideanColorFiltering

my favorite is EuclideanColorFiltering it is easy and simple. for other filtering u can know more about them here. U have to download Aforge dll for apply these in ur code .

So see EuclideanColorFiltering work first see

we are going to apply a color filter it is 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

well to understand how it work look closely at the code

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

first line select the select color value. you all know color has a value 0 to 255.
by filter.CenterColor = Color.FromArgb( 215, 30, 30 ); i specified my center color will be a red effected color because here value of red is 215, green and blue is 30. and filter.Radius = 100 means that all color value near than 100 in my specified color.
now my filter filters pixels, which color is inside/outside of RGB sphere with specified center and radius – it keeps pixels with colors inside/outside of the specified sphere and fills the rest with specified color.

Now for detect objects i use bitmap data and use lockbits method to understand clearly this method see here . then we make it greyscale algorithom hten 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.UnlockBits(objectsData);

now the object we use blobcounter for that. it is a very strong class that aforge provided.

  blobCounter.MinWidth = 5;
            blobCounter.MinHeight = 5;
            blobCounter.FilterBlobs = true;
            blobCounter.ProcessImage(grayImage);
            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);
                    }

                    g.Dispose();
                }

            }

blobCounter.MinWidth and blobCounter.MinHeight define the smallest size of the object in pixel.
and blobCounter.GetObjectRectangles() return all the objects rectangle position. and using graphics class i draw rectangle over the images.

now if u 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;
 blobCounter.ProcessImage(grayImage);
            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);
                    }
                    g.Dispose();
                           }

now image is like that

but if u want to extract u can use 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 u will get ur object like that

now if u want to draw the rectangles in main image use that image reference in graphics. u will find u result. Hope this can be helpful

see update at this post

Advertisements

About kishordgupta

A software developer
This entry was posted in C#, Image Processing and tagged , , , , , , , . Bookmark the permalink.

16 Responses to Detect object from image based on object color by C#

  1. Sergio Gutiérrez says:

    Thanks man!!..your work is amazing!!

    This topic helped me a lot in my grade project!!…is a ANPR system…thank you very much!

    best regards.

  2. Anonymous says:

    hi,
    nice code examples.. currently,i’m doing my project on ear detection and i’ll try to adapt this code to my project later.. if possible,where can i find the full source code..?

  3. Anonymous says:

    thanks for your genius code; this code helps me a lot with my course project; but i’m doing right now my final project using c#: Quality Qualifier , this project uses image processing, how can we determine that the fruit mango be detected if it is a rejected, local, or maybe we can export this product, mango can be determine maybe by the color inorder this will be qualified for export, local, or rejected, …… thanks you very much sir;

    Christian Fletcher

  4. #trans thank you very much for sharing

  5. Thank you so much for posting this. It helps a lot in our projects…
    My only problem is how to extract the color of the object using RGB only. I only need the color of the object and extract it. Thanks again!

  6. jasmine says:

    this code helped me so much last year , why don’t you make another one based on features of the object not only the color ??

  7. Vinayak says:

    hello Kishor D Gupta
    i want to know is der anyway to detect two colors in d same way?
    i tried using two blob counters but its giving an unmanaged code error.
    can u please help! 🙂

  8. gaurav says:

    thanks

  9. Ibrahim says:

    Hi, I am Ibrahim, a masters student of university of east London. I am currently writing my project on object detection from a 3D image. can you be of assistance to me?

  10. lenny lemor says:

    Hi, great code,
    how resolve the error at this line: filter.CenterColor = Color.FromArgb(0, 0, 0); (line 156 about)
    ‘implicit conversion System,Drawing.Color in Aforge.Imaging.RGB’
    I use the last release of Aforge lib.
    Thank in advance.

    • Anonymous says:

      Hi,
      u need to do it this way :
      Color pixel = your_bitmap.get_pixel(x,y);
      Aforge.imaging.RGB your_color = new RGB(pixel.R,pixel.G,pixel.B,pixel.A);
      filter.centercolor = your_color

      and it works 🙂

  11. ilyes says:

    code that doesn’t compile !

  12. Richie says:

    thanks for the help i am currently having a thesis on meat classificatiob

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s