简体   繁体   中英

In Python: the center of mass of specific regions of values in a 2D numpy array

I am working with a series of numpy.ndarray made of 101x101 values ranging 0.0 to 1.0. All arrays look like this:

array([[ 0.216,  0.24 ,  0.244, ...,  0.679,  0.684,  0.707],
       [ 0.23 ,  0.229,  0.238, ...,  0.675,  0.676,  0.695],
       [ 0.221,  0.238,  0.24 , ...,  0.669,  0.677,  0.684],
       ..., 
       [ 0.937,  0.925,  0.923, ...,  0.768,  0.754,  0.752],
       [ 0.937,  0.929,  0.923, ...,  0.737,  0.735,  0.741],
       [ 0.934,  0.932,  0.929, ...,  0.72 ,  0.717,  0.728]])

Now, say that I have a threshold value=0.2 : how could I locate the "regions" of values within the matrix in such a way that, within them, the threshold is exceeded? In this case, I would be looking for regions which values are >=0.2 .

In particular, I would like to:

  1. Count the number of regions that exceed the threshold value ;
  2. Determine their centers of mass .

I know that I can compute the latter by means of: ndimage.measurements.center_of_mass() , but I fail to see how it could be applied to just "regions" of a matrix rather than the whole thing.

EDIT

Please consider that the "regions" I refer to have irregular shapes.

Counting all values above a threshold thr = 0.2 can be done by:

a = np.random.random(size=(100, 100))
above_thr = len(a[a > thr])
print above_thr

For the center of mass it really depends whether you want to discard all values lower than your threshold with something along the lines of

a[a < thr] = 0
ndimage.measurements.center_of_mass(a)

If you view a value below the threshold as missing value, you might want to interpolate this missing value first.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM