原文:Win8Metro(C#)数字图像处理--2.25二值图像距离变换



[函数名称]

二值图像距离变换函数DistanceTransformProcess(WriteableBitmap
src)

[算法说明]

 二值图像的距离变换实际上就是将二值图像转换为灰度图像,在二值图像中我们将图像分为目标图像和背景图像,假设目标图像像素值为1,即为白色,背景像素为0即为黑色。在转换后的幅灰度图像中,每个连通域的各个像素点的灰度级与该像素点到其背景像素的最近距离有关。其中灰度级最大点的集合为目标图像的骨架,就是目标图像中心部分的像素的集合,灰度级反应了背景像素与目标图像边界的影响关系。用数学语言表示如下:

 假设二值图像I包含一个连通域S,其中有目标O和背景B,距离图为D,则距离变换定义如下:

 

 

 

距离变换的具体步骤为:

     1,将图像中的目标像素点分类,分为内部点,外部点和孤立点。

以中心像素的四邻域为例,如果中心像素为目标像素(值为1)且四邻域都为目标像素(值为1),则该点为内部点。如果该中心像素为目标像素,四邻域为背景像素(值为0),则该中心点为孤立点,如下图所示。除了内部点和孤立点之外的目标区域点为边界点。

       6,对于孤立点保持不变。

 以上的距离变换方法由于计算量大,比较耗时,因此在实际应用中,我们采用一种倒角模版算法,只需要对图像进行两次扫描就可以实现距离变换。该方法称为Chamfer倒角距离变换法。

 该方法使用两个模版,分别为前向模版和后向模板,如下图所示:

 

 计算步骤如下:

 1,使用前向模板,对图像从上到下,从左到右进行扫描,模板中心0点对应的像素值如果为0则跳过,如果为1则计算模板中每个元素与其对应的像素值的和,分别为Sum1,Sum2,Sum3,Sum4,Sum5,而中心像素值为这五个和值中的最小值。

 2,使用后向模板,对图像从下到上,从右到左进行扫描,方法同上。

 3,一般我们使用的模板为3*3和5*5,分别如下图所示:

 

                          

[函数代码]

       ///<summary>

       ///
Distance transform of binary image.

       ///</summary>

       ///<param
name="src">The source image.</param>

       ///<returns></returns>

       publicstaticWriteableBitmap
DistanceTransformProcess(WriteableBitmap src)////25二值图像距离变换

       {

           if
(src !=null)

           {

               int
w = src.PixelWidth;

               int
h = src.PixelHeight;

               WriteableBitmap
expansionImage =newWriteableBitmap(w,
h);

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

               int
t1, t2, t3, t4, t5, min = 0;

               for
(int y = 0; y < h; y++)

               {

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

                   {

                       if
(y == 0 || x == 0)

                       {

                           temp[x + y * w * 4] = 0;

                           temp[x + 1 + y * w * 4] = 0;

                           temp[x + 2 + y * w * 4] = 0;

                       }

                       else

                       {

                           if
(temp[x + y * w * 4] != 0)

                           {

                               t1 = temp[x - 3 + (y - 1) * w * 4] + 4;

                               t2 = temp[x + (y - 1) * w * 4] + 3;

                               t3 = temp[x + 3 + (y - 1) * w * 4] + 4;

                               t4 = temp[x - 3 + y * w * 4] + 3;

                               t5 = temp[x + y * w * 4];

                               min = GetMin(t1, t2, t3, t4, t5);

                               temp[x + y * w * 4] = (byte)min;

                               temp[x + 1 + y * w * 4] = (byte)min;
temp[x + 2 + y * w * 4] = (byte)min;

                           }

                           t2 = 0; t3 = 0; t4 = 0; t5 = 0; min = 0;

                       }

                   }

               }

               for
(int y = h - 2; y > 0; y--)

               {

                   for
(int x = w * 4 - 4; x > 0; x -= 4)

                   {

                       if
(y == 1 || x == 3)

                       {

                           temp[x + y * w * 4] = 0;

                           temp[x + 1 + y * w * 4] = 0;

                           temp[x + 2 + y * w * 4] = 0;

                       }

                       else

                       {

                           if
(temp[x + y * w * 4] != 0)

                           {

                               t1 = temp[x - 3 + (y + 1) * w * 4] + 4;

                               t2 = temp[x + (y + 1) * w * 4] + 3;

                               t3 = temp[x + 3 + (y + 1) * w * 4] + 4;

                               t4 = temp[x + 3 + y * w * 4] + 3;

                               t5 = temp[x + y * w * 4];

                               min = GetMin(t1, t2, t3, t4, t5);

                               temp[x + y * w * 4] = (byte)min;

                               temp[x + 1 + y * w * 4] = (byte)min;
temp[x + 2 + y * w * 4] = (byte)min;

                           }

                           t2 = 0; t3 = 0; t4 = 0; t5 = 0; min = 0;

                       }

                   }

               }

               Stream
sTemp = expansionImage.PixelBuffer.AsStream();

               sTemp.Seek(0,
SeekOrigin.Begin);

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

               return
expansionImage;

           }

           else

           {

               returnnull;

           }

       }

       privatestaticint
GetMin(int a,
int b,int
c,int d,int
e)

       {

           int
t = (a < b ? a : b) < c ? (a < b ? a : b) : c;

           return
((t < d ? t : d) < e ? (t < d ? t : d) : e);

       }

[图像效果]

最新文章

  1. logstash无法使用conf启动
  2. iOS基于MBProgressHUD的二次封装,一行搞定,使用超简单
  3. LINUX下如何查看tomcat运行状态,判断其是否启动
  4. HDU_2018——母牛产小牛的问题,递推
  5. PHP学习路上的一点心得
  6. OAuth及第三方登录
  7. Smart.coder每日站立会议08
  8. 将自己的域名代理到Gitpages
  9. 教你如何绘制数学函数图像——numpy和matplotlib的简单应用
  10. 关于jQuery.when()用法
  11. Docker 国内仓库和镜像
  12. NOIP2011提高组 选择客栈
  13. mybatis插入语句空值没有设置jdbcType报错
  14. lzo文件压缩,解压
  15. HighChart 体验之旅 (后台传递JSON参数和数据的方法)
  16. 关于beginPath()和closePath()的关系&gt;&gt;canvas的beginPath和closePath分析总结,包括多段弧的情况
  17. 四、利用EnterpriseFrameWork快速开发基于WCF为中间件的三层结构系统
  18. 【前端安全】 web前端安全编码(模版篇)【转】
  19. android应用:TVlauncher源码分析之Androidmanifest.json
  20. hdu 2768(建图,最大点独立集)

热门文章

  1. Xcode经常使用插件使用及自己主动生成帮助文档
  2. Android中密码输入内容可见性切换
  3. C++ 与 Java 语言对比
  4. iOS 9和xcode7设置
  5. SecureCRT连接虚拟机中的Linux
  6. java-线程-ABCABC
  7. Java爬虫框架WebMagic入门——爬取列表类网站文章
  8. 经典图形的绘制(matlab)
  9. vue 遇到的问题
  10. CUDA查询和选取设备信息