[函數名稱]
二值圖像腐蝕函數CorrosionProcess(WriteableBitmap src)
[算法說明]
二值圖像腐蝕操作屬于圖像形态學的範疇,形态學運算是隻針對二值圖像進行,并依據數學形态學(Mathermatical
Morphogy)集合論方法發展起來的數字圖像處理方法,它主要包括腐蝕,膨脹,開,閉,擊中,擊不中等。
圖像形态學運算,要使用結構元素,所謂結構元素是指具有某種确定形狀的基本結構,它的選擇一般要求其具有旋轉不變性或者鏡像不變性,即:結構元素的原點在其幾何中心處,周圍像素關于原點對稱。
在這裡我們選取如下的結構元素:
其中,F為二值圖像原圖,X為結構元素原點所在的二值圖像中的連通域。
假設F中目标像素為255(白色),非目标為0(黑色),當結構元素S原點移動到點(x,y)時,如果S中所有點均包含在X中(X中對應在S中所有點的位置均為255),則在腐蝕後的二值圖像中,對應于S原點的位置為255(白色),否則為0(黑色)。
用通俗的話來說就是:用結構元素作為模闆在原始二值圖像種平滑一遍,掃描圖像的每一個像素,用結構元素中的每一個元素與其覆寫的二值圖像做“與”操作(假設結構元素都為1),如果結果都為1,則二值圖像中對應結構元素原點位置的像素值為1,否則為0。
[函數代碼]
///<summary>
///
Corrosion process.
///</summary>
///<param
name="src">The source image(It should be the binary image).</param>
///<returns></returns>
publicstaticWriteableBitmap
CorrosionProcess(WriteableBitmap src)////21圖像腐蝕運算
{
if
(src !=null)
{
int
w = src.PixelWidth;
h = src.PixelHeight;
WriteableBitmap
corrosionImage =newWriteableBitmap(w,
h);
byte[]
temp = src.PixelBuffer.ToArray();
tempMask = (byte[])temp.Clone();
for
(int j = 0; j < h; j++)
{
for
(int i = 0; i < w ; i ++)
{
if
(i == 0 || i == w - 1 || j == 0 || j == h - 1)
{
temp[i * 4 + j * w * 4] = (byte)255;
temp[i * 4 + 1 + j * w * 4] = (byte)255;
temp[i * 4 + 2 + j * w * 4] = (byte)255;
}
else
if
(tempMask[i * 4 - 4 + j * w * 4] == 255 && tempMask[i * 4 + j * w * 4] == 255 && tempMask[i * 4 + 4 + j * w * 4] == 255
&& tempMask[i * 4 + (j - 1) * w * 4] == 255 && tempMask[i
* 4 + (j + 1) * w * 4] == 255)
{
temp[i * 4 + j * w * 4] = (byte)255;
temp[i * 4 + 1 + j * w * 4] = (byte)255;
temp[i * 4 + 2 + j * w * 4] = (byte)255;
}
else
temp[i * 4 + j * w * 4] = 0;
temp[i * 4 + 1 + j * w * 4] = 0;
temp[i * 4 + 2 + j * w * 4] = 0;
}
}
Stream
sTemp = corrosionImage.PixelBuffer.AsStream();
sTemp.Seek(0,
SeekOrigin.Begin);
sTemp.Write(temp, 0, w * 4 * h);
return
corrosionImage;
}
else
returnnull;
}
}
[圖像效果]