Sunday 15 July 2012

c# - Track ball position by image -


Says that I have this image:

I have to get the position of white ball (x, Y), this is a very large image,
Then I cut the size of the rectangle .
(Because when the image is small then everything is fast), the result:

Now I, my code - track the position of the white ball by its color (white = RBG (255,255,255)) Want to: To - 1, then HaxScreenOnly.GetPixel (X, Y) = Color.FromArgb (0, 0, 0) then HaxScreenOnly.GetPixel (x + 8, y) = Color.FromArgb (0, 0, 0) and HaxScreenOnly.GetPixel (x + 8, y + 3) = Color.FromArgb (255, 255, 255) Return New Point (X, Y) End if End Next Next Return New Point (0, 0) End Function


If the color of the existing pixel is black < / Strong> and current pixel color (x + 8, y + 3) is white then this is the ball - it's working ... but 200 to track the position of the ball It's slow speed like milliseconds.
This is not fast.
The fast way to track white is the ball ( C # or VBnet )?

Finally, I have a solution calling for you GetPixel one expensive The process is, but you use bitmap. Manipulation / accessing image data from lockbits and pointer I have lockbitmap category .

I checked the performance of what I was already getting, which you mentioned about 200ms around,

Here is a picture of the result using LockBitmap , which is constantly scanning the image with a custom code!

 Here's a picture  Static zero main (string [] args) {byte [] data = new webclient (). DownloadData ("http://srv2.jpg.co.il/1/51c616787a3fa.png"); Image image = image.fromstream (new memorystream); Lockbitmap bitmap = new lockbitmap (new bitmap (image)); // It basically copies the data into memory and copies the array from a pointer to bitmap. Lockbits (); Color black = color. Form ARBB (0, 0, 0); Color white = color. Form ARBB (255, 255, 255); Stopwatch stopwatch = stopwatch Startup (); For (int y = 0; y & lt; bitmap.height; p ++) {for (int x = 0; x & lt; bitmap.Width; x ++) {// GetPixel in a good abstract paragraph If we do not do any work of gross stuff, then the author is made (bitmap.getpixels (x, y) == black) {if (bitmap gatepxal (x + 8, y) == black & amp; Bitmap .Get pixel (x + 8, y + 3) == white) {Console.WriteLine ("White ball found in {0}", stopwatch. Apple.tostring ()); break; }}}} Bitmap.UnlockBits (); // Copy the data from the array to the original pointers console. Read (); }   

Hope this helps, it was definitely an interesting thing for me.

Update:

As the King said, I further reduce the time for you based on algorithm improvement. Therefore, we have gone from O (N) to the complexity of time (I think) from O (n log) time.

algorithm correction

 For  (int y = 0; y & lt; bitmap.height; y + = 3) // For the radius of the ball {int x = 0; x & lt; bitmap.Width; X + = 3) // We can increase this {if (bitmap.getPixel (x, y) == black & amp; amp; bitmap gatepixel (x, y + 3) == white) {console.writeLine ( "White ball found ({0}, {1}) in {2}", x, y, stopwatch. Apple.tostring ()); break; }}}    

No comments:

Post a Comment