I want to extract the points from given image. the image is shown below.开发者_如何学Python.
The points I want are the green upper point and the red point. I tried pixel by pixel comparison but it is too slow. I need a better algorithm. What are your suggestions ?
If the points are always going to be at a known radius from the center then you can just check the points that lie on the circumference.
Pixel by pixel comparison is going to be hard to beat. You can improve your search speed on the green line considerably by using a divide-and-conquer method.
If the image width is x
and its height is y
, search all of the pixels located at x={0...x},y={y/4,3*y/4}
for a green pixel. If none is found, search all the pixels along x={x/4,3*x/4},y={0...y}
. As soon as you find a green pixel p
at coordinates px,py
, search that pixel's two-pixel neighborhood which is farther from the center of the image (that is, {px,py+1},{px+1,py}
if p
is in the upper right, {px,py-1},{px-1,py}
if p
is in the lower left, {px,py+1},{px-1,py}
if p
is in the upper left, or {px,py-1},{px+1,py}
if p
is in the lower right quadrant. Update p
to be the first green neighbor you find. Iterate until p
has no more green neighbors. Worst-case this algorithm is ~O(2*(x+y)+(1/2)*max(x,y)) ~= O(2.5*max(x,y)) ~= O(x)
, which is a lot better than O(x*y)
if you simply check the color value of every {x,y}
pair.
Finding the red dot is going to be expensive, though, no less expensive than O(x*y)
since the only way to improve the cost of searching for a single red pixel will be by subsampling the image (O(x*y)
) and then searching the whole image (now O(sqrt(x*y))
) for the red pixel.
I like par's idea, though, if the two dots are always the same distance from the center of the image then you can just search the pixels that fall along the circumference of that radius!
精彩评论