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).
I solve the problem in the following way:
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])
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.
The vec_1 and vec_2 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 vec_2 than vec_1. However, according to this answer to my question it was expected to be faster. So, why it is not faster?
Finally, I also would like to know if there is a faster alternative to looping over all columns and rows of the image-matrix.
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.