# Obtain 2x2 regions in an image using sliding window

1 view (last 30 days)
Sparsh Garg on 31 Jul 2021
Answered: Image Analyst on 31 Jul 2021
Given an image,I would like to observe 2x2 regions of that image using sliding window.Notice that,as of now I am only interested in obsrving the pixels in that region and not with any further processing.
So its' like I start from the top left,in the 2x2 region there are a set of pixel values,then it goes to the next region and so on till the bottom right.

Walter Roberson on 31 Jul 2021
If all you are doing is "observing" the pixels, then a double-nested loop is probably easiest; the time taken to "observe" them (display them as an image, or output them to a command line) would be much higher than any inefficiency due to looping.
If you were doing calculations, then there may be faster or more convenient methods.
Sparsh Garg on 31 Jul 2021
ahhh that sounds reasonable anyways here is the code that I am using,as mentioned earlier would appreciate it if you could give me suggestions on making it faster.

Image Analyst on 31 Jul 2021
Not sure you've got this solved or not after your last comment, but here is how I'd answer your original question:
[rows, columns, numColorChannels] = size(rgbImage)
for row = 1 : rows-1
for col = 1 : columns - 1
subImage = rgbImage(row:row+1, col:col+1, :);
imshow(subImage, 'InitialMagnification', 70000, 'XData', [col, col+1], 'YData', [row, row+1]);
axis('on', 'image');
pause(0.25);
caption = sprintf('Showing rows %d-%d, columns %d-%d.\n', row, row+1, col, col+1);
xlabel(caption);
fprintf('%s\n', caption);
drawnow;
end
end