I want to divide an image into 25 equal tiles with the help of for loop...

7 views (last 30 days)
I've done it with my basic knowledge but its very large code plz help me out to convert it into for loop my code is as follow,
_ _ _ _
__
aj=imread('cameraman.tif'); %Image contains my real time edge detected picture but here taken Cameraman as an example
[h w]=size(aj)
wm=floor(w/5);
hm=floor(h/5);
figure;
aj1=aj(1:hm,1:wm,:);
subplot(5,5,1);imshow(aj1);
aj2=aj(1:hm,wm:2*wm,:);
subplot(5,5,2);imshow(aj2);
aj3=aj(1:hm,2*wm:3*wm,:);
subplot(5,5,3);imshow(aj3);
aj4=aj(1:hm,3*wm:4*wm,:);
subplot(5,5,4);imshow(aj4);
aj5=aj(1:hm,4*wm:5*wm,:);
subplot(5,5,5);imshow(aj5);
aj6=aj(hm:2*hm,1:wm,:);
subplot(5,5,6);imshow(aj6);
aj7=aj(hm:2*hm,wm:2*wm,:);
subplot(5,5,7);imshow(aj7);
aj8=aj(hm:2*hm,2*wm:3*wm,:);
subplot(5,5,8);imshow(aj8);
aj9=aj(hm:2*hm,3*wm:4*wm,:);
subplot(5,5,9);imshow(aj9);
aj10=aj(hm:2*hm,4*wm:5*wm,:);
subplot(5,5,10);imshow(aj10);
aj11=aj(2*hm:3*hm,1:wm,:);
subplot(5,5,11);imshow(aj11);
aj12=aj(2*hm:3*hm,wm:2*wm,:);
subplot(5,5,12);imshow(aj12);
aj13=aj(2*hm:3*hm,2*wm:3*wm,:);
subplot(5,5,13);imshow(aj13);
aj14=aj(2*hm:3*hm,3*wm:4*wm,:);
subplot(5,5,14);imshow(aj14);
aj15=aj(2*hm:3*hm,4*wm:5*wm,:);
subplot(5,5,15);imshow(aj15);
aj16=aj(3*hm:4*hm,1:wm,:);
subplot(5,5,16);imshow(aj16);
aj17=aj(3*hm:4*hm,wm:2*wm,:);
subplot(5,5,17);imshow(aj17);
aj18=aj(3*hm:4*hm,2*wm:3*wm,:);
subplot(5,5,18);imshow(aj18);
aj19=aj(3*hm:4*hm,3*wm:4*wm,:);
subplot(5,5,19);imshow(aj19);
aj20=aj(3*hm:4*hm,4*wm:5*wm,:);
subplot(5,5,20);imshow(aj20);
aj21=aj(4*hm:5*hm,1:wm,:);
subplot(5,5,21);imshow(aj21);
aj22=aj(4*hm:5*hm,wm:2*wm,:);
subplot(5,5,22);imshow(aj22);
aj23=aj(4*hm:5*hm,2*wm:3*wm,:);
subplot(5,5,23);imshow(aj23);
aj24=aj(4*hm:5*hm,3*wm:4*wm,:);
subplot(5,5,24);imshow(aj24);
aj25=aj(4*hm:5*hm,4*wm:5*wm,:);
subplot(5,5,25);imshow(aj25);
  5 Comments
Matt J
Matt J on 5 Feb 2016
Edited: Matt J on 5 Feb 2016
I think splitting into separate images will be faster than blockproc. However, see also my Answer using sepblockfun. That should be even faster.

Sign in to comment.

Answers (3)

Matt J
Matt J on 5 Feb 2016
Edited: Matt J on 5 Feb 2016
Consider using MAT2TILES ( Download ).
ajTiles=mat2tiles(imread('cameraman.tif'),[hm,wm]+1).';
for k=1:25
subplot(5,5,k); imshow(ajTiles{k});
end

Image Analyst
Image Analyst on 5 Feb 2016
So, other than not knowing how to format your code, which can be solved with this link, what's the problem?
There is a FAQ on dividing an image up into tiles/blocks: http://matlab.wikia.com/wiki/FAQ#How_do_I_split_an_image_into_non-overlapping_blocks.3F

Matt J
Matt J on 5 Feb 2016
Edited: Matt J on 5 Feb 2016
Actually I want to divide image in small tiles and then need to count number of white pixels in each tile.
You may have to pad your image so that it divides evently into hm x wm blocks, but once you do, you can use SEPBLOCKFUN ( Download ) to efficiently count the number of white pixels per block. Example,
blackwhite=rand(15)<.5; %Hypothetical black and white image
counts3x3=sepblockfun(blackwhite ,[3,3],'sum');
%Number of white pixels per 3x3 block

Community Treasure Hunt

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

Start Hunting!