Search code examples
c#opencvobject-detectionemgucv

EmguCV Blob Counter


INPUT IMAGE

Hi I am try to learn EmguCV 3.3 and I have a question about blob counting.As you see in INPUT IMAGE I have black uneven blobs.

I am try to do something like this.

OUTPUT IMAGE

I need to draw rectangle around blobs and count them. I tryied some approches but non of it work. I need Help();


Solution

  • You can use FindCountours() or SimpleBlobDetection() to achieve that, here is an example code uses the first one:

    Image<Gray, Byte> grayImage = new Image<Gray,Byte>(mRGrc.jpg);
    Image<Gray, Byte> canny = new Image<Gray, byte>(grayImage.Size);
    int counter = 0;
    
    using (MemStorage storage = new MemStorage())
    
    for (Contour<Point> contours  = grayImage.FindContours(Emgu.CV.CvEnum.CHAIN_APPROX_METHOD.CV_CHAIN_APPROX_NONE, Emgu.CV.CvEnum.RETR_TYPE.CV_RETR_TREE, storage);contours != null; contours = contours.HNext)
    {
         contours.ApproxPoly(contours.Perimeter * 0.05, storage);
         CvInvoke.cvDrawContours(canny, contours, new MCvScalar(255), new MCvScalar(255), -1, 1, Emgu.CV.CvEnum.LINE_TYPE.EIGHT_CONNECTED, new Point(0, 0));
         counter++;
     }
    
    using (MemStorage store = new MemStorage())
    
    for (Contour<Point> contours1= grayImage.FindContours(Emgu.CV.CvEnum.CHAIN_APPROX_METHOD.CV_CHAIN_APPROX_NONE, Emgu.CV.CvEnum.RETR_TYPE.CV_RETR_TREE, store); contours1 != null; contours1 = contours1.HNext)
    {
        Rectangle r = CvInvoke.cvBoundingRect(contours1, 1);
        canny.Draw(r, new Gray(255), 1);
     }
    
    Console.Writeline("Number of blobs: " + counter);