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;
}
}
图像二值化及掏空算法
最新推荐文章于 2022-02-18 14:23:58 发布