• Bocce Labs
  • Posts
  • A Computer Vision Bocce Ball Algorithm

A Computer Vision Bocce Ball Algorithm

Creating a Computer Vision Pipeline

Howdy 👋

Last week, we delved into my thoughts on whether computer vision for bocce ball is even practical.

This week we'll discover the beginnings of the computer vision pipeline that makes this bocce software pipe dream possible.

Welcome to the 11 new subscribers this week — we've just crossed into 50+ subscribers territory. As more folks subscribe, I'm more motivated to continue building and brainstorming.

A Bocce Ball Computer Vision Algorithm

Bocce Ball Tracking Algorithm

David Hoffman

In comparison to many software concepts, computer vision is often relatable for most non-techies as it begins with your own eyes.

Let's dive in.

Bocce Vision Considerations

Consider the game of bocce ball and what cameras might see:

  • Court surface

  • Lines and markings on the court

  • Balls

  • Players

  • Referees

Let's zero in on the balls themselves. We need to determine their (x, y)-coordinates in the image, and translate those coordinates to real-world placements on the court.

The Bocce Ball Finder Computer Vision Pipeline

Bocce Computer Vision Algorithm

David Hoffman

The flowchart above is the first iteration of the computer vision pipeline that I designed. This flowchart mimics the written code.

Take a moment to read the flowchart from top-to-bottom. Let's go through the components step-by-step.

Step 1: Grab image from camera

This is self-explanatory — we request the latest image from the camera(s).

Step 2: Blur

Blurring an image reduces noise so that adjacent pixels will have less of a variation.

Step 3: Find Circles

We apply the Hough Circles algorithm, which finds circles in the image. Sometimes it "finds" circles as false positives that we filter out later. As we say in bocce, "the balls are round."

Step 4a & Step 4b: Check for circles that are Pallino radius and Bocce radius

Some of the circles have a pixel radius in the range of the Pallino (the small ball) and others will in the range of a Bocce (larger ball). We separate these potential ball regions into two different data structures.

Step 5a & Step 5b: AI Classification of the Pallino radius circles and Bocce radius circles

The AI classifier I designed has one job — determine if a circle is a ball or is not a ball. It filters out the false positive balls and builds our confidence in the true positive balls. We need to know that the beer can isn't a ball and that someone's shoe isn't a ball. The AI classifier is about 98% accurate.

Step 6: Centroid Tracking

Balls that enter this step are 98% likely to be a ball. A centroid is the center (x, y)-coordinate (i.e., the center of the ball). We need to remember the ball centroids in case we see them again in the next frame we grab from the camera. The centroid tracking algorithm simply keeps track of (x, y)-coordinates. It:

  • Logs new centroids

  • Checks them against previously logged centroids that are nearby in distance (within D pixels)

  • Determines if a previously logged centroid is no longer present; if it has disappeared for more than N frames we delete it.

That's the gist. Ideally if a ball gets bumped and moves a little bit, I'll still know that that ball is that ball and isn't a different ball, ya know?

Step 7b: Color Histogram Clustering

This step applies to the larger Bocce balls only.

In bocce most professional balls (shout out to Super Martel) have a multicolored pattern on them. It is part of Bocce culture for teams to show up with their own balls akin to a bowling professional showing up with their own favorite bowling ball. Bocce tournaments regulate the radius/diameter of the ball and the weight of the ball.

Therefore, in this step we aren't simply asking if the ball is blue or red.

Rather, we're asking if the ball's pattern is pattern-A or pattern-B.

There are a couple ways to do this with computer vision.

(1) If we have a priori knowledge of the two types of bocce balls, we can train a "supervised" AI Classifier. That would be a pain if we had to snap a bunch of images of each team's balls and then run the images through AI training. There isn't enough time.

(2) Instead, I've come up with an "unsupervised" approach where the algorithm learns on the fly without a priori knowledge of the balls. This is a clustering algorithm. Given that there is a multicolor pattern on the ball, we want to consider all the pixel colors that are present. The algorithm turns all the pixels in the ball circle region into a list of colors and compares that to all other balls on the court. Knowing that there will only be two clusters (teamA = patternA and teamB = patternB), and assuming that the colors in the patterns are dissimilar enough, we can distinguish between each team's balls.

Step 8: Add balls to a data structure and correlate with balls we've already found

Step 8 is easy — we now know all the balls' centroids and if Bocce balls belong to teamA or teamB.

We place this info into a data structure known in the software world as an Object. After all a bocce ball is an object of my affection.

Repeat Steps 1-8.

For a live video stream, or a recorded video, there will be frame-after-frame. We'll do this again and again to collect ball data.

TL;DR: The algorithm finds circles, determines if the circles are balls, and records & correlates their center (x, y)-coordinates.

Feedback requested

Now that you've read through steps 1-8, take a look back at the diagram and follow it top-to-bottom once again. Does it make any sense?

What will we do with the data

Collecting the data is the boring part. What we do with the data will be the fun part.

Right now, we can:

  • Determine the distance from every Bocce Ball to the Pallino automatically

  • Count how many teamA/teamB balls are "IN" (i.e., closer to the Pallino than the other team's balls)

Soon we'll be able to:

  • Score games from start to finish and display the live score as well as who is in/out on a scoreboard screen (pieces of this project are already in place).

  • Calculate insightful statistics on the fly and retroactively (a topic for an upcoming post).

  • Store game data in a cloud database (pieces of this software are already in place).

  • Make bocce broadcasts more data driven (commentators love to talk about stats)

Thanks for reading!

My line is always open if you reply to this email with your ideas, thoughts, and/or words of encouragement.

Consider forwarding this email to a friend that might be interested!


~ Digital Dave

Join the conversation

or to participate.