這是C#中的一個實現。在我來說,我正常化直方圖當我完成來計算的話。它可能會幫助你
public void Histogram(double[] histogram, Rectangle roi)
{
BitmapData data = Util.SetImageToProcess(image, roi);
if (image.PixelFormat != PixelFormat.Format8bppIndexed)
return;
if (histogram.Length < Util.GrayLevels)
return;
histogram.Initialize();
int width = data.Width;
int height = data.Height;
int offset = data.Stride - width;
unsafe
{
byte* ptr = (byte*)data.Scan0;
for (int y = 0; y < height; ++y)
{
for (int x = 0; x < width; ++x, ++ptr)
histogram[ptr[0]]++;
ptr += offset;
}
}
image.UnlockBits(data);
NormalizeHistogram(histogram, height * width);
}
public void NormalizeHistogram(double[] histogram, int imageArea)
{
for (int i = 0; i < histogram.Length; ++i)
histogram[i] = (double)histogram[i]/imageArea;
}
public void HistogramEqualization(double[] histogram)
{
double[] cdf = new double[Util.GrayLevels];
double sum = 0;
for (int i = 0; i < Util.GrayLevels; i++)
{
sum += histogram[i];
cdf[i] = sum;
}
BitmapData data = Util.SetImageToProcess(image);
unsafe
{
byte* ptr = (byte*)data.Scan0;
int offset = data.Stride - data.Width;
int width = data.Width;
int height = data.Height;
for(int y = 0; y < height; y++)
{
for (int x = 0; x < width; ++x, ++ptr)
ptr[0] = (byte)(cdf[ptr[0]] * ((byte)Util.MaxGrayLevel));
}
ptr += offset;
}
image.UnlockBits(data);
}
static public BitmapData SetImageToProcess(Bitmap image, Rectangle roi)
{
if (image != null)
return image.LockBits(
roi,
ImageLockMode.ReadWrite,
image.PixelFormat);
return null;
}
你能告訴我們你的源代碼嗎? – Andres 2010-02-17 21:32:02
我已經解決了這個問題,感謝你和兩位回答我的人,這真的幫助了我! – maximus 2010-03-02 15:09:39