문제

이것은 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#에 대해 작성된이 알고리즘의 하나의 버전이 있습니다 (빠른). 나는 당신보다 빠르지 만 자유롭게 가져 와서 테스트 할 수 있는지 모르겠습니다. 여기에 코드는 다음과 같습니다.

    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 비트 맵 데이터를 반복하는 코드.

다음은이 스레드를 기반으로 제가 올리는 기능의 사본/ 과거 버전입니다.

안전하지 않은 코드는 비트 맵이 형식 24bpprgb 일 것으로 기대하며, 그렇지 않은 경우 비트 맵을 해당 형식으로 변환하고 클로닝 된 버전에서 작동합니다.

image.clone ()에 대한 호출은 형식 4bppindexed와 같은 색인화 된 픽셀 형식을 사용하여 비트 맵을 전달하면 던져집니다.

내 Dev 시스템의 이미지 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;
    }
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top