Skip to content

Instantly share code, notes, and snippets.

@mlsteele
Created February 27, 2016 02:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mlsteele/d0924cefcafd0170cd8a to your computer and use it in GitHub Desktop.
Save mlsteele/d0924cefcafd0170cd8a to your computer and use it in GitHub Desktop.
Find the centroid of a 2D array.
import numpy as np
print "\n\n"
# Width and height of the image.
W = 5
H = 5
# Generate a random image.
x = np.random.rand(W, H) > 0.5
# Print the image.
print x
print
# Print the iamge as bit cells.
print (x == 1).astype(int)
print
# Print the centroid.
xis, yis = np.nonzero(x)
print xis.mean(), yis.mean()
print
# print np.transpose(np.nonzero(x))
# print np.array(np.nonzero(x))[:,2]
@vinnitu
Copy link

vinnitu commented Jul 25, 2017

?

np.mean(x.astype(int), axis=0)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment