/// <summary>
/// 图形细化
/// </summary>
/// <param name="srcImg"></param>
/// <returns></returns>
public unsafe Bitmap ToThinner(Bitmap srcImg)
{
int iw = srcImg.Width;
int ih = srcImg.Height;
bool bModified; //二值图像修改标志
bool bCondition1; //细化条件1标志
bool bCondition2; //细化条件2标志
bool bCondition3; //细化条件3标志
bool bCondition4; //细化条件4标志
int nCount;
//5X5像素块
byte[,] neighbour = new byte[, ];
//新建临时存储图像
Bitmap NImg = new Bitmap(iw, ih, srcImg.PixelFormat);
bModified = true;
//细化修改标志, 用作循环条件
BitmapData dstData = srcImg.LockBits(new Rectangle(, , iw, ih), ImageLockMode.ReadWrite, srcImg.PixelFormat);
byte* data = (byte*)(dstData.Scan0);
//将图像转换为0,1二值得图像;
int step = dstData.Stride;
for (int i = ; i < dstData.Height; i++)
{
for (int j = ; j < dstData.Width * ; j += )
{
if (data[i * step + j] > )
//如果是白线条,只要将0改成1,将1改成0
data[i * step + j]
= data[i * step + j + ]
= data[i * step + j + ]
= ;
else
data[i * step + j]
= data[i * step + j + ]
= data[i * step + j + ]
= ;
}
} BitmapData dstData1 = NImg.LockBits(new Rectangle(, , iw, ih), ImageLockMode.ReadWrite, NImg.PixelFormat);
byte* data1 = (byte*)(dstData1.Scan0);
int step1 = dstData1.Stride;
//细化循环开始
while (bModified)
{
bModified = false;
//初始化临时二值图像NImg
for (int i = ; i < dstData1.Height; i++)
{
for (int j = ; j < dstData1.Width * ; j++)
{
data1[i * step1 + j] = ;
}
}
for (int i = ; i < ih - ; i++)
{
for (int j = ; j < iw * - ; j += )
{
bCondition1 = false;
bCondition2 = false;
bCondition3 = false;
bCondition4 = false;
if (data[i * step + j] == )
//若图像的当前点为白色,则跳过
continue;
for (int k = ; k < ; k++)
{
//取以当前点为中心的5X5块
for (int l = ; l < ; l++)
{
//1代表黑色, 0代表白色
//neighbour[k, l] = bw[i + k - 2, j + l - 2];
//neighbour[k, l] = data[(i + k - 2) * step + (j + l - 2)];
neighbour[k, l] = data[(i + k - ) * step + (j + l * - )];
}
}
//(1)判断条件2<=n(p)<=6
nCount = neighbour[, ] + neighbour[, ] + neighbour[, ] + neighbour[, ] + neighbour[, ] + neighbour[, ] + neighbour[, ] + neighbour[, ];
if (nCount >= && nCount <= )
bCondition1 = true;
else
{
data1[i * step1 + j] = ;
continue;
//跳过, 加快速度
}
//(2)判断s(p)=1
nCount = ;
if (neighbour[, ] == && neighbour[, ] == )
nCount++;
if (neighbour[, ] == && neighbour[, ] == )
nCount++;
if (neighbour[, ] == && neighbour[, ] == )
nCount++;
if (neighbour[, ] == && neighbour[, ] == )
nCount++;
if (neighbour[, ] == && neighbour[, ] == )
nCount++;
if (neighbour[, ] == && neighbour[, ] == )
nCount++;
if (neighbour[, ] == && neighbour[, ] == )
nCount++;
if (neighbour[, ] == && neighbour[, ] == )
nCount++;
if (nCount == )
bCondition2 = true;
else
{
data1[i * step1 + j] = ;
continue;
}
//(3)判断p0*p2*p4=0 or s(p2)!=1
if (neighbour[, ] * neighbour[, ] * neighbour[, ] == )
bCondition3 = true;
else
{
nCount = ;
if (neighbour[, ] == && neighbour[, ] == )
nCount++;
if (neighbour[, ] == && neighbour[, ] == )
nCount++;
if (neighbour[, ] == && neighbour[, ] == )
nCount++;
if (neighbour[, ] == && neighbour[, ] == )
nCount++;
if (neighbour[, ] == && neighbour[, ] == )
nCount++;
if (neighbour[, ] == && neighbour[, ] == )
nCount++;
if (neighbour[, ] == && neighbour[, ] == )
nCount++;
if (neighbour[, ] == && neighbour[, ] == )
nCount++;
if (nCount != )
//s(p2)!=1
bCondition3 = true;
else
{
data1[i * step1 + j] = ;
continue;
}
}
//(4)判断p2*p4*p6=0 or s(p4)!=1
if (neighbour[, ] * neighbour[, ] * neighbour[, ] == )
bCondition4 = true;
else
{
nCount = ;
if (neighbour[, ] == && neighbour[, ] == )
nCount++;
if (neighbour[, ] == && neighbour[, ] == )
nCount++;
if (neighbour[, ] == && neighbour[, ] == )
nCount++;
if (neighbour[, ] == && neighbour[, ] == )
nCount++;
if (neighbour[, ] == && neighbour[, ] == )
nCount++;
if (neighbour[, ] == && neighbour[, ] == )
nCount++;
if (neighbour[, ] == && neighbour[, ] == )
nCount++;
if (neighbour[, ] == && neighbour[, ] == )
nCount++;
if (nCount != )//s(p4)!=1
bCondition4 = true;
}
if (bCondition1 && bCondition2 && bCondition3 && bCondition4)
{
data1[i * step1 + j] = ;
bModified = true;
}
else
{
data1[i * step1 + j] = ;
}
}
}
// 将细化了的临时图像bw_tem[w,h]copy到bw[w,h],完成一次细化
for (int i = ; i < ih - ; i++)
for (int j = ; j < iw * - ; j++)
data[i * step + j] = data1[i * step1 + j];
}
for (int i = ; i < ih - ; i++)
{
for (int j = ; j < iw * - ; j += )
{
if (data[i * step + j] == ) data[i * step + j]
= data[i * step + j + ]
= data[i * step + j + ]
= ; else data[i * step + j]
= data[i * step + j + ]
= data[i * step + j + ]
= ; }
}
srcImg.UnlockBits(dstData);
NImg.UnlockBits(dstData1);
return (srcImg);
}

C# 指针操作图像 细化处理的更多相关文章

  1. C# 指针操作图像 二值化处理

    /// <summary> /// 二值化图像 /// </summary> /// <param name="bmp"></param& ...

  2. 【opencv】图像细化

    [原文:http://blog.csdn.net/qianchenglenger/article/details/19332011] 在我们进行图像处理的时候,有可能需要对图像进行细化,提取出图像的骨 ...

  3. 【OpenCV】三种方式操作图像像素

    OpenCV中,有3种访问每个像素的方法:使用at方法.使用迭代器方法.使用指针 运行如下程序后可以发现使用at方法速度最快. 代码如下: //操作图像像素 #include <opencv2/ ...

  4. OpenCV图像细化的一个例子

    转自:http://blog.csdn.net/zfdxx369/article/details/9091953?utm_source=tuicool 本文是zhang的一篇经典图像细化论文,效果很好 ...

  5. C#指针操作Marshal实例

    static void Main(string[] args) { ,,,}; ,,,}; IntPtr pt = Marshal.AllocHGlobal(a.Length); //从source数 ...

  6. C语言指针操作

    欢迎访问我的新博客:http://www.milkcu.com/blog/ 原文地址:http://www.milkcu.com/blog/archives/pointer-manipulation. ...

  7. Day4:T1小技巧(类似于指针操作)T2搜索+小细节

    Day4:其中有很多小技巧get T1 一直没有听到过像这样的小技巧的略专业名词,有点类似于指针操作,之前有碰到过很多这样的题目 每次都是以不同的形式出现,但是感觉思想还是有点接近的吧(就比如某天有一 ...

  8. C语言数组操作和指针操作谁更高效

    在上一篇博文  代码优化小技巧(持续更新......) 第三条关于数组和指针谁更高效, 意犹未尽, 决定单独拉出一篇来讲 1. 数组和指针操作对比 #include <stdio.h> i ...

  9. Python 编程快速上手 第十七章 操作图像

    前言 在这一章节,讲了关于图像的三个方面的内容: 获得图像的相关信息:例如 RGBA 值,尺寸... 对图像进行编辑操作:例如 旋转,缩放... 在图像上绘制形状:例如 矩形,圆形... [Image ...

随机推荐

  1. js中val()和value的区别

    val()是在有jQuery插件的时候才能用,value是在没有jQuery插件的情况下也能用.val()是jQuery根据原生JS里面的value写出来的函数 $(this).val(); 有四个重 ...

  2. Read N Characters Given Read4 I & II

    The API: int read4(char *buf) reads 4 characters at a time from a file. The return value is the actu ...

  3. Java中hashCode()方法以及HashMap()中hash()方法

    Java的Object类中有一个hashCode()方法: public final native Class<?> getClass(); public native int hashC ...

  4. 解决 spring mvc 3.0 结合 hibernate3.2 使用<tx:annotation-driven>声明式事务无法提交的问题(转载)

    1.问题复现 spring 3.0 + hibernate 3.2 spring mvc使用注解方式:service使用@service注解 事务使用@Transactional 事务配置使用 < ...

  5. 【python】IP地址处理模块IPy

    来源:https://pypi.python.org/pypi/IPy IPy模块 该模块可以方便的处理IPv4和IPv6地址. 以下是从来源中拷贝的一些例子: >>> from I ...

  6. yii 多模板

    main.php: //替换所有模板 //加载文件名为first的模板 //       'theme'=>'theme1', 'components'=>array(           ...

  7. Hibernate查询语句

    1 hql查询 Hibernate的查询语句,hiberante提供的面向对象的查询语言,和sql语句的语法的相似.而且严格区分大小写. 1.1 from字句 /** * hql: from 字句 * ...

  8. Vector_h

    #ifndef VECTOR_H #define VECTOR_H #include <algorithm> template<typename Object> class V ...

  9. hdu1162(最小生成树 prim or kruscal)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1162 意义:给出一些点,用线问使所有点直接或间接连通,需要多长: 思路:裸最小生成树: 法1: pri ...

  10. MVC公开课 – 1.基础 (2013-3-15广州传智MVC公开课)

      1.MVC设计模式 Model 是指 要处理的业务代码和数据操作代码 View 视图 主要是指的 跟用户打交道 并能够展示数据 Controller 看成是 Model和View的桥梁 优点: 1 ...