Kite
Kite

Reputation: 649

Detect ball/circle in OpenCV (C++)

I am trying to detect a ball in an filtered image. In this image I've already removed the stuff that can't be part of the object. Of course I tried the HoughCircle function, but I did not get the expected output. Either it didn't find the ball or there were too many circles detected. The problem is that the ball isn't completly round.

Screenshots:

enter image description here

I had the idea that it could work, if I identify single objects, calculate their center and check whether the radius is about the same in different directions. But it would be nice if it detect the ball also if he isn't completely visible. And with that method I can't detect semi-circles or something like that.

EDIT: These images are from a video stream (real time).

What other method could I try?

enter image description here

Upvotes: 4

Views: 2285

Answers (1)

Zaphod
Zaphod

Reputation: 1927

Looks like you've used difference imaging or something similar to obtain the images you have..? Instead of looking for circles, look for a more generic loop. Suggestions:

  • Separate all connected components.
  • For every connected component -
  • Walk around the contour and collect all contour pixels in a list
  • Suggestion 1: Use least squares to fit an ellipse to the contour points
  • Suggestion 2: Study the curvature of every contour pixel and check if it fits a circle or ellipse. This check may be done by computing a histogram of edge orientations for the contour pixels, or by checking the gradients of orienations from contour pixel to contour pixel. In the second case, for a circle or ellipse, the gradients should be almost uniform (ask me if this isn't very clear).
  • Apply constraints on perimeter, area, lengths of major and minor axes, etc. of the ellipse or loop. Collect these properties as features.
  • You can either use hard-coded heuristics/thresholds to classify a set of features as ball/non-ball, or use a machine learning algorithm. I would first keep it simple and simply use thresholds obtained after studying some images.

Hope this helps.

Upvotes: 5

Related Questions