Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sliding window using as_strided function in numpy?

As I get to implement a sliding window using python to detect objects in still images, I get to know the nice function:

numpy.lib.stride_tricks.as_strided

So I tried to achieve a general rule to avoid mistakes I may fail in while changing the size of the sliding windows I need. Finally I got this representation:

all_windows = as_strided(x,((x.shape[0] - xsize)/xstep ,(x.shape[1] - ysize)/ystep ,xsize,ysize), (x.strides[0]*xstep,x.strides[1]*ystep,x.strides[0],x.strides[1])

which results in a 4 dim matrix. The first two represents the number of windows on the x and y axis of the image. and the others represent the size of the window (xsize,ysize)

and the step represents the displacement from between two consecutive windows.

This representation works fine if I choose a squared sliding windows. but still I have a problem in getting this to work for windows of e.x. (128,64), where I get usually unrelated data to the image.

What is wrong my code. Any ideas? and if there is a better way to get a sliding windows nice and neat in python for image processing?

Thanks

like image 433
JustInTime Avatar asked Sep 24 '11 21:09

JustInTime


People also ask

What is NP R_?

numpy.r_[array[], array[]] This is used to concatenate any number of array slices along row (first) axis. This is a simple way to create numpy arrays quickly and efficiently.


1 Answers

There is an issue in your code. Actually this code work good for 2D and no reason to use multi dimensional version (Using strides for an efficient moving average filter). Below is a fixed version:

A = np.arange(100).reshape((10, 10))
print A
all_windows = as_strided(A, ((A.shape[0] - xsize + 1) / xstep, (A.shape[1] - ysize + 1) / ystep, xsize, ysize),
      (A.strides[0] * xstep, A.strides[1] * ystep, A.strides[0], A.strides[1]))
print all_windows
like image 101
Roman Podlinov Avatar answered Oct 05 '22 21:10

Roman Podlinov