画像の全体サイズを変更せずに画像中の一部分のみを縮小する方法
Show older comments

初歩的な質問で申し訳ありません。画像全体のサイズを変更せずに、画像中に含まれるオブジェクトのサイズのみを変更したいです。重複した説明になってしまい申し訳ないのですがたとえばこのような画像において全体のサイズ変更せずにりんごの大きさだけを縮小したいと考えております。実際の画像はグレースケール画像になっております。よろしくお願いいたします。
Accepted Answer
More Answers (1)
Atsushi Ueno
on 30 Jul 2021
Edited: Atsushi Ueno
on 31 Jul 2021
org = rgb2gray(imread('apple.jpeg')); % 以下括弧内はImage Processing Toolboxの対応関数
WB = org < 253; % 二値化する(imbinarize)
new = org(any(WB,2),any(WB,1)); % 余白を除く(imcrop)
new = imresize_wo_toolbox(new, 0.5); % 拡大・縮小(imresize)
org(:) = 255; % 元画像を白紙にする
left_top = (size(org) - size(new))./ 2;
org(left_top(1):(left_top(1)+size(new,1)-1), ... % 縮小オブジェクトを元画像の中央に貼付
left_top(2):(left_top(2)+size(new,2)-1)) = new;
imshow(org);
function Ii = imresize_wo_toolbox(I, ratio) % Image Processing Toolbox無しのimresize
[m,n] = size(I);
sz = round(size(I).*ratio,0);
[X,Y] = meshgrid(1:n,1:m);
[Xi,Yi] = meshgrid(linspace(1,n,sz(2)),linspace(1,m,sz(1)));
Ii = interp2(X,Y,double(I),Xi,Yi);
end % 詳細(interp2を使う)は下記のMATLAB Answers等を参照
3 Comments
金子 夏実
on 30 Jul 2021
Atsushi Ueno
on 31 Jul 2021
論理notは不要だったのでanyに変更し、変数名等を整理しました。
金子 夏実
on 31 Jul 2021
Categories
Find more on MATLAB Mobile の基礎 in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!


