tags:

views:

221

answers:

1

Good morning all,

I am currently implementing a method that accepts two bitmap objects. We can assume that said objects are of equal dimensions etc. The return of the method is a list of Pixel changes (this is stored in a self-made object). This is being developed in an iterative manner so the current implementation was a basic one... simply work through each pixel and compare it to its counterpart. This method for generating changes is slower than acceptable (500ms or so), as such I am looking for a faster process.

Ideas that have crossed my mind are to break down the image into strips and run each comparison on a new thread or to compare zones of the screen as objects first then only examine in detail as required.

current code for your understanding...

       for (int x = 0; x < screenShotBMP.Width; x++)
       {
           for (int y = 0; y < screenShotBMP.Height; y++)
           {
               if (screenShotBMP.GetPixel(x, y) != _PreviousFrame.GetPixel(x, y))
               { 
                 _pixelChanges.Add(new PixelChangeJob(screenShotBMP.GetPixel(x,y), x, y));
               }
           }
       }

As you will deduct from the code the concept of the class in question is to take a screenshot and generate a list of pixel changes from the previously taken screenshot.

Thanks for your time.

+2  A: 

You should definitely look at the Lockbits method of manipulating bitmap data.

It is orders of magnitude faster than GetPixel/SetPixel.

EDIT:
Check this link for some code (albeit in VB, but you should get the drift) that almost does what you want. It is simply checking two bitmaps for equality and returning true or false. You could change the function so each pixel check adds to your _pixelChanges list if necessary, and return this list instead of a boolean.

Also, it may be faster if you swap round the iterator loops. i.e. have the inner loop iterating over X, and the outer loop iterating over Y.

Andy
Thank you, any advise on how to implement the above under Lockbits would be appreciated... but for now ill scuttle off and use my own brain!
Thanks Andy I have got that nicely implemented now.
No probs. Just for curiosity sake, how much faster is it?
Andy
Funny I just came on to comment on the speed. For the image sizes I am dealing with its around 4 to 5 times faster. This however is only based on my initial implementation which is almost a straight vb to c# of the provided example. There are a few tweaks I want to try and then ill look at bigger architectural additions e.g. breaking the problem down into chunks and multi-threading it. After doing that I suspect we will be looking at a 10-15 times increase over the original code, CPU permitting...Cheers Nick