In order to obtain the roundness of an object, you either extract the roundness k
or the eccentricity e
of the object. With eccentricity being a better measure because it has a clearly defined range of values and therefore it can be compared much better.
The roundness k
can easily be calculated by dividing the square of the perimeter p
with the area A
:
However, because a circle has a maximal Area A
within a given perimeter p
, a scaling of roundness k
is performed:
Therefore, k
for a circle is equal 1, while for other objects > 1. On the other hand eccentricity e
can derived from the semi-major and semi-minor axes a
and b
of an object:
Moreover, eccentricity can be calculated from the central moments of second order:
The eccentricity can have values from 0 to 1. 0 corresponds to a perfectly round object while 1 to a line shaped object. Therefore using the moments class I guess you can achieve your task. Furthermore, here there is an sort of example where it computes some of the above properties. it is in old C api but I do not think that it would be that hard to port it to the new C++ api.
source
Have you tried the ratio between the 2 axes? If it is 1 (or very close to 1) than it is (may be) a circle