Win8Metro(C#)数字图像处理--2.25二值图像距离变换
原文: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);
}
[图像效果]
Win8Metro(C#)数字图像处理--2.25二值图像距离变换的更多相关文章
- Win8Metro(C#)数字图像处理--2.24二值图像闭运算
原文:Win8Metro(C#)数字图像处理--2.24二值图像闭运算 [函数名称] 二值图像闭运算函数CloseOperateProcess(WriteableBitmap src) [算法说 ...
- Win8Metro(C#)数字图像处理--2.22二值图像膨胀
原文:Win8Metro(C#)数字图像处理--2.22二值图像膨胀 [函数名称] 二值图像膨胀函数DilationProcess(WriteableBitmap src) [算法说明] 膨胀 ...
- Win8Metro(C#)数字图像处理--2.23二值图像开运算
原文:Win8Metro(C#)数字图像处理--2.23二值图像开运算 [函数名称] 二值图像开运算函数OpenOperateProcess(WriteableBitmap src) [算法说明 ...
- Win8Metro(C#)数字图像处理--2.21二值图像腐蚀
原文:Win8Metro(C#)数字图像处理--2.21二值图像腐蚀 [函数名称] 二值图像腐蚀函数CorrosionProcess(WriteableBitmap src) [算法说明] 二值 ...
- Win8Metro(C#)数字图像处理--2.33图像非线性变换
原文:Win8Metro(C#)数字图像处理--2.33图像非线性变换 [函数名称] 图像非线性变换函数NonlinearTransformProcess(WriteableBitmap src ...
- Win8Metro(C#)数字图像处理--2.40二值图像轮廓提取
http://dongtingyueh.blog.163.com/blog/static/4619453201271481335630/ [函数名称] 二值图像轮廓提取 Contour ...
- Win8Metro(C#)数字图像处理--2.34直方图规定化
原文:Win8Metro(C#)数字图像处理--2.34直方图规定化 [函数名称] WriteableBitmap HistogramSpecificateProcess(WriteableBi ...
- Win8Metro(C#)数字图像处理--2.30直方图均衡化
原文:Win8Metro(C#)数字图像处理--2.30直方图均衡化 [函数名称] 直方图均衡化函数HistogramEqualProcess(WriteableBitmap src) [算法说明] ...
- Win8Metro(C#)数字图像处理--2.31灰度拉伸算法
原文:Win8Metro(C#)数字图像处理--2.31灰度拉伸算法 [函数名称] 灰度拉伸函数GrayStretchProcess(WriteableBitmap src) [算法说明] ...
随机推荐
- [tmux] Enable mouse mode in tmux
We'll learn how to use mouse mode in tmux, including enable mouse control for resizing, scrolling an ...
- 用DOM4J包实现对xml文件按属性分离。
转自本人博客:http://www.xgezhang.com/dom4j_xml_separata.html dom4j是一个Java的XML API.类似于jdom.用来读写XML文件的. dom4 ...
- JBoss AS 7之初步了解(The Return Of The King)
watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvam9obl9mX2xhdQ==/font/5a6L5L2T/fontsize/400/fill/I0JBQk ...
- [Angular] Create a custom pipe
For example we want to create a pipe, to tranform byte to Mb. We using it in html like: <div> ...
- php课程 4-14 数组如何定义使用
php课程 4-14 数组如何定义使用 一.总结 1.各种语言键值对取值和赋值赋值表达式左边的特点是什么? 键值对,用于取值和赋值,取值和赋值的左边都是一样的 2.各种语言键值对取值或者赋值的时候如 ...
- [转]erlang ranch
一. ranch app启动: ranch_sup -> ranch_server % 创建ets, 并提供接口给其他进程读写 二. 启动diy app (监听模块: 用ranch_tcp -& ...
- C语言检查本机公网IP并发送邮件
这是一个用来获取本机公网IP地址,并检查是否是配置里保存的IP地址,假设不是,就向指定的邮箱发送一个邮件,报告这个IP地址的一段小代码.放到开机启动中,电脑不设password的时候万一丢了,还能有个 ...
- React Native细节记录
1.环境搭建部分 安装完node后建议设置npm镜像以加速后面的过程(或使用***工具).注意:不要使用cnpm!cnpm安装的模块路径比较奇怪,packager不能正常识别! npm config ...
- 【16.56%】【codeforces 687B】Remainders Game
time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...
- 如何使用egit将本地代码提交到托管平台
本文将讲述如何使用eclipse中的egit插件,将代码提交到git托管平台. Eclipse版本:4.7.0 自带egit插件 云端托管平台:华为软件开发云 1.本地git工具安装&环境配置 ...