Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
281 views
in Technique[技术] by (71.8m points)

c# - Image processing with lockbits, alternative to getpixel?

I am trying to increase my image detection class using lockbits, yet this cause problems with the code and thus it does not run. How can i go about using lockbits and getpixel at the same time in order to speed up image detection, or can someone show me an alternative which is just as fast?

code:

static IntPtr Iptr = IntPtr.Zero;
    static BitmapData bitmapData = null;
    static public byte[] Pixels { get; set; }
    static public int Depth { get; private set; }
    static public int Width { get; private set; }
    static public int Height { get; private set; }

    static public void LockBits(Bitmap source)

    {
            // Get width and height of bitmap
            Width = source.Width;
            Height = source.Height;

            // get total locked pixels count
            int PixelCount = Width * Height;

            // Create rectangle to lock
            Rectangle rect = new Rectangle(0, 0, Width, Height);

            // get source bitmap pixel format size
            Depth = System.Drawing.Bitmap.GetPixelFormatSize(source.PixelFormat);


            // Lock bitmap and return bitmap data
            bitmapData = source.LockBits(rect, ImageLockMode.ReadWrite,
                                         source.PixelFormat);

            // create byte array to copy pixel values
            int step = Depth / 8;
            Pixels = new byte[PixelCount * step];
            Iptr = bitmapData.Scan0;

            // Copy data from pointer to array
            Marshal.Copy(Iptr, Pixels, 0, Pixels.Length);

    }


     static public bool SimilarColors(int R1, int G1, int B1, int R2, int G2, int B2, int Tolerance)
    {
        bool returnValue = true;
        if (Math.Abs(R1 - R2) > Tolerance || Math.Abs(G1 - G2) > Tolerance || Math.Abs(B1 - B2) > Tolerance)
        {
            returnValue = false;
        }
        return returnValue;
    }


     public bool findImage(Bitmap small, Bitmap large, out Point location)
     {
         unsafe
         {
             LockBits(small);
             LockBits(large);
             //Loop through large images width
             for (int largeX = 0; largeX < large.Width; largeX++)
             {
                 //And height
                 for (int largeY = 0; largeY < large.Height; largeY++)
                 {
                     //Loop through the small width
                     for (int smallX = 0; smallX < small.Width; smallX++)
                     {
                         //And height
                         for (int smallY = 0; smallY < small.Height; smallY++)
                         {
                             //Get current pixels for both image
                             Color currentSmall = small.GetPixel(smallX, smallY);
                             Color currentLarge = large.GetPixel(largeX + smallX, largeY + smallY);
                             //If they dont match (i.e. the image is not there)

                             if (!colorsMatch(currentSmall, currentLarge))
                                 //Goto the next pixel in the large image

                                 goto nextLoop;
                         }
                     }
                     //If all the pixels match up, then return true and change Point location to the top left co-ordinates where it was found
                     location = new Point(largeX, largeY);
                     return true;
                 //Go to next pixel on large image
                 nextLoop:
                     continue;
                 }
             }
             //Return false if image is not found, and set an empty point
             location = Point.Empty;
             return false;
         }
     }
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You wouldn't want to rely on getPixel() for image processing; it's okay to make an occasional call to get a point value (e.g. on mouseover), but in general it's preferable to do image processing in image memory or in some 2D array that you can convert to a Bitmap when necessary.

To start, you might try writing a method that using LockBits/UnlockBits to extract an array that is convenient to manipulate. Once you're done manipulating the array, you can write it back to a bitmap using a different LockBits/UnlockBits function.

Here's some sample code I've used in the past. The first function returns a 1D array of values from a Bitmap. Since you know the bitmap's width, you can convert this 1D array to a 2D array for further processing. Once you're done processing, you can call the second function to convert the (modified) 1D array into a bitmap again.

public static byte[] Array1DFromBitmap(Bitmap bmp){
    if (bmp == null) throw new NullReferenceException("Bitmap is null");

    Rectangle rect = new Rectangle(0, 0, bmp.Width, bmp.Height);
    BitmapData data = bmp.LockBits(rect, ImageLockMode.ReadWrite, bmp.PixelFormat);
    IntPtr ptr = data.Scan0;

    //declare an array to hold the bytes of the bitmap
    int numBytes = data.Stride * bmp.Height;
    byte[] bytes = new byte[numBytes];

    //copy the RGB values into the array
    System.Runtime.InteropServices.Marshal.Copy(ptr, bytes, 0, numBytes);

    bmp.UnlockBits(data);

    return bytes;           
}

public static Bitmap BitmapFromArray1D(byte[] bytes, int width, int height)
{
    Bitmap grayBmp = new Bitmap(width, height, PixelFormat.Format8bppIndexed);
    Rectangle grayRect = new Rectangle(0, 0, grayBmp.Width, grayBmp.Height);
    BitmapData grayData = grayBmp.LockBits(grayRect, ImageLockMode.ReadWrite, grayBmp.PixelFormat);
    IntPtr grayPtr = grayData.Scan0;

    int grayBytes = grayData.Stride * grayBmp.Height;
    ColorPalette pal = grayBmp.Palette;

    for (int g = 0; g < 256; g++){
        pal.Entries[g] = Color.FromArgb(g, g, g);
    }

    grayBmp.Palette = pal;

    System.Runtime.InteropServices.Marshal.Copy(bytes, 0, grayPtr, grayBytes);

    grayBmp.UnlockBits(grayData);
    return grayBmp;
}

These methods makes assumptions about the Bitmap pixel format that may not work for you, but I hope the general idea is clear: use LockBits/UnlockBits to extract an array of bytes from a Bitmap so that you can write and debug algorithms most easily, and then use LockBits/UnlockBits again to write the array to a Bitmap again.

For portability, I would recommend that your methods return the desired data types rather than manipulating global variables within the methods themselves.

If you've been using getPixel(), then converting to/from arrays as described above could speed up your code considerably for a small investment of coding effort.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...