Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You can set up a crystal-growth-style algorithm shifting a view alternately along each axis, replacing only data that is flagged with a <code>False</code> but has a <code>True</code> neighbor. This gives a "nearest-neighbor"-like result (but not in Euclidean or Manhattan distance -- I think it might be nearest-neighbor if you are counting pixels, counting all connecting pixels with common corners) This should be fairly efficient with NumPy as it iterates over only axis and convergence iterations, not small slices of the data. </p> <p>Crude, fast and stable. I think that's what you were after:</p> <pre><code>import numpy as np # -- setup -- shape = (10,10,10) dim = len(shape) data = np.random.random(shape) flag = np.zeros(shape, dtype=bool) t_ct = int(data.size/5) flag.flat[np.random.randint(0, flag.size, t_ct)] = True # True flags the data # -- end setup -- slcs = [slice(None)]*dim while np.any(~flag): # as long as there are any False's in flag for i in range(dim): # do each axis # make slices to shift view one element along the axis slcs1 = slcs[:] slcs2 = slcs[:] slcs1[i] = slice(0, -1) slcs2[i] = slice(1, None) # replace from the right repmask = np.logical_and(~flag[slcs1], flag[slcs2]) data[slcs1][repmask] = data[slcs2][repmask] flag[slcs1][repmask] = True # replace from the left repmask = np.logical_and(~flag[slcs2], flag[slcs1]) data[slcs2][repmask] = data[slcs1][repmask] flag[slcs2][repmask] = True </code></pre> <p>For good measure, here's a visualization (2D) of the zones seeded by the data originally flagged <code>True</code>.</p> <p><img src="https://i.stack.imgur.com/kW1IZ.png" alt="enter image description here"></p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload