For some reason that I cannot understand, the open cv function cv2.moments
returns a dictionary with all zero values for the contour I am providing.
Here is a MWE:
contour = [[[271, 67]],
[[274, 67]],
[[275, 68]],
[[278, 68]],
[[279, 69]],
[[283, 69]],
[[284, 70]],
[[287, 70]],
[[288, 71]],
[[291, 71]],
[[292, 72]],
[[295, 72]],
[[292, 72]],
[[291, 71]],
[[288, 71]],
[[287, 70]],
[[284, 70]],
[[283, 69]],
[[279, 69]],
[[278, 68]],
[[275, 68]],
[[274, 67]]]
contour = np.asarray(contour)
moments = cv2.moments(contour)
with the result:
print(moments)
{'m00': 0.0, 'm10': 0.0, 'm01': 0.0, 'm20': 0.0, 'm11': 0.0, 'm02': 0.0, 'm30': 0.0, 'm21': 0.0, 'm12': 0.0, 'm03': 0.0, 'mu20': 0.0, 'mu11': 0.0, 'mu02': 0.0, 'mu30': 0.0, 'mu21': 0.0, 'mu12': 0.0, 'mu03': 0.0, 'nu20': 0.0, 'nu11': 0.0, 'nu02': 0.0, 'nu30': 0.0, 'nu21': 0.0, 'nu12': 0.0, 'nu03': 0.0}
What is the meaning of this behaviour? I believe this is because the contour is open, but i am not sure. Is there a standard way to get rid of this behaviour by checking if the contour is open or closed beforehand?