simplify numpy array representation of image
<p>I have an image, read into <code>np.array</code> by PIL.
In my case, it's a<code>(1000, 1500)</code> <code>np.array</code>.</p>

<p>I'd like to simplify it for visualisation purposes.
By simplification, I following transformation from this matrix</p>

<pre><code>1 1 1 1 0 0
1 0 1 0 0 0 
</code></pre>

<p>to </p>

<pre><code>1 1 0
</code></pre>

<p>so, essentially, look at every <code>2*2</code> sample, if it satisfies some criteria, like more than 50% of 1's -> count it as a 1, if not, count is as a 0.</p>

<p>I don't know how to call it properly, but I believe there should be some well known mathematic procedure for doing so.</p>

<p>Use a combination of <code>np.reshape()</code> and <code>np.sum(array)</code> with <code>axis</code> argument.:</p>

<pre><code>import numpy as np
a = np.array([[1, 1, 1, 1, 0, 0], [1, 0, 1, 0, 0, 0]])
a = np.reshape(a, (a.shape[0]/2, 2, a.shape[1]/2, 2))
a = np.sum(a, axis=(1, 3)) &gt;= 2
</code></pre>

<p>The reshaping groups your array into small 2x2 blocks (the orginals axis lengths must be multiples of 2), I then use sum along the created axis to check that at least of the 4 values in the group is 1.</p>

<p>See <a href="http://stackoverflow.com/questions/32319036/computing-average-for-numpy-array/32319504#32319504">Computing average for numpy array</a> for similar question.</p>

python arrays image numpy image-processing