長方形の画像(JPG)をレターボックス処理を行い正方形にしたいのですが、可能でしょうか?
4 views (last 30 days)
Show older comments

0 Comments
Accepted Answer
Atsushi Ueno
on 5 Jan 2022
入力画像の幅・高さの内大きい方のサイズに合わせた正方形の黒い画像の中央に元画像をコピーします。
I = imread('peppers.png');
sz = size(I); % 画像の幅・高さ・色数を取得
mx = max(sz(1:2));
LtBx = uint8(zeros(mx,mx,3)); % 幅・高さの大きい方に合わせた正方形の黒画像
if sz(1) < mx % 元画像が横長の場合
top = floor((mx-sz(1)) / 2);
LtBx(top:top+sz(1)-1,:,:) = I; % 黒画像の中央に元画像をコピー
else % 元画像が縦長の場合
left = floor((mx-sz(2)) / 2);
LtBx(:,left:left+sz(2)-1,:) = I; % 黒画像の中央に元画像をコピー
end
imshow(LtBx);
More Answers (0)
See Also
Categories
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!