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

转载于:https://www.cnblogs.com/mahatmasmile/p/4247710.html

C# 指针操作图像 细化处理相关推荐

  1. opencv-之图像细化(直线细化)

    平时在我们图像处理的时候会经常用到图像细化或者称为直线细化,在这里我跟大家分享一下我个人的理解,这是我花了一天的时间才搞明白其中的原理,具体的原理大家可以在其他博主的博客里搜索到,偶有详细的解释,我这 ...

  2. OpenCV(一)图像读取与新建、图像显示、操作图像像素(2种涂色并比较算法优劣、输出RGB)

    目录 一.读取图像与新建图像 1.读取图像 2.新建图像 二.显示图像 1.过程 2.代码 3.运行效果 三.操作图像像素 1.逐RGB涂色(单循环)(快) 1-1.过程 2-2.代码 2-3.运行结 ...

  3. OpenCV 【一】—— OpenCV中数组指针、图像分块计算、指针取像素值与MatToEigen方法,内存对齐

    { Topic1: 高效开辟内存,使适用于大型数组.//开辟新数组,或者开辟新的0或者某一数值的数组/Mat或者Map直接使用memset //大数组操作效率较高 举例1:cv::Mat cv_ncc ...

  4. 【opencv】图像细化

    [opencv]图像细化 [opencv]图像细化 2014-02-17 21:03 5404人阅读 评论(14) 收藏 举报  分类: opencv(1)  版权声明:本文为博主原创文章,未经博主允 ...

  5. 图像处理------图像细化

    图像处理------图像细化 算法流程参考自:图像处理细化算法 参考博文中没有细化算法的代码实现,只有算法的具体流程,在本文中,使用python实现图像细化的代码实现,但其运行效率没有考虑,只为理解算 ...

  6. 图像细化 A fast parallel algorithm for thinning digital patterns

    我这是测试了两个人的代码,似乎有些区别的,第二篇作者贴出来的代码还存在一些bug,我简单修改了一下,实现的效果上似乎是有一下差别,后续看看论文再做评价. 两个方法也都能满足一定的需求. 参考blog: ...

  7. python :图像细化、骨架提取

    (附上详细的解释及稍微修改的代码) 参考链接:http://www.cnblogs.com/xianglan/archive/2011/01/01/1923779.html 原博主的链接. 图像细化: ...

  8. 改进zhang方法图像细化(单像素)

    本文主要实现实现了和讲解图像细化算法,引用论文 "牟少敏,杜海洋,苏平,查绪恒,陈光艺.一种改进的快速并行细化算法[J].微电子学与计算机,2013,(第1期)" ,免费下载地址点 ...

  9. canvas上的像素操作(图像复制,细调)

    canvas上的像素操作(图像复制,细调) 总结 1.操作对象:ImageData 对象,其实是canvas的像素点集合 2.主要操作: var obj=ctx.getImageData(0,0,10 ...

最新文章

  1. python: c_char_p指向的bitmap图像数据,通过c_char_Array最终赋值给PIL的Image对象
  2. 面试:为什么 https 比 http 更安全?
  3. 2639-Bone Collector II (01背包之第k优解)
  4. 细说IIS异常日志 — 你必须知道的功能
  5. 电池图标不见了怎么解决
  6. mysql中alter语句卡死的一个解决方法
  7. ef 子表和父表不同时保存_canon粉不懂镜头参数?我只能嘲笑你
  8. 将Jquery EasyUI中DataGird的数据导入Excel中
  9. Matlab数组及多项式运算
  10. 各个平台下 Perl 源码安装教程
  11. Matlab2017b安装教程及破解失败方法
  12. PS图片压缩教程,教你快速压缩jpg图片文件的大小而又不失真!
  13. 牛客网 - 编程初学者入门训练 - 分支控制(BC50~BC77)
  14. 【C语言】abs()用法及其他绝对值函数
  15. euht网络登录_基于EUHT技术的城轨高速线路车地无线网络解决方案
  16. 中国抗生素产业运行状况与需求前景规模预测报告2022版
  17. Java中创建一个类的所有方式
  18. foxmail设置,服务器备份(很实用)
  19. Stream Collectors - filtering
  20. 解决error mounted is not defined no-undef

热门文章

  1. 『Linux』ArchLinux与VirtualBox的结合「二」
  2. Windows下PowerShell监控Keepalived
  3. RH413-Linux系统下umask测试
  4. Management reporter 2012 与AX 2012
  5. 伪造http请求救急
  6. 利用openmp实现矩阵相乘_MP116:线性代数补习班(4):矩阵的张量积
  7. 响应优先级与zorder
  8. 2018年山西省环境空气质量综合指数平均下降10.8%
  9. 启动虚拟机报错VMware Workstation cannot connect to the virtual machine
  10. PostgreSQL 获取拼音首字母的函数 - 摘自互联网