0

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.

FaCoffee
  • 7,609
  • 28
  • 99
  • 174
  • Well, I was only able to locate the center of mass of the whole matrix with the function`ndimage.measurements.center_of_mass()`. But again, this is for the entire matrix, and not for sections of it where my condition is satisfied... – FaCoffee Nov 18 '15 at 13:38
  • 2
    My answer to your previous question [here](http://stackoverflow.com/q/33707095/1461210) already shows how to do this - you need to pass a vector of indices as the third argument to [`scipy.ndimage.measurements.center_of_mass`](http://docs.scipy.org/doc/scipy-0.16.0/reference/generated/scipy.ndimage.measurements.center_of_mass.html) specifying which labels to process. Note that this vector needs to start at 1 rather than 0, since all locations that aren't part of any of the above-threshold regions get assigned a label of 0. – ali_m Nov 18 '15 at 13:41
  • what is "region"? Each cell? Or a group of connected cells? – lejlot Nov 18 '15 at 13:44
  • Yes, a group of cells. They might not be connected, in the sense that a region could be also made of a single cell. – FaCoffee Nov 18 '15 at 13:45

1 Answers1

0

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.

Jolfulorc
  • 173
  • 3
  • 9
  • 1
    remember that if the array represents some "map", then your solution does not distinguish between groups, it just gets one huge, non-consistant group, which is **probably** not what OP asks for – lejlot Nov 18 '15 at 13:43
  • Depends on the type of data, for sure! – Jolfulorc Nov 18 '15 at 13:45
  • 1
    Ok, now it is for sure not what he asks for (see his comment) – lejlot Nov 18 '15 at 13:54