直方图均衡化

直方图均衡化:

增强图像的对比度,增强图像的视觉效果,提高图像成分的清晰度;便于计算机处理: 原因 : 

颜色直方图包含了图像中的颜色信息,反应了颜色的数量特征,它描述的是不同色彩在整幅图相中所占的比例,并不关心每种色彩所处的控件位置,即无法描述图像中的对象或物体。

其主要步骤为:

(1)求出原图像直方图;

(2)根据原直方图求出灰度值的变换表

(3)X=H(x),这里H(x)是对1、2、中的灰度值变换进行查表变换操作,得出对应的像素点新的灰度值;(模糊的短图像用到此方法进行预处理);

其基本原理:

反映数字图像的概貌性描述,例如图像的灰度范围,灰度的分布,整幅图像的平均亮度和阴暗对比度等,并可由此得出进一步处理的重要依据。直方图均衡化也叫直方图均匀化,就是把给定图像的直方图分布改变成均匀分布的直方图,它是一种常用的灰度增强算法。

代码示例如下:

 

 

核心代码如下: 

 1  public void PicAverage(string path, int hight, int length, string resultPath) 
 2         {
 3             byte[,] pic = this.GetPic(path, hight, length);
 4             int size = hight * length;
 5           
 6             int[] graydenss = new int[256];
 7             for (int i = 0; i < hight; i++)
 8             {
 9                 for (int j = 0; j < length; j++)
10                 {
11                     graydenss[Convert.ToInt16(pic[i, j])] += 1;//直方图均值化                    
12                 }
13             }
14                      
15             for (int i = 1; i < 256; i++)//计算分布率
16             {
17                 graydenss[i] += graydenss[i - 1];
18             }
19             float fNormalizeConstant = (float)255 / size;
20             for (int i = 0; i < 255; i++)
21             {
22                 graydenss[i] = Convert.ToInt16(graydenss[i] * fNormalizeConstant);
23             }
24 
25             byte[,] picResult = new byte[hight, length];
26             for (int i = 0; i < hight; i++)
27             {
28                 for (int j = 0; j < length; j++)
29                 {
30                     float color = Convert.ToSingle(pic[i, j]);
31                     float colorResult = 0;
32                     if (color == 0)
33                     {
34                         colorResult = 0;
35                     }
36                     else
37                     {
38                         colorResult = Convert.ToSingle(graydenss[Convert.ToInt16(pic[i, j])]);                      
39                     }
40                     picResult[i, j] = Convert.ToByte(colorResult);
41                 }
42             }
43 
44             this.SaveAveragePic(resultPath, hight, length, picResult);//保存处理好的图片
45         }
原文地址:https://www.cnblogs.com/sumuncle/p/5619259.html