【问题标题】:Object Detection using AForge c#使用 AForge c# 进行对象检测
【发布时间】:2017-01-25 00:21:22
【问题描述】:

我正在尝试使用 AForge.NET 在 C# windows 窗体项目中创建对象检测。

我写了这段代码:

public void DetectCorners()
{
    // Load image and create everything you need for drawing
    Bitmap image = new Bitmap(@"C:\Users\ssammour\Desktop\Unbenannt.PNG");
    originalPicture.ImageLocation = @"C:\Users\ssammour\Desktop\Unbenannt.PNG";
    BlobCounter blobCounter = new BlobCounter();
    blobCounter.ProcessImage(image);
    Graphics g = this.CreateGraphics();
    Blob[] blobs = blobCounter.GetObjectsInformation();
    SimpleShapeChecker shapeChecker = new SimpleShapeChecker();
    Pen redPen = new Pen(Color.Red);
    for (int i = 0, n = blobs.Length; i < n; i++)
    {
        List<IntPoint> corners;
        List<IntPoint> edgePoints = blobCounter.GetBlobsEdgePoints(blobs[i]);

        if (shapeChecker.IsQuadrilateral(edgePoints, out corners))
        {
            g.DrawPolygon(redPen, ToPointsArray(corners));
            image = new Bitmap(image.Width, image.Height, g);
        }
    }

    // Display
    newPicture.Image = image;
}

private System.Drawing.Point[] ToPointsArray(List<IntPoint> points)
{
    System.Drawing.Point[] array = new System.Drawing.Point[points.Count];
    return array;
}

结果总是黑色照片,我不知道为什么。 我用这张照片来尝试代码:

但仍会收到一张黑色照片。 有什么帮助吗?为什么是这样? 如果你能告诉我如何检测图像中的所有对象。

【问题讨论】:

    标签: c# aforge


    【解决方案1】:

    你没有在 ToPointsArray 中做任何事情。你只是返回一个相同长度的数组。

    你应该这样做(我不知道 IntPoint):

    private System.Drawing.Point[] ToPointsArray(List<IntPoint> points)
    {
        System.Drawing.Point[] array = new System.Drawing.Point[points.Count];
        int i = 0;
        foreach (IntPoint p in points)
        {
            array[i++] = new System.Drawing.Point(p.X, p.Y);
        }
        return array;
    }
    

    此外,您正在破坏您的 for 循环中的图像。此代码有效:

    public void DetectCorners()
    {
        // Load image and create everything you need for drawing
        Bitmap image = new Bitmap(@"C:\Users\ssammour\Desktop\Unbenannt.PNG");
        BlobCounter blobCounter = new BlobCounter();
        blobCounter.ProcessImage(image);
        Bitmap result = new Bitmap(image.Width, image.Height, Graphics.FromImage(image));
        Graphics g = Graphics.FromImage(result);
        g.DrawImage(image,0,0);
        Blob[] blobs = blobCounter.GetObjectsInformation();
        SimpleShapeChecker shapeChecker = new SimpleShapeChecker();
        Pen redPen = new Pen(Color.Red);
        for (int i = 0, n = blobs.Length; i < n; i++)
        {
            List<IntPoint> corners;
            List<IntPoint> edgePoints = blobCounter.GetBlobsEdgePoints(blobs[i]);
    
            if (shapeChecker.IsQuadrilateral(edgePoints, out corners))
            {
                corners.Dump();
                g.DrawPolygon(redPen, ToPointsArray(corners, image.Height));
            }
        }
        result.Save(@"c:\result.png", ImageFormat.Png);
    }
    

    我明白了以下几点:

    (0 0),(574 0),(574 398),(0 398)

    (161 391),(162 390),(165 393),(165 394)

    (301 394),(304 392),(310 398),(303 398)

    (552 398),(558 392),(561 392),(562 398)

    (155 397),(156 396),(157 398),(155 398)

    因此,BlobCounter 似乎没有找到您要查找的 blob。

    【讨论】:

    • 但点列表来自 IntPoint 而不是 System.Drawing.Point
    • 对不起,没看到,但您仍然需要将数据填充到新数组中。我编辑了我的答案。
    • 我仍然得到一张黑色照片,我想问题出在 image = new Bitmap(image.Width, image.Height, g);
    • corners.Dump 有什么作用?我现在收到照片并收到 Blob,但照片上方没有显示任何内容。当我们细化对象时它应该画线
    • 抱歉,corners.Dump() 来自 linqPad,我在这里做了示例。当我运行它时,我在 result.png 中的图像顶部得到了小多边形(和边界矩形)。
    猜你喜欢
    • 2018-10-01
    • 2013-05-06
    • 1970-01-01
    • 1970-01-01
    • 2019-03-12
    • 2016-03-13
    • 2019-12-26
    • 2020-03-11
    • 1970-01-01
    相关资源
    最近更新 更多