2017-01-24 193 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); –