title: Implement MATLAB's im2col 'sliding' in Python
body: <p>Q: How to speed this up?</p>
  
  <p>Below is my implementation of Matlab's <a href="http://www.mathworks.com/help/images/ref/im2col.html" rel="noreferrer">im2col</a> 'sliding' with the additional feature of returning every n'th column.  The function takes an image (or any 2 dim array) and slides from left to right, top to bottom, picking off every overlapping sub-image of a given size, and returning an array whose columns are the sub-images.</p>
  
  <pre><code>import numpy as np
  
  def im2col_sliding(image, block_size, skip=1):
  
      rows, cols = image.shape
      horz_blocks = cols - block_size[1] + 1
      vert_blocks = rows - block_size[0] + 1
  
      output_vectors = np.zeros((block_size[0] * block_size[1], horz_blocks * vert_blocks))
      itr = 0
      for v_b in xrange(vert_blocks):
          for h_b in xrange(horz_blocks):
              output_vectors[:, itr] = image[v_b: v_b + block_size[0], h_b: h_b + block_size[1]].ravel()
              itr += 1
  
      return output_vectors[:, ::skip]
  </code></pre>
  
  <p>example:</p>
  
  <pre><code>a = np.arange(16).reshape(4, 4)
  print a
  print im2col_sliding(a, (2, 2))  # return every overlapping 2x2 patch
  print im2col_sliding(a, (2, 2), 4)  # return every 4th vector
  </code></pre>
  
  <p>returns:</p>
  
  <pre><code>[[ 0  1  2  3]
   [ 4  5  6  7]
   [ 8  9 10 11]
   [12 13 14 15]]
  [[  0.   1.   2.   4.   5.   6.   8.   9.  10.]
   [  1.   2.   3.   5.   6.   7.   9.  10.  11.]
   [  4.   5.   6.   8.   9.  10.  12.  13.  14.]
   [  5.   6.   7.   9.  10.  11.  13.  14.  15.]]
  [[  0.   5.  10.]
   [  1.   6.  11.]
   [  4.   9.  14.]
   [  5.  10.  15.]]
  </code></pre>
  
  <p>The performance isn't great, especially considering whether I call <code>im2col_sliding(big_matrix, (8, 8))</code> (62001 columns) or <code>im2col_sliding(big_matrix, (8, 8), 10)</code> (6201 columns; keeping only every 10th vector) it will take the same amount of time [where big_matrix is of size 256 x 256].</p>
  
  <p>I'm looking for any ideas to speed this up.</p>