9

我正在尝试使用 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();
}

必须有比使用固定宽度更好的方法(因为它们可能会有很大差异)

4

1 回答 1

0

如果你想得到一个标记的 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();
    }

问我是否想要不同的东西,我会尽我所能提供帮助:)

于 2016-05-15T17:57:18.713 回答