.
图24 灰度图像
3.灰度图像处理
【例12】改善对比度。
算法说明:本例根据特定的输入输出灰度转换关系,增强了图像灰度,处理后图像的中等灰度值增大,图像变亮。
注意:本例中描述对比度改善的输入、输出灰度值对应关系的程序段为:
lev= 80; wid =100;
for (x=0;x<256;x+=1) {
lut[x]=255; }
for (x=lev;x<(lev+wid);x++) {
dm =((double)(x-lev)/(double)wid)*255f; lut [x] =(int)(dm); }
(1)在窗体上添加一个“对比度”命令按钮。 (2)双击“对比度”命令按钮,编辑代码如下:
private void button5_Click(object sender, EventArgs e) {
Color c = new Color();
Bitmap box1 = new Bitmap(pictureBox1.Image); Bitmap box2 = new Bitmap(pictureBox1.Image); int rr, x, m, lev, wid; int[] lut = new int[256];
int [,,]pic=new int[600,600,3]; double dm; lev = 80; .
.
wid = 100;
for (x = 0; x < 256; x += 1) {
lut[x] = 255; }
for (x = lev; x < (lev + wid); x++) {
dm = ((double)(x - lev) / (double)wid) * 255f; lut[x] = (int)dm; }
for (int i = 0; i < pictureBox1.Image.Width - 1; i++) {
for (int j = 0; j < pictureBox1.Image.Height; j++) {
c = box1.GetPixel(i, j); pic[i, j, 0] = c.R; pic[i, j, 1] = c.G; pic[i, j, 2] = c.B; } }
for (int i = 0; i < pictureBox1.Image.Width - 1; i++) {
for (int j = 0; j < pictureBox1.Image.Height; j++) {
m = pic[i, j, 0]; rr=lut[m];
Color c1 = Color.FromArgb(rr, rr, rr); box2.SetPixel(i,j,c1); }
pictureBox2.Refresh(); pictureBox2.Image = box2; } }
(3)运行程序,运行结果如图25所示。
.
.
图25 改善对比度
.