2017-01-24 165 views
0

我想在C#窗体中使用AForge.NET创建对象检测项目。使用AForge c的对象检测#

我写了这个代码:

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; 
} 

结果总是黑的照片,我不知道为什么。 我使用这张照片试一下代码: enter image description here

但仍然收到一张黑色的照片。 有帮助吗?为什么是这样? 如果你愿意,可以告诉我如何检测图像内的所有物体。

回答

4

你没有在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没有找到你正在寻找的斑点。

+0

但点列表是从IntPoint不是从System.Drawing.Point –

+0

对不起,没有看到,但你仍然需要填充数据到新的数组。我编辑了我的答案。 –

+0

我仍然得到一个黑色的照片,我想问题是与图像=新的位图(image.Width,image.Height,g); –