Noise removal in spatial data
<p>I have an array with two different values that characterize this image:
<a href="http://i.stack.imgur.com/rnOYu.png" rel="nofollow"><img src="http://i.stack.imgur.com/rnOYu.png" alt="enter image description here"></a></p>

<p>I would like to keep the linear trends in red and remove the "noise" (single red points). Is there a good way to do this? </p>

<p>If there is no way to determine the noise from the signal based on a threshold value (i.e. all the red point have the same value or are just a 1/0 flag), a relatively simple but easy to implement approach could be to look at removing the noise based on the size of the clumps. </p>

<p>Take a look at <a href="http://docs.scipy.org/doc/scipy-0.16.0/reference/generated/scipy.ndimage.measurements.label.html" rel="nofollow">scipy's label</a>. This will give you an array where each separate 'clump' has an individual number. It is then a case of just removing those features that are less then some threshold number of pixels (<code>n_thresh</code> below).</p>

<pre><code>&gt;&gt;&gt; from scipy.ndimage.measurements import label
&gt;&gt;&gt; import numpy as np
&gt;&gt;&gt; n_thresh = 1
&gt;&gt;&gt; a = np.array([[0,0,1,1,0,0],[0,0,0,1,0,0],
                  [1,1,0,0,1,0],[0,0,0,1,0,0],
                  [1,1,0,0,1,0],[0,0,1,1,0,0]])
&gt;&gt;&gt; a
array([[0, 0, 1, 1, 0, 0],
       [0, 0, 0, 1, 0, 0],
       [1, 1, 0, 0, 1, 0],
       [0, 0, 0, 1, 0, 0],
       [1, 1, 0, 0, 1, 0],
       [0, 0, 1, 1, 0, 0]])
&gt;&gt;&gt; labeled_array, num_features = label(a)
&gt;&gt;&gt; binc = np.bincount(labeled_array.ravel())
&gt;&gt;&gt; noise_idx = np.where(binc &lt;= n_thresh)
&gt;&gt;&gt; shp = a.shape
&gt;&gt;&gt; mask = np.in1d(labeled_array, noise_idx).reshape(shp)
&gt;&gt;&gt; a[mask] = 0
&gt;&gt;&gt; a
array([[0, 0, 1, 1, 0, 0],
       [0, 0, 0, 1, 0, 0],
       [1, 1, 0, 0, 0, 0],
       [0, 0, 0, 0, 0, 0],
       [1, 1, 0, 0, 0, 0],
       [0, 0, 1, 1, 0, 0]])
&gt;&gt;&gt; a
</code></pre>

<p>As the features are diagonal, you may want to pay attention to the example in <code>label</code>'s documentations that groups diagonally-touching pixels in the sample clump.</p>

python numpy scipy noise-reduction