複数のjpegイメージを一括でトリミングする方法

同フォルダにある複数のjpegイメージを一括で同じ範囲でトリミングする方法はありますか?
まず,imreadでそれぞれの画像情報を読み込んでも,
それらの情報を1つにまとめられないので難航しています.
よろしくお願いいたします.

 採用された回答

Atsushi Ueno
Atsushi Ueno 2021 年 12 月 2 日
編集済み: Atsushi Ueno 2021 年 12 月 3 日

0 投票

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
Warning: The ability to read FITS files with IMREAD may be removed in a future release. Use FITSREAD to import FITS files.
imshow(gallery); % ギャラリを表示

2 件のコメント

mi
mi 2021 年 12 月 3 日
できました.ありがとうございます。
Atsushi Ueno
Atsushi Ueno 2021 年 12 月 3 日
編集済み: Atsushi Ueno 2021 年 12 月 3 日
上記質問を受けて(?)見直すと酷かったので勝手ながら修正しました
  • ギャラリの添字を計算するのにind2sub関数を使うように変更
  • ギャラリの扱いが行優先だった⇒MATLABなので列優先に変更
  • 添え字の扱いが0ベースだった⇒MATLABなので1ベースに変更
  • その他、コメント・変数名・実行順等

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeImage Processing Toolbox についてさらに検索

製品

リリース

R2021a

質問済み:

mi
2021 年 12 月 2 日

編集済み:

2021 年 12 月 3 日

Community Treasure Hunt

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

Start Hunting!