C#迭代法图像二值化处理

 		private void 图片二值化ToolStripMenuItem_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 T = 0;
                byte maxGray = 0;
                byte minGray = 255;
                int[] countPixel = new int[256];
                double mu1, mu2;
                double numerator, denominator;

                //计算直方图
                for (int i = 0; i < grayValues.Length; i++)
                {
                    temp = grayValues[i];
                    countPixel[temp]++;
                    if (temp > maxGray) 
                    {
                        //最大灰度等级
                        maxGray = temp;
                    }
                    if (temp < minGray) 
                    {
                        //最小灰度等级
                        minGray = temp;
                    }
                }

                //迭代法
                byte oldT;
               
                //初始阈值
                T = oldT = Convert.ToByte((maxGray + minGray) / 2);

                //迭代过程
                do
                {
                    oldT = T;
                    numerator = denominator = 0;

                    //迭代法公式1
                    for (int i = minGray; i <T; i++)
                    {
                        numerator += i * countPixel[i];
                        denominator += countPixel[i];
                    }
                    mu1 = numerator / denominator;

                    numerator = denominator = 0;
                    for (int i = T; i < maxGray; i++)
                    {
                        numerator += i * countPixel[i];
                        denominator += countPixel[i];
                    }

                    mu2 = numerator / denominator;

                    //迭代法公式2
                    T = Convert.ToByte((mu1 + mu2)/2);
                }
                while (T!=oldT);

                //图像二值化
                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;
            }
           
           
        }

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

稻田里展望者

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值