I have an image which I want to divide into overlapping blocks.
I have set the box to be of size 8 rows and 8 columns, and the overlapping factor to be 4 rows/columns.
This is what I have written to solve this:
img = imread('a03-017-05.png');
overlap = 4
count = 1;
for i = 1:overlap:size(img,1)
for j = 1:overlap:size(img,2)
new{count} = img(i:i+8,j:j+8);
count = count+1;
end
end
This works right until it reaches the end of the image where j+8
or i+8
will go beyond the dimensions of the image. Is there any way to avoid this with minimal data loss?
Thanks
If you simply want to ignore the columns/rows that lie outside full sub-blocks, you just subtract the width/height of the sub-block from the corresponding loop ranges:
Let's say your image is 16x16. The block beginning at column
8
will account for the remainder of the columns, so having a starting index between 9 and 16 is pointless.Also, I think your example miscalculates the block size... you're getting blocks that are 9x9. I think you want to do:
As an example, in a 13x13 image with the code above, your row indices will be
[1, 5]
and the row ranges of the blocks will be1:8
and5:12
. Row/column 13 will be left out.