2016-04-24 118 views
1

畫我用這個例子: http://www.aforgenet.com/framework/features/blobs_processing.htmlC#AForge.Net圖像處理圖像

我用最後一個例子,並顯示在圖片框輸出按鈕,點擊後嘗試:

using AForge; 
using AForge.Imaging; 
using AForge.Imaging.Filters; 
using AForge.Math.Geometry; 
using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Drawing.Imaging; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
using System.Windows.Forms; 


namespace Image_Processing_testings 
{ 
public partial class Form1 : Form 
{ 
    Bitmap image = null; 
    public Form1() 
    { 
     InitializeComponent(); 
     Bitmap bitmap = new Bitmap("C:\\Users\\user\\Desktop\\test.png"); 
     Bitmap gsImage = Grayscale.CommonAlgorithms.BT709.Apply(bitmap); 

     DifferenceEdgeDetector filter = new DifferenceEdgeDetector(); 
     image = filter.Apply(gsImage); 



     // process image with blob counter 
     BlobCounter blobCounter = new BlobCounter(); 
     blobCounter.ProcessImage(image); 
     Blob[] blobs = blobCounter.GetObjectsInformation(); 

     // create convex hull searching algorithm 
     GrahamConvexHull hullFinder = new GrahamConvexHull(); 

     // lock image to draw on it 
     BitmapData data = image.LockBits(
      new Rectangle(0, 0, image.Width, image.Height), 
       ImageLockMode.ReadWrite, image.PixelFormat); 
     int i = 0; 
     // process each blob 
     foreach (Blob blob in blobs) 
     { 
      List<IntPoint> leftPoints, rightPoints, edgePoints = new List<IntPoint>(); 

      // get blob's edge points 
      blobCounter.GetBlobsLeftAndRightEdges(blob, 
       out leftPoints, out rightPoints); 

      edgePoints.AddRange(leftPoints); 
      edgePoints.AddRange(rightPoints); 

      // blob's convex hull 
      List<IntPoint> hull = hullFinder.FindHull(edgePoints); 


      Drawing.Polygon(data, hull, Color.Red); 
      i++; 
     } 

     image.UnlockBits(data); 


     MessageBox.Show("Found: " + i + " Objects"); 
    } 

    private void button1_Click_1(object sender, EventArgs e) 
    { 
     pictureBox1.Image = image; 
    } 
} 
} 

結果是我在過濾後獲取圖像,但沒有任何多邊形。

我算斑點的數量和這幅畫拿到3:

Image for processing

+1

您提供的鏈接中的示例假定白色像素屬於對象,黑色像素屬於背景。你提供的圖像是相反的。因此,在應用算法之前反轉圖像,看看是否有效。 – rayryeng

+1

謝謝!工作! – Yogevnn

+0

沒問題。你介意我是否加入答案,以便你能夠接受並接受它? :)我不介意讓代表。 – rayryeng

回答

2

在您所提供的鏈接中的示例假設白色像素屬於對象和黑色像素屬於背景。你提供的圖像是相反的。因此,在應用算法之前顛倒圖像並且應該可以工作。