Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detecting balls on a pool table (stripes and solids)

Tags:

java

opencv

I couldn't find any satisfying answer on that topic. I want to make a program that will get snapshots from camera above the pool table and detect balls. I am using OpenCV and Java. My algorithm now is basically:

blurring image -> converting RGB to HSV -> splitting into 3 planes -> using Canny() on H plane -> using HoughCircles() method to detect balls

This algorithm detects balls quite well, it has problem with two balls only (green and blue, because background of the table is green). But I want to go one step further and:

  • Detect if the ball belongs to stripes or solids
  • Set an ID of every ball, stripes would have for example 1-7 and solids 8-14, every ball would have unique ID that doesn't change during the game

Do you have any idea how to implement task #1? My idea is to use inRange() function, but then I'd have to prepare a mask for every ball that detects that one ball in specified range of colors, and do this detection for every ball, am I right? Thanks for sharing your opinions.

@Edit: Here I give you some samples of how my algorithm works. I changed some parameters because I wanted to detect everything, and now it works worse, but it still works with quite nice accuracy. I`ll give you three samples of original image from camera, image where I detect balls (undistorted, with some filters) and image with detected balls.

Original image number 1Canny image number 1Image with detected balls number 1

Original image number 2Canny image number 2Image with detected balls number 2

like image 909
boratocielli Avatar asked Aug 10 '18 19:08

boratocielli


1 Answers

Recommendation:

If you can mask out the pixels corresponding to a ball, the following method should work to differentiate striped/solid balls based on their associated pixels:

  1. Desaturate the ball pixels and threshold them at some brightness p.
  2. Count the number of white pixels and total pixels within the ball area.
  3. Threshold on counts: if the proportion of white pixels is greater than some threshold q, classify it as a striped ball. Otherwise, it's a solid ball.

(The idea being that the stripes are white, and always at least partially visible, so striped balls will have a higher proportion of white pixels).


Sample Testing:

Here's an example of this applied (by hand, with p = 0.7) to some of the balls in the unrectified image, with final % white pixels on the right.

It looks like a classification threshold of q = 0.1 (minimum 10% white pixels to be a striped ball) will distinguish the two groups, although it would be ideal to tune the thresholds based on more data.

If you run into issues with shadowed balls using this method, you also can try rescaling each ball's brightnesses before thresholding (so that the brightnesses span the full range 0, 1), which should make the method less dependent on the absolute brightness.

like image 116
Ollin Boer Bohan Avatar answered Nov 01 '22 08:11

Ollin Boer Bohan