wrong center of mass x direction calculation (openCV Python) -


i try calculate center of mass of objects in binary image opencv , python. use cv2.findcontours , cv2.moments wrong x direction. has positive offset of few pixels , don't why. think did in opencv doc.

here example. green contour found cv2.findcontours. red calculated center of mass.

enter image description here

my code is:

import cv2 import numpy np  img = cv2.imread('c:/users/arno/documents/masterarbeit/matlab/rect2.png', 0) cimg = cv2.cvtcolor(img, cv2.color_gray2bgr) contours = cv2.findcontours(img, cv2.retr_tree, cv2.chain_approx_simple) cnt1 = contours[0] cnt2 = contours[1] cv2.drawcontours(cimg, cnt2, -1, (0, 255, 0), 2)  m = cv2.moments(cnt1) cx = int(m['m10']/m['m00']) cy = int(m['m01']/m['m00'])  cv2.circle(cimg, (cx, cy), 1, (0, 0, 255), 2) cv2.imshow('center of mass', cimg) 


Comments

Popular posts from this blog

matlab - "Contour not rendered for non-finite ZData" -

delphi - Indy UDP Read Contents of Adata -

javascript - Any ideas when Firefox is likely to implement lengthAdjust and textLength? -