C#自动阈值选择:Otsu法编程实例(图像分割)

news/2024/7/21 3:53:10 标签: c#, 图像处理
//Otsu阈值
        private void OtsuToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (m_Bitmap != null)
            {
                Rectangle rect = new Rectangle(0, 0, m_Bitmap.Width, m_Bitmap.Height);
                BitmapData bmpData = m_Bitmap.LockBits(rect, System.Drawing.Imaging.ImageLockMode.ReadWrite, m_Bitmap.PixelFormat);
                IntPtr ptr = bmpData.Scan0;
                int bytes = m_Bitmap.Width * m_Bitmap.Height*3;
                byte[] grayValues = new byte[bytes];
                System.Runtime.InteropServices.Marshal.Copy(ptr, grayValues, 0, bytes);

                byte temp = 0;               
                byte maxGray = 0;
                byte minGray = 255;
                int[] countPixel = new int[256];
                double mu1, mu2;

                //计算直方图
                for (int i = 0; i < grayValues.Length; i++)
                {
                    temp = grayValues[i];
                    countPixel[temp]++;
                    if (temp > maxGray)
                    {
                        //最大灰度等级
                        maxGray = temp;
                    }
                    if (temp < minGray)
                    {
                        //最小灰度等级
                        minGray = temp;
                    }
                }
                //Otsu法
                double w1 = 0, w2 = 0;
                double sum = 0;
                int numerator = 0;
                double sigma;
                double tempMax = 0;
                byte T = 0;
                for (int i = minGray; i < maxGray; i++)
                {
                    sum += i * countPixel[i];
                }
                for (int i = minGray; i < maxGray; i++)
                {
                    w1 += countPixel[i];
                    numerator += i * countPixel[i];
                    mu1 = numerator / w1;
                    w2 = grayValues.Length - w1;
                    mu2 = (sum - numerator) / w2;

                    //otsu公式
                    sigma = w1 * w2 * (mu1 - mu2) * (mu1 - mu2);

                    if (sigma>tempMax)
                    {
                        tempMax = sigma;
                        T = Convert.ToByte(i);
                    }
                }
                for (int i = 0; i < bytes; i++)
                {
                    if (grayValues[i] < T)
                    {
                        grayValues[i] = 0;
                    }
                    else
                    {
                        grayValues[i] = 255;
                    }
                }
                System.Runtime.InteropServices.Marshal.Copy(grayValues, 0, ptr, bytes);
                m_Bitmap.UnlockBits(bmpData);
                pictureBox1.Image = m_Bitmap;
            }
        }


http://www.niftyadmin.cn/n/1783373.html

相关文章

继承的基本用法

1.继承的好处&#xff1a;可以减少重复的代码 2.语法&#xff1a; class A : public B A为子类或者派生类B为父类或基类 3.派生类中的成员包含两大部分&#xff1a; 一类是从基类继承过来的&#xff0c;一类是自己增加的成员从基类继承过来的表现其共性&#xff0c;而新增的…

iPhone OS SDK的这些事[安装、下载、版本、实例、脱机文档等资料汇总]

第一次使用iPhone SDK是&#xff0c;如果不清楚版本和操作系统等的关系&#xff0c;会浪费很多的时间进行下载和安装。 以下汇总了一些常见可以尽快使用的方法和参考。 SDK安装 切记选择和自己的mac os系统吻合的版本&#xff0c;下载包都很大&#xff0c;国内需要很长的时间下…

nutch与起点R3集成之笔记(二)

为什么80%的码农都做不了架构师&#xff1f;>>> 在nutch与起点R3集成之笔记&#xff08;一&#xff09;中介绍了在起点R3中添加nutch要用到的索引字段&#xff0c;上述字段建好后&#xff0c;就可以通过nutch抓取一个或多个网站内容&#xff0c;并通过 bin/nutch s…

Canny边缘检测算法原理

Canny算子是在边缘检测的三个指标和三个准则的基础上发展起来的一种很有效的边缘检测方法 三个指标&#xff1a; 好的检测 好的定位 最小响应 3个准则&#xff1a; 信噪比准则 定位精度准则 单边缘响应准则 canny边缘检测算法步骤&#xff1a; 1、用高斯滤波器对对图像进行平…

类的继承方式

1.继承语法&#xff1a; class 子类 : 继承方式 父类 2.继承方式一共三种&#xff1a; 公共继承保护继承私有继承#include<iostream> using namespace std; #include<string>//继承方式 class Base1 { public:int m_a; protected:int m_b; private:int m_c; };//…

普通用户摇身变root!!!

简单的网络*** ***linux系统——普通权限变root! 1、用root建立一个普通用户mary&#xff0c;并切换到mary。 2、我们首先测试一下当前用户的权限 3、进入到/tmp&#xff0c;新建目录abc。 4、执行下列相关命令。并保证最后一行后面的两块红色部分为闪动状态。 5、vim abc.c …

C#高斯平滑算法 :二维高斯卷积代码实例

//高斯平滑处理方法//inputImage 输入图像// outputImage 输出图像//sigema 均方差private void gaussSmooth(double[]inputImage,out double[] outputImage,double sigema){//方差double std2 2 * sigema * sigema;//半径3sigemaint radius Convert.ToInt16(Math.Ceiling(3 …

判断是否联网

判断是否联网 在uses中加入WinInetif InternetCheckConnection(http://www.sina.com.cn,1,0) thenbegin showmessage(在线);endelsebegin showmessage(离线);end; 或直接用ping后&#xff0c;查看(0% loss)或(100% loss)&#xff0c;可知在线或离线