Find drops in a bitmap

I use AForge.Net to search for a drop in a bitmap, my bitmap looks like this:

Search blobs in AForge.Net

My problem is that AForge.Net only detects one blob when there are two connected blob on the thin line.

In my question, is there an algorithm that identifies that there are two large drops with a thin connection between them? And how do I implement this algorithm in C # or VB?

Image for samples:

Image for samples

+6
source share
4 answers

, OpenCv AForge (, AForge , OpenCv ). # OpenCvSharp nuget. , ++ python, .

, OpenCv blob, blob, , , blob ( ).

, OpenCv , - ( ), findContours, Hull. , , :

using (var src = new Mat(filePath))
using (var gray = new Mat())
{
    using (var bw = src.CvtColor(ColorConversionCodes.BGR2GRAY)) // convert to grayscale
    {
        // invert b&w (specific to your white on black image)
        Cv2.BitwiseNot(bw, gray);
    }

    // find all contours
    var contours = gray.FindContoursAsArray(RetrievalModes.List, ContourApproximationModes.ApproxSimple);
    using (var dst = src.Clone())
    {
        foreach (var contour in contours)
        {
            // filter small contours by their area
            var area = Cv2.ContourArea(contour);
            if (area < 15 * 15) // a rect of 15x15, or whatever you see fit
                continue;

            // also filter the whole image contour (by 1% close to the real area), there may be smarter ways...
            if (Math.Abs((area - (src.Width * src.Height)) / area) < 0.01f)
                continue;

            var hull = Cv2.ConvexHull(contour);
            Cv2.Polylines(dst, new[] { hull }, true, Scalar.Red, 2);
        }

        using (new Window("src image", src))
        using (new Window("dst image", dst))
        {
            Cv2.WaitKey();
        }
    }
}

enter image description here

+7

Perhaps you want to use OpenCV for your project. It is easier and faster.

NuGet: https://www.nuget.org/packages/OpenCvSharp3-AnyCPU/3.3.1.20171117

Mat im = Cv2.ImRead("blob.jpg", ImreadModes.GrayScale);
SimpleBlobDetector detector = SimpleBlobDetector.Create();
KeyPoint[] points = detector.Detect(im);
Mat result = new Mat();
Cv2.DrawKeypoints(im, points, result, Scalar.Red); 
+1
source

All Articles