C#写的图像细化算法

自己用C#写的图像细化算法,输入图像为Bitmap类型,输出也是同样的类型,注意<pre name="code" class="csharp">ToThinner(Bitmap srcImg)
中的输入图像srcImg必须为像素0和255的二值化的图像。


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; j++)
                {
                    if (data[i * step + j] > 128)
                        data[i * step + j] = 0;
                    else
                        data[i * step + j] = 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; j++)
                    {
                        data1[i * step1 + j] = 0;
                    }
                }

                for (int i = 2; i < ih - 2; i++)
                {
                    for (int j = 2; j < iw - 2; j++)
                    {
                        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)];
                            }
                        }

                        //(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 - 2; j++)
                        data[i * step + j] = data1[i * step1 + j];
            }

            for (int i = 2; i < ih - 2; i++)
            {
                for (int j = 2; j < iw - 2; j++)
                {
                    if (data[i * step + j] == 1)
                        data[i * step + j] = 0;
                    else
                        data[i * step + j] = 255;
                }
            }
            srcImg.UnlockBits(dstData);
            NImg.UnlockBits(dstData1);
            return (srcImg);
        }

<img src="http://img.blog.csdn.net/20140813102748875?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvbW9vbmxpZ2h0cmFu/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast" alt="" />
如上图就是用细化算法细化的图像。


</pre>

C#写的图像细化算法

时间: 2024-08-03 06:33:55

C#写的图像细化算法的相关文章

【MATLAB】图像细化算法

细化算法 图像细化(Image Thinning),一般指二值图像的骨架化(Image Skeletonization)的一种操作运算. 所谓的细化就是经过一层层的剥离,从原来的图中去掉一些点,但仍要保持原来的形状,直到得到图像的骨架.骨架,可以理解为图象的中轴. 评价指标: 收敛性 保证细化后细线的连通性 保持原图的基本形状 减少笔画相交处的畸变 细化结果是原图像的中心线 细化的快速性和迭代次数少 其中: 非迭代算法: 一次即产生骨架,如基于距离变换的方法.游程长度编码细化等 迭代算法: 重复

hilditch图像细化算法python实现

import cv2 import numpy as np import matplotlib.pyplot as plt # hilditch thining def hilditch(img): # get shape H, W, C = img.shape # prepare out image out = np.zeros((H, W), dtype=np.int) out[img[..., 0] > 0] = 1 # inverse pixel value tmp = out.copy

基于matlab的经典图像边缘检测算法

图像边缘检测算法 (1)Robert算子边缘检测 (2)Sobel算子边缘检测 (3)Prewitt算子边缘检测 (4)LOG算子边缘检测 (5)Canny边缘检测 Matlab的实现. 其实还只是掉包侠,一点算法没有写 争取有空用openCV写一遍 I=imread('1.jpg'); I0=rgb2gray(I); subplot(231); imshow(I); BW1=edge(I0,'Roberts',0.16); subplot(232); imshow(BW1); title('R

【opencv】图像细化

[opencv]图像细化 [opencv]图像细化 2014-02-17 21:03 5404人阅读 评论(14) 收藏 举报  分类: opencv(1)  版权声明:本文为博主原创文章,未经博主允许不得转载. 在我们进行图像处理的时候,有可能需要对图像进行细化,提取出图像的骨架信息,进行更加有效的分析. 图像细化(Image Thinning),一般指二值图像的骨架化(Image Skeletonization) 的一种操作运算. 所谓的细化就是经过一层层的剥离,从原来的图中去掉一些点,但仍

Python 手写数字识别-knn算法应用

在上一篇博文中,我们对KNN算法思想及流程有了初步的了解,KNN是采用测量不同特征值之间的距离方法进行分类,也就是说对于每个样本数据,需要和训练集中的所有数据进行欧氏距离计算.这里简述KNN算法的特点: 优点:精度高,对异常值不敏感,无数据输入假定 缺点:计算复杂度高,空间复杂度高 适用数据范围:数值型和标称型(具有有穷多个不同值,值之间无序)    knn算法代码: #-*- coding: utf-8 -*- from numpy import * import operatorimport

OpenCV图像细化的一个例子

转自:http://blog.csdn.net/zfdxx369/article/details/9091953?utm_source=tuicool 本文是zhang的一篇经典图像细化论文,效果很好,采用并行计算,速度非常快; 下文是 "智慧视觉"在CSDN上对这篇论文程序的一个改造,亲测可用! 由于OpenCV没有自带的图像细化函数,网上提供的基本是基于1.0接口的,于是乎动手搞成2.0 Mat类型接口的,方便好用.细化方法当中,当属经典的Zhang并行快速细化算法,细化之后的轮廓

图像缩放算法【转】

转自:http://blog.csdn.net/qq_21792169/article/details/51020005 版权声明:本文为Linux_Google原创文章,转载请加上原创链接. 转载别人的,但是这篇文章写得确实太好了,所以想分享出来,可是原创文章地址找不到了 ,很可惜. 图像缩放算法 摘要:首先给出一个基本的图像缩放算法,然后一步一步的优化其速度和缩放质量: 高质量的快速的图像缩放 全文 分为:      上篇 近邻取样插值和其速度优化      中篇 二次线性插值和三次卷积插值

c++opencv中线条细化算法

要达到的效果就是将线条尽量细化成单像素,按照论文上的Hilditch算法试了一下,发现效果不好,于是自己尝试着写了一下细化的算法,基本原理就是从上下左右四个方向向内收缩. 1.先是根据图片中的原则确定了以下16种情况 2.调试过后发现,迭代次数多了之后,原来连接着的线条会断开,分析原因如下图 3.修改了一下判断条件 4.调试过后发现还是会出现断的地方,再次分析原因如下图 5.又加了判断条件,如下图 最终实现的效果如下   对比图 对规则曲线的效果比较好 但是圆的效果不太好,有待改进 附上代码,测

11、图像缩放算法

正文: 为了便于讨论,这里只处理32bit的ARGB颜色:  代码使用C++;涉及到汇编优化的时候假定为x86平台;使用的编译器为vc2005;  为了代码的可读性,没有加入异常处理代码;  测试使用的CPU为AMD64x2 4200+(2.37G)  和 Intel Core2 4400(2.00G); 速度测试说明:  只测试内存数据到内存数据的缩放  测试图片都是800*600缩放到1024*768; fps表示每秒钟的帧数,值越大表示函数越快 //////////////////////