图像灰度化 直方均衡

图像灰度化将图像中的所有像素按照 0.3*r+0.59*g+0.11*b 的方式灰度处理

直方均衡算法

1.统计每个灰度阶像素的数目

2.求每个灰度级占总数的概率

3.求每个灰度级的累积概率 c[]

4.按照c[src[x][y]]*range+min 确定新图像的灰度,其中range是这个图像的灰度范围,min是整个图像最小的灰度值

int maxScale=1;
            for (int y = 0; y < bmp.Height; y++)
            {
                for (int x = 0; x < bmp.Width; x++)
                {
                    Color c = bmp.GetPixel(x, y);
                    int grayscale =(int) (0.3*c.R +0.59* c.G +0.11*c.B);     //换RGB到灰度(亮度)
                    Color temp = Color.FromArgb(100,grayscale,grayscale,grayscale);
                    bmp.SetPixel(x,y,temp);
                    scales[grayscale]++;                       //该亮度的统计增一
                    if (scales[grayscale] > max)
                        maxScale = scales[grayscale];          //记住最大的数值
                    if (grayscale > max)
                        max = grayscale;          //记住最大的数值
                    if (grayscale < min)
                        min = grayscale;          //记住最小的数值
                }
            }
            for (int i = 0; i < scales.Length; i++)
                scales[i] = scales[i] * 255 / maxScale;        //把亮度数组缩小到0~255区间,以便用图像直观表示出来
            Bitmap result = new Bitmap(256, 256);              //准备一个直方图
            using (Graphics g = Graphics.FromImage(result))
            {
                for (int x = 0; x < result.Height; x++)
                    g.DrawLine(Pens.Black, x, result.Height, x, result.Height - scales[x]);  //每个色阶画一条线,长度依据该色阶的统计数值
            }

            for (int y = 0; y < bmp.Height; y++)
            {
                for (int x = 0; x < bmp.Width; x++)
                {
                    Color c = bmp.GetPixel(x, y);
                    grayscale=(int)(p[c.R]*range+min);
                    Color temp = Color.FromArgb(100,grayscale,grayscale,grayscale);
                    bmp.SetPixel(x, y, temp);
                }
            }



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值