Click here to Skip to main content
15,867,832 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
I have a problem with counting small objects on the image. In example I used BlobCounter class (from AForge framework). But it doesnt recognize splitted objects. Here is the function output

http://s3.postimg.org/cwkexxucj/result.jpg[^]

Here is the treshold that method produces

http://s29.postimg.org/y0rze5ulj/treshold.jpg[^]

Here is the code

C#
private void count_Objects_Click(object sender, EventArgs e)
        {
            Bitmap sample = (Bitmap)picProcessed.Image.Clone();

           // create grayscale filter
            Grayscale filter = new Grayscale(0.2125, 0.7154, 0.0721);
            // apply the filter
            Bitmap grayImage = filter.Apply(sample);

            // create threshold filter
            SISThreshold filterT = new SISThreshold();
            // apply the filter
            filterT.ApplyInPlace(grayImage);

            //Because blobcounter looks white objects on black background
            //But we have to count black objects
            Invert filterInvert = new Invert();
            // apply the filter
            filterInvert.ApplyInPlace(grayImage);

            BlobCounter blobCounter = new BlobCounter();
            blobCounter.FilterBlobs = true;
            blobCounter.MinWidth = 2; 
            blobCounter.MinHeight = 2;

            blobCounter.CoupledSizeFiltering = true; 
            blobCounter.ProcessImage(grayImage);
            Blob[] blobs = blobCounter.GetObjectsInformation();

            //manipulations to draw borders around founded objects

            // create convex hull searching algorithm
            GrahamConvexHull hullFinder = new GrahamConvexHull();
            Bitmap image = (Bitmap)picProcessed.Image;
            Bitmap clonimage = (Bitmap)picProcessed.Image.Clone();
            BitmapData data = clonimage.LockBits(new Rectangle(0, 0, image.Width, image.Height), ImageLockMode.ReadWrite, image.PixelFormat);


            // process each blob
            foreach (Blob blob in blobs)
            {
                List<IntPoint> leftPoints, rightPoints, edgePoints;
                edgePoints = new List<IntPoint>();

                // get blob's edge points
                blobCounter.GetBlobsLeftAndRightEdges(blob,
                    out leftPoints, out rightPoints);

                edgePoints.AddRange(leftPoints);
                edgePoints.AddRange(rightPoints);

                // blob's convex hull
                List<IntPoint> hull = hullFinder.FindHull(edgePoints);

                Drawing.Polygon(data, hull, Color.Black);
            }

            clonimage.UnlockBits(data);
            picProcessed.Image = clonimage;
            picProcessed.Refresh();

            int count = blobs.Length;
            MessageBox.Show("Total objects found: " + count);
        }


1 First function process image to binary image for blobcount algorithm
2 Then it looks for blobs
3 Then it draws (on original image) borders around blobs

How to split blobs? Or maybe there is other methods for counting objects in image?
Posted

1 solution

There is no such thing as "splitted" objects. From the blob recognition standpoint, they are just different objects (calculated correctly, for this matter). It's just your scenery is not well defined. You can play with contrast and other filters before applying blob recognition. Such manipulations are usual and unavoidable. Only preserve original image, use the filtered one only to feed to the blob recognition.

—SA
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900