/ / MATLABで画像をサブ画像に分割するときに削除される画像の一部-MATLAB、image-processing

MATLAB - MATLABで画像をサブ画像に分割する際に画像の一部が削除され、画像処理

336 * 104ピクセルの画像があります。35 * 35のサブ画像に分割します。ただし、336と104は35で割り切れないため、分割すると画像の一部が削除されます。 35 * 35ピクセルではない部分のない再構成画像を取得します。私はそれを行うと画像が非常に異なるため、サイズを変更できません。ここに私のコード:

% Size of the image
[H W] = size(im);

% Each sub image has 35*35 pixels
size_sub_image = 35;

% Size of each sub image
H_divided = H/size_sub_image;
W_divided = W/size_sub_image;

% Reminders allowing to take the images which are not divided
R_H = rem(H,size_sub_image);
R_W = rem(W,size_sub_image);

%Variable which is incremented and stand for each sub image
nbr_sub_images = 1;

% Division of the image into sub images
for i = 1:floor(H_divided)
for j = 1:floor(W_divided)

% R_H and R_W allow to create the sub images reminding, which have not
%35*35 pixels.
%My idea was that when each i or j reach the max in the loop, the
%algorithm take the remindind images which have the size of R_H or R_W.
% R_H = 21 (336 = 35*9+21) and R_W = 34 (104 = 35*2+34)

if (i ~= floor(H_divided) && j ~= floor(W_divided))

nbr_sub_images = nbr_sub_images + 1;
im_divided(:,:,nbr_sub_images) = im(size_sub_image*(i-1)+1:size_sub_image*i ,size_sub_image*(j-1)+1:size_sub_image*j );

end

if (i == floor(H_divided))

nbr_sub_images = nbr_sub_images + 1;
im_divided(:,:,nbr_sub_images) = im(size_sub_image*(i-1)+1:size_sub_image*i ,size_sub_image*(j-1)+1:size_sub_image*j );

nbr_sub_images = nbr_sub_images + 1;
im_divided(:,:,nbr_sub_images) = im(size_sub_image*i+1:size_sub_image*i + R_H,size_sub_image*(j-1)+1:size_sub_image*j);

end

if (j == floor(W_divided))

nbr_sub_images = nbr_sub_images + 1;
im_divided(:,:,nbr_sub_images) = im(size_sub_image*(i-1)+1:size_sub_image*i ,size_sub_image*(j-1)+1:size_sub_image*j );

nbr_sub_images = nbr_sub_images + 1;
im_divided(:,:,nbr_sub_images) = im(size_sub_image*(i-1)+1:size_sub_image*i,size_sub_image*j+1:size_sub_image*j + R_W);


end

end
end

実行すると、「添え字付き割り当てディメンションの不一致」というエラーが表示されますが、理由を理解できません R_H そして R_W、コードは完全に機能します。

回答:

回答№1は1

私が理解しているように、あなたは異なるサイズのサブイメージを3-D配列に入れようとしていますが、うまくいきません。配列に2次元画像を追加するとき im_divided、1番目と2番目の次元はすべてのサブ画像で同じ(つまり35x35)でなければなりません。詳細については、 MATLABドキュメンテーション.

考えられる解決策は2つあります。幅と高さが35で割り切れるように画像の端を(ゼロまたはその他の値で)埋め込むか、セル配列を使用してサブ画像を保存します。

im_divided{nbr_sub_images} = im(rows, columns)

セル配列は、さまざまなサイズの行列(または実際にはすべての種類のさまざまなオブジェクト)を格納できます。