.net 白色斑点检测

qyzbxkaa  于 2022-12-20  发布在  .NET
关注(0)|答案(1)|浏览(134)

我正在尝试使用AForge.NET来检测一个图像上的粗白线。
这就像我得到的管道,是我想要的结果后,应用阈值过滤器。
我知道如何检测形状,我已经在做了,但这在任何形状下都不匹配,因为它没有边,也不是圆。
我有检测等边形状的示例代码,但我不知道这是否相关。

public void DetectQuadrilateralType(Bitmap bitmap)
{
    BlobCounter blobCounter = new BlobCounter();
    blobCounter.ProcessImage(bitmap);
    Blob[] blobs = blobCounter.GetObjectsInformation();
    //Graphics object to draw
    Pen pen;
    Graphics g = Graphics.FromImage(bitmap);
    SimpleShapeChecker shapeChecker = new SimpleShapeChecker();

    for (int i = 0; i < blobs.Length; i++)
    {
        List<IntPoint> edgePoints = blobCounter.GetBlobsEdgePoints(blobs[i]);
        List<IntPoint> corners;

        if (i < edgePoints.ToArray().Length && i > -1)
        {
            try
            {
                if (shapeChecker.IsConvexPolygon(edgePoints, out corners))
                {
                    PolygonSubType subType = shapeChecker.CheckPolygonSubType(corners);

                    pen = new Pen(colors[subType], 2);
                    g.DrawPolygon(pen, ToPointsArray(corners));
                    pen.Dispose();
                }
            }
            catch (Exception e) { }
        }
    }
    g.Dispose();
}

这是我想看到的画面:

如果有人知道我如何使用AForge或其他C #/. NET检测白色斑点,我会非常感激。
上面的代码只检测边缘,因此它只检测具有尖锐边缘的形状,这种情况很少发生。

    • 编辑**

我用下面的方法让它工作

public void DetectBigBlobs(Bitmap bitmap)
{
    BlobCounter blobCounter = new BlobCounter();
    blobCounter.ProcessImage(bitmap);
    Rectangle[] rects = blobCounter.GetObjectsRectangles();
    //Graphics object to draw
    Pen pen = new Pen(Color.Red, 2);
    Graphics g = Graphics.FromImage(bitmap);

    foreach (Rectangle rect in rects)
    {
        if (rect.Width > 200 && rect.Height > 150)
        {
            g.DrawRectangle(pen, rect);
        }
    }

    pen.Dispose();
    g.Dispose();
}

一定有比使用固定宽度更好的方法(因为它们可能变化很大)

0md85ypi

0md85ypi1#

如果你想得到一个标记的blob,你可以使用下面的代码

public void DetectBigBlobs(Bitmap bitmap)
    {
        BlobCounter blobCounter = new BlobCounter();

        Graphics g = Graphics.FromImage(bitmap);

        //filtering the blobs before searching for blobs 
        blobCounter.FilterBlobs = true;
        blobCounter.MinHeight = bitmap.Height/3;
        blobCounter.MinWidth = bitmap.Width/3;

        blobCounter.ProcessImage(bitmap);
        Blob[] blobs = blobCounter.GetObjectsInformation();

        foreach (Blob b in blobs)
        { 
            //getting the found blob edgepoints 
            List<IntPoint> edgePoints = blobCounter.GetBlobsEdgePoints(b);
            //if you want to mark every edge point RED 
            foreach (IntPoint point in edgePoints)
                bitmap.SetPixel(point.X, point.Y, Color.Red);
            //if you want to draw a rectangle around the blob 
            g.DrawRectangle(Pens.Blue,b.Rectangle);

        }

        g.Dispose();
    }

问我,如果你想要不同的东西,我会尽我所能帮助:)

相关问题