2015-02-24 68 views
0

我有這個圖像,我想刪除背景以隔離綠色圖片。背景不是完全黑色,但它包含一些與綠色圖片內其他像素顏色相同的像素。 enter image description here 我已經使用這個刪除背景後的位圖中的剩餘部分

private void ButtonFilterClick(object sender, EventArgs e) 
{ 
    PixelFormat pxf = PixelFormat.Format24bppRgb; 
    Bitmap bitmap = ((Bitmap)(_smartLabForm.pictureBox1.Image)); 
    Rectangle rect = new Rectangle(0, 0, bitmap.Width, bitmap.Height); 
    BitmapData bmpData = 
    bitmap.LockBits(rect, ImageLockMode.ReadWrite, pxf); 
    IntPtr ptr = bmpData.Scan0; 
    int numBytes = bmpData.Stride * bitmap.Height; 
    byte[] rgbValues = new byte[numBytes]; 
    Marshal.Copy(ptr, rgbValues, 0, numBytes); 
    for (int counter = 0; counter < rgbValues.Length; counter += 3) 
    { 
    if (rgbValues[counter] < 15 && 
     rgbValues[counter + 1] < 15 && 
     rgbValues[counter + 2] < 15) 
    { 
     rgbValues[counter] = 255; 
     rgbValues[counter + 1] = 255; 
     rgbValues[counter + 2] = 255; 
    } 
    } 
    Marshal.Copy(rgbValues, 0, ptr, numBytes); 
    bitmap.UnlockBits(bmpData); 
    _smartLabForm.Refresh(); 
} 

什麼,我得到的是: enter image description here

我怎樣才能去除「噪音」剩下的無損傷綠色的圖像而不會影響性能? 謝謝?

回答

1

這實際上是計算機視覺中一個相當複雜的話題(image segmentation)。涵蓋先進的技術將過於寬泛。但這裏有個快速而簡單的想法,可能會完成這項工作:

提高閾值,使所有背景像素都低於此閾值。在檢查像素是否應該被移除時,還要將某個鄰域中的所有像素(例如圓形半徑)與閾值進行比較。如果他們是全部低於閾值,只能刪除它。

通過這種方式,您可以在靠近特徵區域時更積極地去除像素。

+1

_當檢查像素是否應該被移除時,還將某個鄰域(例如圓形半徑)中的所有像素與閾值進行比較._這是關鍵。也重複這個過程,直到它看起來不錯。他目前的門檻似乎沒有問題,但實際上__summing up__所有鄰居的價值將有所幫助。 – TaW 2015-02-24 10:32:19

+0

我已經使用這個鏈接http://www.codeproject.com/Articles/336915/Connected-Component-Labeling-Algorithm,它似乎工作非常好,但不幸的是一些像素在圖像周圍存活。謝謝 – MartinaLabMath 2015-02-25 09:57:08