Основной алгоритм гистограммы выглядит примерно так:
int[] hist = new hist[256];
//at this point dont forget to initialize your vector with 0s.
for(int i = 0; i < height; ++i)
{
for(int j = 0 ; j < widthl ++j)
{
hist[ image[i,j] ]++;
}
}
Алгоритм суммирует, сколько пикселей со значением 0 у вас есть, сколько со значением = 1 и так далее.
Основная идея состоит в том, чтобы использовать значение пикселя в качестве индекса для позиции гистограммы, где вы будете считать.
У меня есть одна версия этого алгоритма, написанная для 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);
}
static public BitmapData SetImageToProcess(Bitmap image, Rectangle roi)
{
if (image != null)
return image.LockBits(
roi,
ImageLockMode.ReadWrite,
image.PixelFormat);
return null;
}
Надеюсь, я смогу вам помочь.