图像基本变换---图像形态学算法(膨胀+腐蚀+开运算+闭运算)

二值图像腐蚀函数

[算法说明]

  二值图像腐蚀操作属于图像形态学的范畴,形态学运算是只针对二值图像进行,并依据数学形态学(Mathermatical Morphogy)集合论方法发展起来的数字图像处理方法,它主要包括腐蚀,膨胀,开,闭,击中,击不中等。

  图像形态学运算,要使用结构元素,所谓结构元素是指具有某种确定形状的基本结构,它的选择一般要求其具有旋转不变性或者镜像不变性,即:结构元素的原点在其几何中心处,周围像素关于原点对称。

  在这里我们选取如下的结构元素:

图像基本变换---图像形态学算法(膨胀+腐蚀+开运算+闭运算)_第1张图片

  我们假设结构元素为S,则腐蚀算法如下:

  其中,F为二值图像原图,X为结构元素原点所在的二值图像中的连通域。

  假设F中目标像素为255(白色),非目标为0(黑色),当结构元素S原点移动到点(x,y)时,如果S中所有点均包含在X(X中对应在S中所有点的位置均为255),则在腐蚀后的二值图像中,对应于S原点的位置为255(白色),否则为0(黑色)

  用通俗的话来说就是:用结构元素作为模板在原始二值图像种平滑一遍,扫描图像的每一个像素,用结构元素中的每一个元素与其覆盖的二值图像做“与”操作(假设结构元素都为1),如果结果都为1,则二值图像中对应结构元素原点位置的像素值为1,否则为0
[函数代码]

        /// 

        /// Corrosion process.

        /// 

        /// The source image(It should be the binary image).

        /// 

        public static WriteableBitmap CorrosionProcess(WriteableBitmap src)////21图像腐蚀运算

        {

            if (src != null)

            {

                int w = src.PixelWidth;

                int h = src.PixelHeight;

                WriteableBitmap corrosionImage = new WriteableBitmap(w, h);

                byte[] temp = src.PixelBuffer.ToArray();

                byte[] 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

            {

                return null;

            }   

        }

[图像效果]

图像基本变换---图像形态学算法(膨胀+腐蚀+开运算+闭运算)_第2张图片

Fig.1原图                                Fig.2效果图



二值图像膨胀函数

[算法说明]

  膨胀算法也是属于形态学算法的范畴,前一节已经简单介绍了形态学,这里不再累赘。

  我们这里介绍的膨胀算法依旧采用上一节腐蚀中的结构元素S,则算法过程如下:

  用通俗的话讲就是,用结构元素作为模板在原始二值图像种平滑一遍,扫描图像的每一个像素,用结构元素中的每一个元素与其覆盖的二值图像做“或”操作(假设结构元素都为1),如果结果为1,则二值图像中对应结构元素原点位置的像素值为1,否则为0

[函数代码]

        /// 

        /// Dilation process.

        /// 

        /// The source image(It should be the binary image).

        /// 

        public static WriteableBitmap DilationProcess(WriteableBitmap src)////22图像膨胀运算

        {

            if (src != null)

            {

                int w = src.PixelWidth;

                int h = src.PixelHeight;

                WriteableBitmap dilationImage = new WriteableBitmap(w, h);

                byte[] temp = src.PixelBuffer.ToArray();

                byte[] 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 = dilationImage.PixelBuffer.AsStream();

                sTemp.Seek(0, SeekOrigin.Begin);

                sTemp.Write(temp, 0, w * 4 * h);

                return dilationImage;

            }

            else

            {

                return null;

            }

        }

[图像效果]

图像基本变换---图像形态学算法(膨胀+腐蚀+开运算+闭运算)_第3张图片

Fig.1原图                                Fig.2效果图




二值图像开运算函数

[算法说明]

  开运算就是先进性一次腐蚀后进行一次膨胀。算法过程如公式2-(27)

[函数代码]

        /// 

        /// Open operate process.

        /// 

        /// The source image(It should be the binary image).

        /// 

        public static WriteableBitmap OpenOperateProcess(WriteableBitmap src)////23图像开运算

        {

            if (src != null)

            {

                WriteableBitmap temp = DilationProcess(CorrosionProcess(src));

                return temp;

            }

            else

            {

                return null;

            }

        }

[图像效果]

图像基本变换---图像形态学算法(膨胀+腐蚀+开运算+闭运算)_第4张图片

Fig.1原图                                Fig.2效果图



二值图像闭运算函数

[算法说明]

  闭运算就是先进性一次膨胀后进行一次腐蚀。算法过程如公式2-(28)

[函数代码]

        /// 

        /// Close operate process.

        /// 

        /// The source image(It should be the binary image).

        /// 

        public static WriteableBitmap CloseOperateProcess(WriteableBitmap src)////24图像闭运算

        {

            if (src != null)

            {

                WriteableBitmap temp = CorrosionProcess(DilationProcess(src));

                return temp;

            }

            else

            {

                return null;

            }

        }

[图像效果]

图像基本变换---图像形态学算法(膨胀+腐蚀+开运算+闭运算)_第5张图片

Fig.1原图                                Fig.2效果图


demo 下载: http://www.zealfilter.com/forum.php?mod=viewthread&tid=30&extra=page%3D1

你可能感兴趣的:(图像基础)