how to divide an image 512x512 into 4x4 overlapping blocks.

1 view (last 30 days)
I have a lena image of 512x512. I want divide the image into 4x4 overlapping blocks, for which i wrote the below code. And i also have to find the no. of 4x4 overlapping blocks. Here I have set a counter to check the no.of 4x4 overlaping blocks. Am i doing it correctly? Please Help.Thanks
[e,f] = size(outImg);
counter=0
for i = 1:e-3
for j = 1:f-3
I = double(outImg((i:i+3),(j:j+3)));
counter=counter+1;
end
end
  1 Comment
Guillaume
Guillaume on 18 Nov 2014
As a piece of advice, use better, more descriptive names for your variables, e.g.
[height, width] = size(inImg);
for row = 1:height-3
for column = 1:width-3
'e' is a particularly bad name, consider the output of the following two lines
1:e-3
1e-3
One character difference, a completely different output. A good source of bugs!

Sign in to comment.

Accepted Answer

Image Analyst
Image Analyst on 18 Nov 2014
Why overlapping? What are you doing with the badly-named I? Nothing. If you want non-overlapping sub-images extracted, you can use indexing or mat2cell. These techniques are shown in the FAQ: http://matlab.wikia.com/wiki/FAQ#How_do_I_split_an_image_into_non-overlapping_blocks.3F
If you want overlapping you can use imfilter(), conv2(), or even blockproc(), but you have to specify some operation, which you haven't done yet.
  1 Comment
Sanghamitra Tripathy
Sanghamitra Tripathy on 18 Nov 2014
Edited: Sanghamitra Tripathy on 18 Nov 2014
Here "I" in which i have stored the 4x4 blocks.I have executed the code but i get a very big value for the count (like (e-3)x(f-3)=259081). If i execute the code for 4x4 block for the entire image,doubt was like should i get such big value(i.e no. of 4x4 blocks=259081) Need to cross check with blockproc() then.

Sign in to comment.

More Answers (1)

Thorsten
Thorsten on 18 Nov 2014
If you do something for N times for M times you do it for N*M times, i.e. for (e-3)*(f-3) times in your example. No need to use a counter, just write
Nblocks = (e-4)*(f-3);
So for a 512x512 image you end up with 259081 blocks.
In your implementation you do not store all the separate blocks in I but just the block for the current i, j, so that after the loop I will be the bottom right 4x4 block in your image. If you sure that you really need to have such a redundant representation of all these blocks, which is roughly 4*4=16 times the size of the image, you can use
I(:,:,counter) = double(outImg((i:i+3),(j:j+3)));

Community Treasure Hunt

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

Start Hunting!