複数のjpegイメージを一括でトリミングする方法
17 views (last 30 days)
Show older comments
同フォルダにある複数のjpegイメージを一括で同じ範囲でトリミングする方法はありますか?
まず,imreadでそれぞれの画像情報を読み込んでも,
それらの情報を1つにまとめられないので難航しています.
よろしくお願いいたします.
0 Comments
Accepted Answer
Atsushi Ueno
on 2 Dec 2021
Edited: Atsushi Ueno
on 3 Dec 2021
Image Processing Toolboxの製品指定も無い為、MATLAB(R2021a)単体で実行可能である事に拘ってます。
Bx = [10, 30, 75, 50]; % トリミング範囲(x座標,y座標,w(幅),h(高さ))
imds = imageDatastore(uigetdir); % 選択フォルダの画像をイメージデータストアとして取得
%imds = imageDatastore([matlabroot '/toolbox/images/imdata']); % 動作確認用
number = numel(imds.Files); % 画像ファイル数
rowlen = ceil(sqrt(number)); % ギャラリの行数
collen = floor(sqrt(number)); % ギャラリの列数
for index = 1:number
image = imread(imds.Files{index}); % 各画像情報を読み込む
if size(image,3) == 1 % モノクロ画像はカラー形式(のモノクロ)に変換
image = repmat(image(:,:,1),[1 1 3]); % 3枚(RGB)重ねる
end
image = image(Bx(2):Bx(2)+Bx(4)-1, Bx(1):Bx(1)+Bx(3)-1,1:3); % トリミング
[row col] = ind2sub([rowlen collen], index); % ギャラリの位置を計算
gallery(Bx(4)*(row-1)+1:Bx(4)*row,Bx(3)*(col-1)+1:Bx(3)*col,:) = image; % ギャラリに追加
% [filepath, name, ext] = fileparts(imds.Files{index}); % 元画像のパスを得る
% imwrite(image, [filepath,filesep,'trimmed_',name,ext]); % 別名でトリム画像を保存
end
imshow(gallery); % ギャラリを表示
2 Comments
Atsushi Ueno
on 3 Dec 2021
Edited: Atsushi Ueno
on 3 Dec 2021
上記質問を受けて(?)見直すと酷かったので勝手ながら修正しました
- ギャラリの添字を計算するのにind2sub関数を使うように変更
- ギャラリの扱いが行優先だった⇒MATLABなので列優先に変更
- 添え字の扱いが0ベースだった⇒MATLABなので1ベースに変更
- その他、コメント・変数名・実行順等
More Answers (0)
See Also
Categories
Find more on Image Processing Toolbox 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!