What I would do:
- Find all the contours (and store the in a vector of point) (
cv::findContours
) Apply a custom particle filter. That is, a function that will tell you whether a contour is valid. To do that, you need to base your decision on morphological features such as
- circularity
- convexity
- aspect ratio
Take a look at something like that to have visual examples:
You can calculate the circularity from the area (cv::contourArea
) and the perimeter (cv::arcLength
) while convexity will involve computing the convex hull (cv::convexHull
).
This step should result in a new vector containing only valid (e.i. circular contours).
Your contourArea(contours[i], false) < 100
is a good start, but it will not tell you whether a contour is a circle.
After that, you can calculate a centre of gravity (and display it) of each element of your new vector. Does is make sense ?
Finally, I would not use contours.erase()
for large vectors. I think iteratively erasing is a fairly heavy job for the CPU. Instead, I would store the good contours in a new preallocated vector. This is however just a performance detail.