本文主要是介绍Win8Metro(C#)数字图像处理--2.30直方图均衡化,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
[函数名称]
直方图均衡化函数HistogramEqualProcess(WriteableBitmap src)
[算法说明]
直方图均衡化,又叫做直方图修平,是对图像进行非线性拉伸,重新分配图像像素值,把原始图像的灰度直方图从比较集中的某个灰度区间转换为全部灰度范围内的均匀分布,这样就增加了像素灰度值的动态范围,达到增强图像整体对比度的效果。
[函数代码]
/// <summary>
/// Histogram equalization process.
/// </summary>
/// <param name="src">The source image.</param>
/// <returns></returns>
public static WriteableBitmap HistogramEqualProcess(WriteableBitmap src)30图像直方图均衡化
{
if (src != null)
{
int w = src.PixelWidth;
int h = src.PixelHeight;
WriteableBitmap histogramEqualImage = new WriteableBitmap(w, h);
byte[] temp = src.PixelBuffer.ToArray();
byte gray;
int[] tempArray = new int[256];
int[] countPixel = new int[256];
byte[] pixelMap = new byte[256];
for (int i = 0; i < temp.Length; i += 4)
{
gray = (byte)(temp[i] * 0.114 + temp[i + 1] * 0.587 + temp[i + 2] * 0.299);
countPixel[gray]++;
}
for (int i = 0; i < 256; i++)
{
if (i != 0)
{
tempArray[i] = tempArray[i - 1] + countPixel[i];
}
else
{
tempArray[0] = countPixel[0];
}
pixelMap[i] = (byte)(255 * tempArray[i] * 4 / temp.Length + 0.5);
}
for (int i = 0; i < temp.Length; i+=4)
{
gray = temp[i];
temp[i] = pixelMap[gray];
gray = temp[i+1];
temp[i+1] = pixelMap[gray];
gray = temp[i+2];
temp[i+2] = pixelMap[gray];
}
Stream sTemp = histogramEqualImage.PixelBuffer.AsStream();
sTemp.Seek(0, SeekOrigin.Begin);
sTemp.Write(temp, 0, w * 4 * h);
return histogramEqualImage;
}
else
{
return null;
}
}
[图像效果]
这篇关于Win8Metro(C#)数字图像处理--2.30直方图均衡化的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!