MATLAB: How to divide square image into rectangle with different size

image processingMATLAB

Hi,
I want to divide an image with 256×256 pixels (x,y) dimensions into 8 parts of rectangle, using sequence {32,8,64,16,8,32,64,32}. Hence I try to code so I can read the horizontal (x) region first, and assigned it into n1,n2.n3,n4,n5,n6,n7,n8. But the code is having mistakes hence i did not know how to proceed. Besides when i apply the codes, how i want to assign the value of n1=32,n2=8 and according to the specific region i try to divide?
%Dividing square image into 8 parts according to the row of image in its
%coordinates
clc;clear;
Pic=imread('cameraman_256.png');
figure(1)
imshow(Pic);
size(Pic)
row=size(Pic,2)
column=size(Pic,1)
if row==(1:32)
n1=row
disp(n1)
elseif row==(33:40)
n2=row
disp(n2)
elseif row==(41:104)
n3=row;
disp(n3)
elseif row==(105:120)
n4=row;
disp(n4)
elseif row==(121:128)
n5=row;
disp(n5)
elseif row==(129:160)
n6=row;
disp(n6)
elseif row==(161:224)
n7=row;
disp(n7)
else row==(223:256)
n8=row;
disp(n8)
end

Best Answer

Syahirah - first, your code
row=size(Pic,2)
column=size(Pic,1)
is mixing up the dimensions (I know that this matrix is a square so doesn't really matter) as the first dimension corresponds to the rows and the second to the columns
numRows = size(Pic,1);
numColums = size(Pic,2);
I've renamed the variables to make it a little more clear on what the variables represent (i.e. numRows for "number of rows").
As for dividing your matrix into smaller matrices, you can use mat2cell. Given your {32,8,64,16,8,32,64,32} then I will assume that you want to create sub-matrices along the rows whose dimensions will be 32x256, 8x256, 64x256, etc. The code for this would be
X = magic(256);
subMatrices = mat2cell(X, [32 8 64 16 8 32 64 32]);
In the above example, you would use your image, Pic, instead of X.