2012-12-25 49 views
0

我試圖繪製圖像與Aforge.net不工作

Graphics g = Graphics.FromImage(BWImage); 
     Pen red = new Pen(Color.Red, 2); 
     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); 

      g.DrawPolygon(red, ToPointsArray(hull)); 

     } 

我從一個攝像頭框架上,併爲每個幀我做一些處理然後執行該代碼繪製圖像,但它沒有工作,我試圖調試它,我發現該行

Graphics g = Graphics.FromImage(BWImage); 

它繼續執行而不執行任何行後!
請幫忙嗎?謝謝。

這裏是我的代碼

private void video_NewFrame(object sender, NewFrameEventArgs eventArgs) 
    { 
     Bitmap bitmap = (Bitmap)eventArgs.Frame.Clone(); 

     ProcessImage(bitmap); 
    } 

和processImage來方法:

#region Skin Detection 
     YCbCrFiltering YCbCrFilter = new YCbCrFiltering(); 

     YCbCrFilter.Y = new Range(0, 1); 
     YCbCrFilter.Cb = new Range(-0.1862745098039216f, 0.0294117647058824f); 
     YCbCrFilter.Cr = new Range(0.0137254901960784f, 0.2254901960784314f); 
     // apply the filter 
     var YCbCrFilteredImage = YCbCrFilter.Apply(bitmap); 

     #endregion 

     #region GrayScale 

     // create grayscale filter (BT709) 
     Grayscale Gray_filter = new Grayscale(0.2125, 0.7154, 0.0721); 
     // apply the filter 
     Bitmap grayImage = Gray_filter.Apply(YCbCrFilteredImage); 

     Threshold threshold = new Threshold(50); 
     Bitmap BWImage = threshold.Apply(grayImage); 

     #endregion 

     #region Remove noise 
     // create filter 
     BlobsFiltering BlobsFilteringfilter = new BlobsFiltering(); 
     // configure filter 
     BlobsFilteringfilter.CoupledSizeFiltering = true; 
     BlobsFilteringfilter.MinWidth = 150; 
     BlobsFilteringfilter.MinHeight = 150; 
     BlobsFilteringfilter.MaxHeight = 600; 
     BlobsFilteringfilter.MaxWidth = 600; 
     // apply the filter 
     var BWImageFiltered = BlobsFilteringfilter.Apply(BWImage); 
     #endregion 


     #region Fill Holes 
     FillHoles FillHolesfilter = new FillHoles(); 
     FillHolesfilter.MaxHoleHeight = 50; 
     FillHolesfilter.MaxHoleWidth = 50; 
     FillHolesfilter.CoupledSizeFiltering = false; 
     var BWImageFilteredHoles = FillHolesfilter.Apply(BWImageFiltered); 
     #endregion 

     #region Contouring 

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

     Graphics g = Graphics.FromImage(BWImageFilteredHoles); 
     Pen red = new Pen(Color.Red, 2); 
     // create convex hull searching algorithm 
     GrahamConvexHull hullFinder = new GrahamConvexHull(); 
foreach (Blob blob in blobs) 
     { 
      List<IntPoint> AllEdgesPoints = new List<IntPoint>(); 
      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); 

      g.DrawPolygon(red, ToPointsArray(hull)); 
g.Dispose(); 

     #endregion 

     pictureBox1.Image = grayImage; 
     pictureBox2.Image = BWImage; 
    } 
+0

此代碼如何連接到攝像頭?當網絡攝像頭接收到幀時,您是否擁有「事件處理程序」?你從哪裏得到'BWImage'?請看這個示例中的第60行和第93行:http://haryoktav.wordpress.com/2009/03/21/webcam-in-c-aforgenet/ – Cheesebaron

回答

0

是否有未顯示解決此代碼的異常處理程序? Graphics.FromImage()應該拋出異常,因爲你是passing an image with an indexed pixel format; Grayscale.Apply()返回一個圖像Format8bppIndexed,和BlobsFiltering.Apply()將保留。

底線是,你需要與同樣大小創建一個新的Bitmap作爲BWImage,讓您的Graphics從,油漆BWImage到新的圖像,然後矩形的斑點。