I followed the code in this link to find the object center for my graylevel image,
def find_center(im):
immat = im
(X, Y) = [im.shape[0],im.shape[1]]
m = np.zeros((X, Y))
for x in range(X):
for y in range(Y):
m[x, y] = immat[(x, y)] != 0
m = m / np.sum(np.sum(m))
# marginal distributions
dx = np.sum(m, 1)
dy = np.sum(m, 0)
# expected values
cx = np.sum(dx * np.arange(X))
cy = np.sum(dy * np.arange(Y))
return [cx,cy]
xy1=find_center(img) #img is a binary image, object has value==1 and back ground value of 0
print xy1
plt.imshow(img)
plt.annotate('center', xy1, xycoords='data',
xytext=(0.5, 0.5), textcoords='figure fraction',
arrowprops=dict(arrowstyle="->"))
plt.show()
However, I am not getting the right answer (the center is not inside the object), the following image shows the output:
What am I doing wrong?