Fastest way to scan an image for pixel color (black and white)
-
i have a black and white image, i need to quickly scan the entire image for white (255,255,255 RGB) pixels and add then to an array. I have a for loop in a for loop to do this as follows: <pre private void trackLane() { Bitmap leftLane = new Bitmap(detectLeft.Image); Bitmap rightLane = new Bitmap(detectRight.Image); for (int i = 0; i <= detectLeft.Width; i++) { for (int j = 0; j <= detectLeft.Height; j++) { leftLane.GetPixel(i, j); rightLane.GetPixel(i, j); } } } ></pre> Where j is the y axis and i is the x axis, i think there is a more efficient way of doing this, any suggestions?
-
i have a black and white image, i need to quickly scan the entire image for white (255,255,255 RGB) pixels and add then to an array. I have a for loop in a for loop to do this as follows: <pre private void trackLane() { Bitmap leftLane = new Bitmap(detectLeft.Image); Bitmap rightLane = new Bitmap(detectRight.Image); for (int i = 0; i <= detectLeft.Width; i++) { for (int j = 0; j <= detectLeft.Height; j++) { leftLane.GetPixel(i, j); rightLane.GetPixel(i, j); } } } ></pre> Where j is the y axis and i is the x axis, i think there is a more efficient way of doing this, any suggestions?
Hi, here are some ideas: 1. j <= detectLeft.Height gets executed for every pixel, you'd better store detectLeft.Height in a local int variable 2. executing a lot of GetPixel() calls tends to be slow since each time the coordinates are checked against the image boundaries, and translated into a linear index, something that can be done more easily once one knows they all get handled sequentially 3. the most practical approach to (2) is by using pointers 4. why do you use 4 bytes per pixel if all it can be is black and white; an array (bools, bytes, ints, ...) or a BitArray would be more economical in space, and hence in speed (assuming your image is say 512*512, it takes 1MB which is about the size of the level 2 cache); less data would run faster. Maybe you should swith to one byte per pixel (optimum between density and simplicity) 5. there are special code snippets to find the lowest bit set in an integer, this could be much faster assuming a binary image (yours is), and a majority of zeroes.
x & -x
results in zero if x is zero, and in an int with exactly one bit set which is also set in x otherwise. :)Luc Pattyn [Forum Guidelines] [My Articles]
The quality and detail of your question reflects on the effectiveness of the help you are likely to get. Show formatted code inside PRE tags, and give clear symptoms when describing a problem.
-
Hi, here are some ideas: 1. j <= detectLeft.Height gets executed for every pixel, you'd better store detectLeft.Height in a local int variable 2. executing a lot of GetPixel() calls tends to be slow since each time the coordinates are checked against the image boundaries, and translated into a linear index, something that can be done more easily once one knows they all get handled sequentially 3. the most practical approach to (2) is by using pointers 4. why do you use 4 bytes per pixel if all it can be is black and white; an array (bools, bytes, ints, ...) or a BitArray would be more economical in space, and hence in speed (assuming your image is say 512*512, it takes 1MB which is about the size of the level 2 cache); less data would run faster. Maybe you should swith to one byte per pixel (optimum between density and simplicity) 5. there are special code snippets to find the lowest bit set in an integer, this could be much faster assuming a binary image (yours is), and a majority of zeroes.
x & -x
results in zero if x is zero, and in an int with exactly one bit set which is also set in x otherwise. :)Luc Pattyn [Forum Guidelines] [My Articles]
The quality and detail of your question reflects on the effectiveness of the help you are likely to get. Show formatted code inside PRE tags, and give clear symptoms when describing a problem.
-
This link describes the fast way to access pixels in C#: http://www.bobpowell.net/lockingbits.htm[^]