白色斑点检测

时间:2012-03-20 20:13:43

标签: c# .net image pattern-recognition aforge

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

这是我想要检测的图像:

enter image description here

如果有人知道如何用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();
}

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

1 个答案:

答案 0 :(得分: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();
    }

问我是否想要不同的东西,我会尽可能多地帮助:)