C# 指针操作图像 细化处理
/// <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# 指针操作图像 细化处理的更多相关文章
- C# 指针操作图像 二值化处理
/// <summary> /// 二值化图像 /// </summary> /// <param name="bmp"></param& ...
- 【opencv】图像细化
[原文:http://blog.csdn.net/qianchenglenger/article/details/19332011] 在我们进行图像处理的时候,有可能需要对图像进行细化,提取出图像的骨 ...
- 【OpenCV】三种方式操作图像像素
OpenCV中,有3种访问每个像素的方法:使用at方法.使用迭代器方法.使用指针 运行如下程序后可以发现使用at方法速度最快. 代码如下: //操作图像像素 #include <opencv2/ ...
- OpenCV图像细化的一个例子
转自:http://blog.csdn.net/zfdxx369/article/details/9091953?utm_source=tuicool 本文是zhang的一篇经典图像细化论文,效果很好 ...
- C#指针操作Marshal实例
static void Main(string[] args) { ,,,}; ,,,}; IntPtr pt = Marshal.AllocHGlobal(a.Length); //从source数 ...
- C语言指针操作
欢迎访问我的新博客:http://www.milkcu.com/blog/ 原文地址:http://www.milkcu.com/blog/archives/pointer-manipulation. ...
- Day4:T1小技巧(类似于指针操作)T2搜索+小细节
Day4:其中有很多小技巧get T1 一直没有听到过像这样的小技巧的略专业名词,有点类似于指针操作,之前有碰到过很多这样的题目 每次都是以不同的形式出现,但是感觉思想还是有点接近的吧(就比如某天有一 ...
- C语言数组操作和指针操作谁更高效
在上一篇博文 代码优化小技巧(持续更新......) 第三条关于数组和指针谁更高效, 意犹未尽, 决定单独拉出一篇来讲 1. 数组和指针操作对比 #include <stdio.h> i ...
- Python 编程快速上手 第十七章 操作图像
前言 在这一章节,讲了关于图像的三个方面的内容: 获得图像的相关信息:例如 RGBA 值,尺寸... 对图像进行编辑操作:例如 旋转,缩放... 在图像上绘制形状:例如 矩形,圆形... [Image ...
随机推荐
- 【leetcode】N-Queens
N-Queens The n-queens puzzle is the problem of placing n queens on an n×n chessboard such that no tw ...
- Python操作PDF与Tiff文件
1.PDF文件的合并与拆分 pypdf http://www.douban.com/note/455252403/ http://www.redicecn.com/html/Python/201301 ...
- 64位win系统上面tomcat6启动不了 window不能再本地计算机启动
64位的jdk装完之后,jre的bin目录下面没有client文件夹, 而tomcat6.0.20的默认配置启动在client文件夹下面. 所以打开tomcat6w,在java选项界面,取消Use d ...
- jquery 常用的备忘
1.table 取 tr 的 id值 <table class="table table-striped table-hover table-bordered" id=&q ...
- ACM/ICPC 之 昂贵的聘礼-最短路解法(POJ1062)
//转移为最短路问题,枚举必经每一个不小于酋长等级的人的最短路 //Time:16Ms Memory:208K #include<iostream> #include<cstring ...
- HDU 4865 Peter's Hobby(概率、dp、log)
给出2个影响矩阵,一个是当天天气对湿度的影响,一个是前一天天气对当天天气的影响. 即在晴天(阴天.雨天)发生Dry(Dryish.Damp.Soggy)的概率,以及前一天晴天(阴天.雨天)而今天发生晴 ...
- codeforces 510B. Fox And Two Dots 解题报告
题目链接:http://codeforces.com/problemset/problem/510/B 题目意思:给出 n 行 m 列只有大写字母组成的字符串.问具有相同字母的能否组成一个环. 很容易 ...
- Vi文档
Vi简介 Vi是一种广泛存在于各种UNIX和Linux系统中的文本编辑程序. Vi不是排版程序,只是一个纯粹的文本编辑程序. Vi是全屏幕文本编辑器,它没有菜单,只有命令. Vi不是基于窗口的,所以, ...
- nVivo highlight code中的文本
要highlight nvivo中的code一颗在如图highlight中下拉菜单选择,如coding for all nodes,所有的有归属code的文本都会被高亮.如果选择coding for ...
- oracle学习不错的网站
http://oracle-base.com/articles/linux/rlwrap.php