图像二值化及掏空算法

  Bitmap bmp;
            Bitmap bmp2;
            Bitmap bmp3;
            if (this.opnFile.ShowDialog() == DialogResult.OK)
            {
                bmp = new Bitmap(this.opnFile.FileName);
                bmp2 = new Bitmap(this.opnFile.FileName);
                bmp3 = new Bitmap(this.opnFile.FileName);
                this.pictureBox1.Image = bmp;
                pictureBox1.Visible = true;
                for (int i = 0; i < bmp.Width; i++)
                {
                    for (int j = 0; j < bmp.Height; j++)
                    {
                        Color Color1 = bmp.GetPixel(i, j);
                        int red = Convert.ToInt32((Color1.R + Color1.G + Color1.B) / 3);
                        //如果大于128将其设为白色255
                        //精确灰度处理算法 0.3*R+0.59*G+0.11*B
                        //优化算法(30*R+59*G+11*B)/100
                        if (red > 128)    
                            red = 255;
                        else
                            red = 0;
                        bmp2.SetPixel(i, j, Color.FromArgb(red, red, red));
                    
                    }
                }
                //掏空内部点的算法
                for (int m = 1; m < bmp2.Width-1; m++)
                {
                    for (int n = 1; n < bmp2.Height-1; n++)
                    {
                        Color Color0 = bmp2.GetPixel(m, n);
                        Color Color1 = bmp2.GetPixel(m, n - 1);
                        Color Color2 = bmp2.GetPixel(m - 1, n);
                        Color Color3 = bmp2.GetPixel(m + 1, n);
                        Color Color4 = bmp2.GetPixel(m, n + 1);
                        int red;
                        if (Color0.B == 0)
                        {
                            if (Math.Abs(Color1.R - Color0.R) == 0 && Math.Abs(Color2.R - Color0.R) == 0 && Math.Abs(Color3.R - Color0.R) == 0 && Math.Abs(Color4.R - Color0.R) == 0)
                                red = 255;
                            else
                                red = 0;
                        }
                        else
                            red = 255;
                        bmp3.SetPixel(m, n, Color.FromArgb(red, red, red));
                    }
                this.pictureBox2.Image = bmp2;
                this.pictureBox3.Image = bmp3;
                }
            }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值