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

深層学習によるX線画像の二値分類を行なっております。
様々なサイズのX線画像があり、全ての画像を長径の長さの正方形にゼロパディングした後、224×224に縮小したいです。
何か方法はありますでしょうか。

 Risposta accettata

Akira Agata
Akira Agata il 6 Ott 2022
Modificato: Akira Agata il 6 Ott 2022

1 voto

関数 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 Commento

拓 青柳
拓 青柳 il 19 Ott 2022
上手くいきました!
ありがとうございます。

Accedi per commentare.

Più risposte (0)

Prodotti

Release

R2022b

Community Treasure Hunt

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

Start Hunting!