How to divide square image into rectangle with different size?

9 ビュー (過去 30 日間)
Syahirah Rahim
Syahirah Rahim 2019 年 3 月 3 日
回答済み: Geoff Hayes 2019 年 3 月 3 日
Hi,
I want to divide an image with 256x256 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

採用された回答

Geoff Hayes
Geoff Hayes 2019 年 3 月 3 日
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.

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeImage Processing Toolbox についてさらに検索

製品


リリース

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by