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

조회 수: 7 (최근 30일)
mi
mi 2021년 12월 2일
편집: Atsushi Ueno 2021년 12월 3일
同フォルダにある複数のjpegイメージを一括で同じ範囲でトリミングする方法はありますか?
まず,imreadでそれぞれの画像情報を読み込んでも,
それらの情報を1つにまとめられないので難航しています.
よろしくお願いいたします.

채택된 답변

Atsushi Ueno
Atsushi Ueno 2021년 12월 2일
편집: Atsushi Ueno 2021년 12월 3일
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개)

카테고리

Help CenterFile Exchange에서 Image Processing Toolbox에 대해 자세히 알아보기

제품


릴리스

R2021a

Community Treasure Hunt

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

Start Hunting!