This is the original image

I first start out by blurring the image to reduce some noise.

I then use thresholding and edge detection to determine where the tennisballs approximately are.

Since the thresholding isn't perfect, I blur the binary image a bit

After this, all grayscale pixels above a certain value are made white, while all pixels below this value are set to black.

Segmentation, Filtering, and Data Association:

The large white balls are all identified by binary value 1, so to make each ball individual, a floodfill algorithm is used to find the connected pixels and identify each ball by a different number.

This algorithm also kept track of the volume of each ball, and the propper data to estimate the centroid of each ball.

From this, I compute the length of each side of the 'square' the tennis balls make.

Distance Estimation:

Estimating the distance is an association of the average side lengh of the estimated square, and an equation that determines the distance based off of previously gathered data. The picture above was taken a little less than 4 feet away, and the program estimates it to be about 4.01 feet away.

The following is a chart of estimated distances using this program. (No 2ft pictures because my camera was too small)

And the actual pictures themselves


One potantial source for erorr is the threshold picking up other color that could be identified as targets.

For example, this picture:

Another source for error is hvaing a ball partially obscured. While not as severe as the previous example, it still messes with the data a bit.
For example, in this image:

My last example of error is a fault of my program. Sometimes the thresholding.edge detection messes up a bit, and the stripes on the tennis ball prevents the whole ball from being captured.
As observed here: