Question

I am currently trying to divide an image into smaller windows, which are later used for cross-correlation. The below function works well for the purpose (window_size is the length of the window size in pixels (assumed to be square), overlap is by how many pixels the windows overlap (so for a 32x32 window, 16 overlap would imply half overlap).

It works well for that purpose. However, now I want to compare two different sized windows (for example one being 24x24 and the other being 12x12. The "challenge" is that they have to be centred and the number of big and small windows has to be the same. I can currently achieve this by cropping the window to centre the smaller windows with the bigger ones (so for example if I have a 256x256 image, I crop it by 6px from every direction, getting a 244x244 image).

However, even though cropping is really fast (I have thousands of images), but it gets tedious (and sometimes impossible if I want the smaller windows to overlap)changing the parameter if I want to experiment with other size windows.

So basically, my question is, is there a way to centre the two different sized windows, without cropping the image ? Some notes:

1) The bigger windows can be "out of range" of the image, provided the increase in size is a black (or 0 gray scale value) image. 2) The number of big and small windows must be the same.

def moving_window_array(array, window_size, overlap):
"""
Create from the array a new array
with three dimension, of size (n_windows, window_size, window_size), in which
each slice, (along the first axis) is an interrogation window.
"""

    sz = array.itemsize
    shape = array.shape

    strides = (sz*shape[1]*(window_size-overlap), sz*(window_size-overlap),
               sz*shape[1], sz)
    shape = (int((shape[0] - window_size)/(window_size-overlap))+1,
             int((shape[1] - window_size)/(window_size-overlap))+1 ,
             window_size, window_size)

    return numpy.lib.stride_tricks.as_strided(array, strides=strides,
                                              shape=shape ).reshape(-1, window_size,
                                                                    window_size)

No correct solution

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top