# About building a boundary around an object in an image

I would say the task using the pixel-by-pixel method is inappropriate for any device other than a high-end PC.

Ideally the image would be cropped to remove most of the background, leaving a smaller area to process. If the image has to arrive as-is, prompt the user to pick top left and bottom right points that define the area to be tested. However, the pixel-by-pixel method is still slow. Rather than test every pixel within the User-defined area, working left to right, once a pixel is hit that is not the background colour, we want to plot there and then test the surrounding pixels. So the code needs procedures for these little test areas to effectively only test in the region of the actual border and not all the pixels of the image. The code to do that type of test surely already exists - either as something that can be adapted and used on the App canvas (perhaps ImageMagik), or via an online service using the Custom Web View extension.

Here's a rough guess on a shape-detection algorithm:

• Code a point function to tell you is that point inside or outside.
• Make a variable to hold the (x,y) of a point for the center of the object, initially the center of the Canvas.
• Guess the distance to edge in each of 4 directions from the object's center, using binary search along four outgoing radials.
• Recenter the center point to get it closer to the center of the object, based on left/right radial and up/down radials.
• Shoot out 8 or more equally spaced (by angle) radials from the new center, taking their lengths by binary search.
• The standard deviation of the 8 radial lengths should tell you the shape:
• round = small standard deviation
• rectangular = large standard deviation.

The idea to use the calculated area to define the shape. It can be done this way:

1. To find the leftmost, rightmost, topmost and bottommost pixels of the image.
2. To calculate the hypothetical area of the figure.
3. To count the number of pixels in the figure.
4. If the results of step 2 and 3 are approximately equal, the figure is a rectangle. If the difference is significant, then the figure is a circle.