有時為強化影像之輪廓可以採用"Laplacian 邊緣偵測",強化影像邊緣。

其概念是梯度的散度,即是針對X 軸與Y 軸分別二次偏微分,然後相加。

 

此處以3*3的 「遮罩」為例,透過「Spatial Convolution」對整張影像進行處理。

 

針對影像進行"Laplacian 邊緣偵測"實驗成果如下圖所示 :

 


C#

private static Bitmap laplacian(Bitmap bitmap)
{
    int width = bitmap.Width, height = bitmap.Height;
    int w = 3, h = 3;
    Bitmap dstBitmap = new Bitmap(bitmap);

    byte[,] pix =ImageExtract.getimageArray(bitmap);
    byte[,] resPix = new byte[3, width * height];

    for (int y = 1; y < (height - 1); y++)
    {
        for (int x = 1; x < (width - 1); x++)
        {
            //b,g,r 
            for (int c = 0; c < 3; c++)
            {
                //mask
                int current = x + y * width;
                byte[] mask = new byte[w * h];
                for (int my = 0; my < h; my++)
                    for (int mx = 0; mx < w; mx++)
                    {
                        int pos = current + (mx - 1) + ((my - 1) * width);
                        mask[mx + my * w] = pix[c, pos];
                    }

                resPix[c, current] = LaplacianMask33(mask);
            }
        }
    }
    ImageExtract.writeImageByArray(resPix, dstBitmap);
    return dstBitmap;
}
private static byte LaplacianMask33(byte[] gate)
{
    int[] mask ={
                    -1,-1,-1,
                    -1,8,-1,
                    -1,-1,-1,
            };
    double result = 0;

    for (int i = 0; i < gate.Length; i++)
        result += (double)((gate[i] * mask[i]));
    return (byte)((result > 255) ? 255 : (result < 0) ? 0 : result);
}

完整程式碼 : https://github.com/Lung-Yu/ImageToolBox/blob/master/ImageProcessToolBox/Laplacian.cs

arrow
arrow
    文章標籤
    Image Process space processing
    全站熱搜

    Lung-Yu,Tsai 發表在 痞客邦 留言(1) 人氣()