Detecting boundaries in image
I'm writing a game for a mobile device that display a sequence of colored balls against a background. If the user touches the balls, I want to zoom them and show a message.
As the number and size of the balls is ale开发者_JAVA技巧atory, I tried to flood fill the area ocupied by the balls and get the boundary rectangle to pass to the zoom function.
This only works if the user, by chance, doesn't touche over a ball or inside a closed path...
Here's a graphical representation of my problem:
*, dark color background
spaces, light color background
+, are the balls
*********************************
*** ***
*** ***
*** ***
*** + + + ++ ++ ***
*** + + ++ + + ***
*** + + + + + + ***
*** + + + + + ***
*** + + + ++ ***
*** + + ++ + + ***
*** ***
*********************************
I want to know the coordinates of the rectangle that includes all balls.
Any suggestions?
Thanks, pmc
A rectangle which stores all the balls can be calculated by simple minimum and maximum calculation. You just have to find the minimum value of (position - radius) and maximum for (position + radius) in both coordinates. Of course, this assumes the balls were drawn by you or at least their coordinates are known. Otherwise, it's a computer vision problem, albeit a simple one, as a mobile device doesn't have such a big screen to make a full pixel by pixel scan unfeasible.
Say your origin is in the lower left. Positive x is to the right, and positive y is up.
Your left boundary will be the minimum value of (x_i - r_i) where x_i is the x coordinate of ball i, and r is the radius of ball i.
Your right boundary will be the maximum value of (x_i + r_i).
Your bottom boundary will be the minimum value of (y_i - r_i).
Your top boundary will be the maximum value of (y_i + r_i).
精彩评论