QQ登录

只需一步,快速开始

登录 | 立即注册 | 找回密码
微像素首页 图像处理 查看内容
订阅

文章

图像基本变换---图像边缘检测(Sobel+Roberts+Previtt+Laplace)

发表时间:2016-5-9 23:31作者:bean 阅读(1306) 评论: 0

导读: 本文详细 介绍图像基本变换---图像边缘检测(Sobel+Roberts+Previtt+Laplace) 算法及实现,并给出了一份完整程序DEMO下载链接,跟大家分享一下,希望大家喜欢!

[Sobel算法说明]

  Sobel算子是边缘检测中一种常用的一阶导数模板算子。模板如下图所示。

Fig.1 Sobel模板算子

  算法过程如公式2-15

其中p(x, y)3*3模板的中心位置像素。

[函数代码]

        /// <summary>

        /// Sobel edge detection.

        /// </summary>

        /// <param name="src">Source image.</param>

        /// <returns></returns>

        public static WriteableBitmap SobelEdgeProcess(WriteableBitmap src)////12 Sobel边缘检测

        {

            if(src!=null )

            {

            int w = src.PixelWidth;

            int h = src.PixelHeight;

            WriteableBitmap sobelImage = new WriteableBitmap(w,h);

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

            byte[] tempMask = (byte[])temp.Clone();

            int b = 0, g = 0, r = 0;

            for (int j = 1; j < h - 1; j++)

            {

                for (int i = 4; i < w * 4 - 4; i += 4)

                {

                    if (i == 0 || i == w - 4 || j == 0 || j == h - 1)

                    {

                        temp[i + j * w * 4] = (byte)0;

                        temp[i + 1 + j * w * 4] = (byte)0;

                        temp[i + 2 + j * w * 4] = (byte)0;

                    }

                    else

                    {

                        b = Math.Abs(tempMask[i - 4 + (j - 1) * w * 4] + 2 * tempMask[i - 4 + j * w * 4] + tempMask[i - 4 + (j + 1) * w * 4] - tempMask[i + 4 + (j - 1) * w * 4]

                            - 2 * tempMask[i + 4 + j * w * 4] - tempMask[i + 4 + (j + 1) * w * 4]) + Math.Abs(tempMask[i - 4 + (j - 1) * w * 4] + 2 * tempMask[i + (j - 1) * w * 4]

                            + tempMask[i + 4 + (j - 1) * w * 4] - tempMask[i - 4 + (j + 1) * w * 4] - 2 * tempMask[i + (j + 1) * w * 4] - tempMask[i + 4 + (j + 1) * w * 4]);

                        g = Math.Abs(tempMask[i - 4 + 1 + (j - 1) * w * 4] + 2 * tempMask[i - 4 + 1 + j * w * 4] + tempMask[i - 4 + 1 + (j + 1) * w * 4] - tempMask[i + 4 + 1 + (j - 1) * w * 4]

                           - 2 * tempMask[i + 4 + 1 + j * w * 4] - tempMask[i + 4 + 1 + (j + 1) * w * 4]) + Math.Abs(tempMask[i - 4 + 1 + (j - 1) * w * 4] + 2 * tempMask[i + 1 + (j - 1) * w * 4]

                           + tempMask[i + 4 + 1 + (j - 1) * w * 4] - tempMask[i - 4 + 1 + (j + 1) * w * 4] - 2 * tempMask[i + 1 + (j + 1) * w * 4] - tempMask[i + 1 + 4 + (j + 1) * w * 4]);

                        r = Math.Abs(tempMask[i - 4 + 2 + (j - 1) * w * 4] + 2 * tempMask[i - 4 + 2 + j * w * 4] + tempMask[i - 4 + 2 + (j + 1) *w * 4] - tempMask[i + 4 + 2 + (j - 1) * w * 4]

                           - 2 * tempMask[i + 4 + 2 + j * w * 4] - tempMask[i + 4 + 2 + (j + 1) * w * 4]) + Math.Abs(tempMask[i - 4 + 2 + (j - 1) * w * 4] + 2 * tempMask[i + 2 + (j - 1) * w * 4]

                           + tempMask[i + 4 + 2 + (j - 1) * w * 4] - tempMask[i - 4 + 2 + (j + 1) * w * 4] - 2 * tempMask[i + 2 + (j + 1) * w * 4] - tempMask[i + 2 + 4 + (j + 1) * w * 4]);

                        temp[i + j * w * 4] = (byte)(b > 0 ? (b < 255 ? b : 255) : 0);

                        temp[i + 1 + j * w * 4] = (byte)(g > 0 ? (g < 255 ? g : 255) : 0);

                        temp[i + 2 + j * w * 4] = (byte)(r > 0 ? (r < 255 ? r : 255) : 0);

                    }

                    b = 0; g = 0; r = 0;

                }

            }

            Stream sTemp = sobelImage.PixelBuffer.AsStream();

            sTemp.Seek(0, SeekOrigin.Begin);

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

            return sobelImage;

            }

            else

            {

                return null;

            }   

        }

[图像效果]

Fig.2原图                                Fig.3效果图



[Roberts边缘检测算法说明]

  Roberts算子是边缘检测中一种常用的一阶导数模板算子。模板如下图所示。

Fig.1 Roberts模板算子

  算法过程如公式2-(16)

  其中p(x,y)3*3模板的中心位置像素。

[函数代码] 

        /// <summary>

        /// Roberts edge detection.

        /// </summary>

        /// <param name="src">Source image.</param>

        /// <returns></returns>

        public static WriteableBitmap RobertEdgeProcess(WriteableBitmap src)////13 Robert边缘检测

        {

            if(src!=null )

            {

            int w = src.PixelWidth;

            int h = src.PixelHeight;

            WriteableBitmap robertImage = new WriteableBitmap(w,h);

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

            byte[] tempMask = (byte[])temp.Clone();

            int b = 0, g = 0, r = 0;

            for (int j = 1; j < h - 1; j++)

            {

                for (int i = 4; i < w * 4 - 4; i += 4)

                {

                    if (i == 0 || i == w - 4 || j == 0 || j == h - 1)

                    {

                        temp[i + j * w * 4] = (byte)0;

                        temp[i + 1 + j * w * 4] = (byte)0;

                        temp[i + 2 + j * w * 4] = (byte)0;

                    }

                    else

                    {

                        b = Math.Abs(tempMask[i + j * w * 4] - tempMask[i - 4 + (j+1) * w * 4]) + Math.Abs(tempMask[i - 4 + j * w * 4] - tempMask[i + (j + 1) * w * 4]);

                        g = Math.Abs(tempMask[i + 1 + j * w * 4] - tempMask[i - 4 + 1 + (j + 1) * w * 4]) + Math.Abs(tempMask[i - 4 + 1 + j * w * 4] - tempMask[i + 1 + (j + 1) * w * 4]);

                        r = Math.Abs(tempMask[i + 2 + j * w * 4] - tempMask[i - 4 + 2 + (j + 1) * w * 4]) + Math.Abs(tempMask[i - 4 + 2 + j * w * 4] - tempMask[i + 2 + (j + 1) * w * 4]);

                        temp[i + j * w * 4] = (byte)(b > 0 ? (b < 255 ? b : 255) : 0);

                        temp[i + 1 + j * w * 4] = (byte)(g > 0 ? (g < 255 ? g : 255) : 0);

                        temp[i + 2 + j * w * 4] = (byte)(r > 0 ? (r < 255 ? r : 255) : 0);

                    }

                    b = 0; g = 0; r = 0;

                }

            }

            Stream sTemp = robertImage.PixelBuffer.AsStream();

            sTemp.Seek(0, SeekOrigin.Begin);

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

            return robertImage;

            }

            else

            {

                return null;

            }   

        }

[图像效果]

Fig.2原图                                Fig.3效果图

[Prewitt 边缘检测算法说明]

  Prewitt算子是边缘检测中一种常用的一阶导数模板算子。模板如下图所示。

Fig.1 Prewitt模板算子

  算法过程如公式2-(17)

  其中p(x,y)3*3模板的中心位置像素。

[函数代码]

        /// <summary>

        /// Smooth edge detection.

        /// </summary>

        /// <param name="src">Source image.</param>

        /// <returns></returns>

        public static WriteableBitmap PrewittEdgeProcess(WriteableBitmap src)////14 Prewitt边缘检测

        {

            if(src!=null )

            {

            int w = src.PixelWidth;

            int h = src.PixelHeight;

            WriteableBitmap smoothImage = new WriteableBitmap(w,h);

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

            byte[] tempMask = (byte[])temp.Clone();

            int b = 0, g = 0, r = 0;

            for (int j = 1; j < h - 1; j++)

            {

                for (int i = 4; i < w * 4 - 4; i += 4)

                {

                    if (i == 0 || i == w - 4 || j == 0 || j == h - 1)

                    {

                        temp[i + j * w * 4] = (byte)0;

                        temp[i + 1 + j * w * 4] = (byte)0;

                        temp[i + 2 + j * w * 4] = (byte)0;

                    }

                    else

                    {

                        b = Math.Abs(tempMask[i - 4 + (j - 1) * w * 4] + tempMask[i + (j - 1) * w * 4] + tempMask[i + 4 + (j - 1) * w * 4]

                            - tempMask[i - 4 + (j + 1) * w * 4] - tempMask[i + (j + 1) * w * 4] - tempMask[i + 4 + (j + 1) * w * 4]) +

                            Math.Abs(tempMask[i + 4 + (j - 1) * w * 4] + tempMask[i + 4 + j * w * 4] + tempMask[i + 4 + (j + 1) * w * 4]

                            - tempMask[i - 4 + (j - 1) * w * 4] - tempMask[i - 4 + j * w * 4] - tempMask[i - 4 + (j + 1) * w * 4]);

                        g = Math.Abs(tempMask[i - 4 + 1 + (j - 1) * w * 4] + tempMask[i + 1 + (j - 1) * w * 4] + tempMask[i + 1 + 4 + (j - 1) * w * 4]

                            - tempMask[i - 4 + 1 + (j + 1) * w * 4] - tempMask[i + 1 + (j + 1) * w * 4] - tempMask[i + 1 + 4 + (j + 1) * w * 4]) +

                            Math.Abs(tempMask[i + 1 + 4 + (j - 1) * w * 4] + tempMask[i + 1 + 4 + j * w * 4] + tempMask[i + 1 + 4 + (j + 1) * w * 4]

                            - tempMask[i - 4 + 1 + (j - 1) * w * 4] - tempMask[i - 4 + 1 + j * w * 4] - tempMask[i - 4 + 1 + (j + 1) * w * 4]);

                        r = Math.Abs(tempMask[i - 4 + 2 + (j - 1) * w * 4] + tempMask[i + 2 + (j - 1) * w * 4] + tempMask[i + 4 + 2 + (j - 1) * w * 4]

                            - tempMask[i - 4 + 2 + (j + 1) * w * 4] - tempMask[i + 2 + (j + 1) * w * 4] - tempMask[i + 4 + 2 + (j + 1) * w * 4]) +

                            Math.Abs(tempMask[i + 4 + 2 + (j - 1) * w * 4] + tempMask[i + 4 + 2 + j * w * 4] + tempMask[i + 4 + 2 + (j + 1) * w * 4]

                            - tempMask[i - 4 + 2 + (j - 1) * w * 4] - tempMask[i - 4 + 2 + j * w * 4] - tempMask[i - 4 + 2 + (j + 1) * w * 4]);

                        temp[i + j * w * 4] = (byte)(b > 0 ? (b < 255 ? b : 255) : 0);

                        temp[i + 1 + j * w * 4] = (byte)(g > 0 ? (g < 255 ? g : 255) : 0);

                        temp[i + 2 + j * w * 4] = (byte)(r > 0 ? (r < 255 ? r : 255) : 0);

                    }

                    b = 0; g = 0; r = 0;

                }

            }

            Stream sTemp = smoothImage.PixelBuffer.AsStream();

            sTemp.Seek(0, SeekOrigin.Begin);

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

            return smoothImage;

            }

            else

            {

                return null;

            }   

        }

[图像效果]

Fig.2原图                                Fig.3效果图


[图像锐化算法说明]

  图像锐化就是补偿图像的轮廓,增强图像的边界及灰度跳变部分,使模糊的图像变得清晰。在这里我们介绍一种常用的彩色图像锐化方法——拉普拉斯锐化。

  拉普拉斯锐化公式如下:

:这里采用的是3*3的拉普拉斯模板。

[函数代码]

        /// <summary>

        /// Sharpening process.

        /// </summary>

        /// <param name="src">Source image.</param>

        /// <returns></returns>

        public static WriteableBitmap SharpeningProcess(WriteableBitmap src)////11 锐化处理

        {

            if(src!=null )

            {

            int w = src.PixelWidth;

            int h = src.PixelHeight;

            WriteableBitmap sharpenImage = new WriteableBitmap(w,h);

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

            byte[] tempMask = (byte[])temp.Clone();

            double b = 0, g = 0, r = 0, srR = 0, srG = 0, srB = 0;

            for (int j = 1; j < h - 1; j++)

            {

                for (int i = 4; i < w * 4 - 4; i += 4)

                {

 

                    srB = tempMask[i + j * w * 4];

                    srG = tempMask[i + 1 + j * w * 4];

                    srR = tempMask[i + 2 + j * w * 4];

                    b = Math.Abs(5 * srB - tempMask[i - 4 + j * w * 4] - tempMask[i + 4 + j * w * 4] - tempMask[i + (j - 1) * w * 4] - tempMask[i + (j + 1) * w * 4]);

                    g = Math.Abs(5 * srG - tempMask[i - 4 + 1 + j * w * 4] - tempMask[i + 4 + 1 + j * w * 4] - tempMask[i + 1 + (j - 1) * w * 4] - tempMask[i + 1 + (j + 1) * w * 4]);

                    r = Math.Abs(5 * srR - tempMask[i - 4 + 2 + j * w * 4] - tempMask[i + 4 + 2 + j * w * 4] - tempMask[i + 2 + (j - 1) * w * 4] - tempMask[i + 2 + (j + 1) * w * 4]);

                    temp[i + j * w * 4] = (byte)(b > 0 ? (b < 255 ? b : 255) : 0);

                    temp[i + 1 + j * w * 4] = (byte)(g > 0 ? (g < 255 ? g : 255) : 0);

                    temp[i + 2 + j * w * 4] = (byte)(r > 0 ? (r < 255 ? r : 255) : 0);

                    b = 0; g = 0; r = 0; srR = 0; srG = 0; srB = 0;

                }

            }

            Stream sTemp = sharpenImage.PixelBuffer.AsStream();

            sTemp.Seek(0, SeekOrigin.Begin);

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

            return sharpenImage;

            }

            else

            {

                return null;

            }   

        }

 [图像效果]

Fig.1原图                       Fig.2效果图(拉普拉斯锐化)


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


鲜花

握手

雷人

路过

鸡蛋
上一篇:图像滤镜---微软自拍APP滤镜实现DEMO下一篇:图像基本变换---图像曝光+反相算法

最新评论

    联系我们
  • QQ:3243218172
  • 邮箱:3243218172@qq.com
  • QQ交流群:567648913
    手机扫描二维码