Sliding patches of an image

I have create a loop to crop an image to non overlapping patches.
Now i must change my code to create overlapping patches with step half of the x dimension of the patch
How can i do it?
[rows columns numberOfColorBands] = size(Image);
count=1;
for row=1:patch_row:rows %% patch_row and patch_col are the dimension of the patches
for col=1:patch_col:columns
patch{count}=Image_reshaped(row:row+patch_row-1,col:col+patch_col-1,:);
caption = sprintf('%s_%d.png',name,count);
count=count+1;
end
end
end

5 Comments

The solution seems easy: divide the step size by two, but not the width. How would you implement that?
doesnt work
count=1;
for row=1:patch_row*0.5:rows
for col=1:patch_col:columns
patch{count}=Image_reshaped(row:row+patch_row-1,col:col+patch_col-1,:);
caption = sprintf('%s_%d.png',name,count);
% imwrite(patch{count},fullfile('C:\Users\User\Desktop\test',caption))
count=count+1;
end
end
end
Then you need to change your code.
If you want help, make it easy to be helped. Mention the entire error message. Make sure we can run your code.
For now my guesstimate is that you have an odd patch size and you forgot to round your indices.
I am sorry!I am new tto all this..this is the code for a simple image of 1696x2544x3.
Here is the msg:
Index exceeds matrix dimensions.
Error in test_slide (line 29)
patch{count}=Image_reshaped(row:row+patch_row-1,col:col+patch_col-1,:);
clc;
clear all;
image=imread('C:\Users\User\Desktop\e-Optha_big\C0001273.png');
patch_row=32;
patch_col=32;
x=size(image,1);
y=size(image,2);
X=fix(x/patch_row);%nnumber if blocks in x
Y=fix(y/patch_col);%nnumber if blocks in y
Image_reshaped=imresize(image,[X*patch_row,Y*patch_col]);
[rows columns numberOfColorBands] = size(Image_reshaped);
% disp(size(image))
% disp(size(Image_reshaped))
count=1;
for row=1:patch_row*0.5:rows
for col=1:patch_col:columns
patch{count}=Image_reshaped(row:row+patch_row-1,col:col+patch_col-1,:);
count=count+1;
end
end
row=1:patch_row*0.5:rows;
row(end) %returns 1681
row(end)+patch_row-1 %returns 1712
Your last iteration will attempt to access a whole width, while there is only half a width left.
I would suggest you change your strategy and use indices as your loop variable, and then construct the row and column indices from those loop indices. That will also allow you to store patch properly.

Sign in to comment.

Answers (0)

Categories

Commented:

Rik
on 3 Oct 2020

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!