Created
February 27, 2016 02:13
-
-
Save mlsteele/d0924cefcafd0170cd8a to your computer and use it in GitHub Desktop.
Find the centroid of a 2D array.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 the iamge as bit cells. | |
print (x == 1).astype(int) | |
# Print the centroid. | |
xis, yis = np.nonzero(x) | |
print xis.mean(), yis.mean() | |
# print np.transpose(np.nonzero(x)) | |
# print np.array(np.nonzero(x))[:,2] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
?