简体   繁体   中英

How to divide any sized image to equal sized block using only for loop in matlab?

How to divide image to equal sized blocks using simple matlab for loop only? As a beginner I have tried but its showing error.I have done same thing using mat2cell and using simple calculation.

This is my code:

[rows, columns, numberOfColorChannels] = size(p); 
r4 = int32(rows/4); 
c4 = int32(columns/4); 
% Extract images. 
image1 = p(1:r4, 1:c4); 
image2 = p(1:r4, c4+1:2*c4); 
image3 = p(1:r4, 2*c4+1:3*c4); 
image4 = p(1:r4, 3*c4+1:4*c4); 

I need to do it with a for loop only.

First things first if you separate x and y into 4 equally sized sections you will get 16 smaller images. You need to understand this first part.

[rows, columns, numberOfColorChannels] = size(p); 
r4 = int32(rows/4); 
c4 = int32(columns/4); 
output = zeros(16,r4,c4,numberOfColorChannels);
cnt = 1;
for i=1:4,
   for j=1:4,
      output(cnt,:,:,:) = p((i-1)*r4+1:i*r4, (j-1)*c4+1:j*c4); 
      cnt = cnt + 1;
   end
end

The code basically does what you've already done but in two dimensions.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM