user3424964
user3424964

Reputation: 19

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.

Upvotes: 2

Views: 1399

Answers (1)

carlosdc
carlosdc

Reputation: 12132

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.

Upvotes: 1

Related Questions