Win8 Metro(C#)數字圖像處理--2.40二值圖像輪廓提取算法

原文: Win8 Metro(C#)數字圖像處理--2.40二值圖像輪廓提取算法



[函數名稱]html

  二值圖像輪廓提取         ContourExtraction(WriteableBitmap src) 算法

[算法說明]函數

  二值圖像的輪廓提取對於圖像識別,圖像分割有着重要意義。該算法的核心就是將圖像目標的內部點消除。所謂內部點,咱們要根據當前像素點的鄰域來進行判斷,假設鄰域窗口爲3*3窗口,若是當前像素P(x,y)的八個鄰域像素知足以下條件,則該點即內部點:spa

  1P(x,y)爲目標像素,假設目標像素爲黑色0,背景像素爲白色255,那麼P(x,y)=0;.net

  2P(x,y)的八個鄰域像素均爲目標像素0code

  咱們把知足條件的內部點刪除,換爲背景點255,便可獲得圖像輪廓。htm

  內部點以下圖所示:blog

[函數代碼]get

/// <summary>
        /// Contour Extraction process.
        /// </summary>
        /// <param name="src">The source image.</param>
        /// <returns></returns>
        public static WriteableBitmap ContourExtraction(WriteableBitmap src)
        {
            if (src != null)
            {
                int w = src.PixelWidth;
                int h = src.PixelHeight;
                WriteableBitmap srcImage = new WriteableBitmap(w, h);
                byte[] temp = src.PixelBuffer.ToArray();
                byte[] tempMask = (byte[])temp.Clone();
                for (int j = 1; j < h-1; j++)
                {
                    for (int i = 4; i < w*4-4; i+=4)
                    {
                        if ((tempMask[i + j * w * 4] == 0) && (tempMask[i - 4 + j * w * 4] == 0) && (tempMask[i + 4 + j * w * 4] == 0) && (tempMask[i - 4 + (j - 1) * w * 4] == 0)
                            && (tempMask[i - 4 + (j + 1) * w * 4] == 0) && (tempMask[i + (j - 1) * w * 4] == 0) && (tempMask[i + (j + 1) * w * 4] == 0)
                            && (tempMask[i + 4 + (j - 1) * w * 4] == 0) && (tempMask[i + 4 + (j + 1) * w * 4] == 0))
                        {
                            temp[i + j * w * 4] = (byte)255;
                            temp[i + 1 + j * w * 4] = (byte)255;
                            temp[i + 2 + j * w * 4] = (byte)255;
                        }
                    }
                }
                Stream sTemp = srcImage.PixelBuffer.AsStream();
                sTemp.Seek(0, SeekOrigin.Begin);
                sTemp.Write(temp, 0, w * 4 * h);
                return srcImage;
            }
            else
            {
                return null;
            }
        }
相關文章
相關標籤/搜索