views:

347

answers:

2

hello to every one, i am new one to this site and this is my first query... i need simple Sliding window algorithm implementation in c++ or matlab please help me in this regard thanks

+1  A: 

Assuming you need a generic sliding window for image processing, in Matlab you could do:

image = imread('image.png');
imageWidth = size(image, 2);
imageHeight = size(image, 1);

windowWidth = 32;
windowHeight = 32;

for j = 1:imageHeight - windowHeight + 1
    for i = 1:imageWidth - windowWidth + 1
        window = image(j:j + windowHeight - 1, i:i + windowWidth - 1, :);
        % do stuff with subimage
    end
end
Iggy Kay
Just note that loops in Matlab are slow. You better use some of the "batch" operations as in Jonas' answer.
Amnon
+1  A: 

If the function is a simple linear combination of pixel values in the neighborhood, such as an average, you can use CONV2 to make the convolution. There are also specialized functions, such as MEDFILT2 if you want to take the median of each sliding window.

If the function you want to apply to each neighborhood is more complex, you have two options:

(1) If you have enough memory, you can transform your image into a large array such that every column corresponds to one sliding window using IM2COL. Then you apply your function to every column and reshape.

(2) If you don't have that much memory, use NLFILTER to apply the function to each sliding window.

In any case, you may want to have a look at PADARRAY to pad your image before you run the convolution to avoid shrinking your image while reducing border effects.

Jonas
Good observation re: border effects.
jasonmp85
thanks but my application is about data stream mining