title: How to extract a sub-array in numpy efficiently?
body: <p>I have an image given as a 3D numpy array (width, height and color as dimensions). Now I would like to extract all possible sub-images of a given shape. For example, the width and height of the original image is 300 and 200 pixels, respectively, and I would like to extract all sub-images with width and heights equal to 10 and 20 pixels, respectively. Moreover, each sub-image I would like to have as 1D array (for that I need to specify a particular ordering of pixels).</p>
  
  <p>I solve the problem in the following way:</p>
  
  <pre><code>for col0 in range(w_max - max_shift):
      x3s_new = [x + col0 for x in x3s]
      for row0 in range(h_max - max_shift):
  
          vec_1 = []
          for col_shift, row_shift in px_inds:
              col = col0 + col_shift
              row = row0 + row_shift
              vec_1 += [ia[row, col, 0], ia[row, col, 1], ia[row, col, 2]]
  
          y3s_new = [y + row0 for y in y3s]
          vec_2 = list(ia[y3s_new, x3s_new, z3s])
  </code></pre>
  
  <p>In the above code I make a loop over columns and rows of the "matrix" representing the image. Then each pixel (given by its column and row) I treat as the left-top corner of the sub-image and extract the sub-image.</p>
  
  <p>The <code>vec_1</code> and <code>vec_2</code> are the desired sub-images given as 1D arrays (lists). They are identical, I just wanted to test what way is faster. Surprisingly, it take more time to generate <code>vec_2</code> than <code>vec_1</code>. However, according to <a href="https://stackoverflow.com/questions/30182076/how-to-extract-a-list-of-elements-given-by-their-indices-from-a-numpy-array-effi">this answer</a> to my question it was expected to be faster. So, why it is not faster?</p>
  
  <p>Finally, I also would like to know if there is a faster alternative to looping over all columns and rows of the image-matrix. </p>
  
  <p>To summarize, my question is: How to achive what I need in faster way? At the moment it takes me about 5 minutes to "process" one image and it is not acceptable for my purposes.</p>