画像をパディングし正方形にする方法

17 views (last 30 days)
拓 青柳
拓 青柳 on 5 Oct 2022
Commented: 拓 青柳 on 19 Oct 2022
深層学習によるX線画像の二値分類を行なっております。
様々なサイズのX線画像があり、全ての画像を長径の長さの正方形にゼロパディングした後、224×224に縮小したいです。
何か方法はありますでしょうか。

Accepted Answer

Akira Agata
Akira Agata on 6 Oct 2022
Edited: Akira Agata on 6 Oct 2022
関数 padarray を使って、以下のようにする方法はいかがでしょうか?
% サンプル画像 (1000×200ピクセル)
I_in = randi(255, [1000 200], "uint8");
% 縦横のピクセル数とその差を取得
sz = size(I_in);
d = abs(diff(sz));
% 縦横のうち短いほうに対して、両側に d/2 だけゼロパディング(※)
if sz(1) < sz(2)
I_pad = padarray(I_in, [round(d/2), 0], 0, "both");
else
I_pad = padarray(I_in, [0, round(d/2)], 0, "both");
end
% 全体を 224×224 にリサイズ
I_out = imresize(I_pad, [224, 224]);
※厳密には、縦横のピクセル数の差が奇数の場合にはどちらか片側のパディング幅を round(d/2)-1 にする必要があります。
  1 Comment
拓 青柳
拓 青柳 on 19 Oct 2022
上手くいきました!
ありがとうございます。

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!