このヒストグラムクラスを高速化するにはどうすればよいですか?
-
06-07-2019 - |
質問
これは、8ビットのグレースケール画像のヒストグラムを計算することになっています。 1024x770のテストビットマップでは、CreateTimeは約890msになります。どうすればこれをより速く(方法、方法)速くすることができますか?
編集:これは実際にはまだヒストグラムを計算せず、ビットマップから値を取得するだけであることを言及する必要があります。 8ビットのグレースケール画像からすべてのピクセル値を取得する最も速い方法は何ですか?
public class Histogram {
private static int[,] values;
public Histogram(Bitmap b) {
var sw = Stopwatch.StartNew();
values = new int[b.Width, b.Height];
for (int w = 0; w < b.Width; ++w) {
for (int h = 0; h < b.Height; ++h) {
values[w, h] = b.GetPixel(w, h).R;
}
}
sw.Stop();
CreateTime = (sw.ElapsedTicks /
(double)Stopwatch.Frequency) * 1000;
}
public double CreateTime { get; set; }
}
解決
基本的なヒストグラムアルゴリズムは次のようなものです:
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#向けに記述されたこのアルゴリズムの1つのバージョン(高速)があります。
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;
}
お役に立てば幸いです。
他のヒント
Bitmap.LockBitsメソッドを使用して、ピクセルデータにアクセスします。 これは、プロセスに関する適切なリファレンスです。基本的に、 unsafe
コードを使用してビットマップデータを反復処理する必要があります。
これは、このスレッドに基づいて作成した関数のコピー/貼り付け可能なバージョンです。
安全でないコードはビットマップがFormat24bppRgbであると想定しており、そうでない場合、ビットマップをその形式に変換し、クローンバージョンで動作します。
Format4bppIndexedなどのインデックス付きピクセル形式を使用してビットマップを渡すと、image.Clone()の呼び出しがスローされることに注意してください。
開発マシンの画像9100x2048からヒストグラムを取得するのに約200msかかります。
private long[] GetHistogram(Bitmap image)
{
var histogram = new long[256];
bool imageWasCloned = false;
if (image.PixelFormat != PixelFormat.Format24bppRgb)
{
//the unsafe code expects Format24bppRgb, so convert the image...
image = image.Clone(new Rectangle(0, 0, image.Width, image.Height), PixelFormat.Format24bppRgb);
imageWasCloned = true;
}
BitmapData bmd = null;
try
{
bmd = image.LockBits(new Rectangle(0, 0, image.Width, image.Height), ImageLockMode.ReadOnly,
PixelFormat.Format24bppRgb);
const int pixelSize = 3; //pixels are 3 bytes each w/ Format24bppRgb
//For info on locking the bitmap bits and finding the
//pixels using unsafe code, see http://www.bobpowell.net/lockingbits.htm
int height = bmd.Height;
int width = bmd.Width;
int rowPadding = bmd.Stride - (width * pixelSize);
unsafe
{
byte* pixelPtr = (byte*)bmd.Scan0;//starts on the first row
for (int y = 0; y < height; ++y)
{
for (int x = 0; x < width; ++x)
{
histogram[(pixelPtr[0] + pixelPtr[1] + pixelPtr[2]) / 3]++;
pixelPtr += pixelSize;//advance to next pixel in the row
}
pixelPtr += rowPadding;//advance ptr to the next pixel row by skipping the padding @ the end of each row.
}
}
}
finally
{
if (bmd != null)
image.UnlockBits(bmd);
if (imageWasCloned)
image.Dispose();
}
return histogram;
}