nasのブログ -6ページ目

nasのブログ

ブログの説明を入力します。

画像を切り取って保存する。

 

%% 初期設定
clear all
cropedsave = 1;%切り取った画像を保存する

%% フォルダから読み込む。
homedir=pwd;
imgdir = uigetdir;%フォルダをひらく
if(cropedsave==1)
    croppedfolder=inputdlg('切り取り画像フォルダ名は?','Input');if(isempty(croppedfolder));croppedfolder='cropped';end;
    cd(imgdir);mkdir(croppedfolder);cd(croppedfolder);
end
flist = imageSet(imgdir);%画像ファイルリスト取り込み
imgs = cell(flist.Count,1);%ファイルパス格納用
rect = zeros(flist.Count,4);%座標格納用
n=1;
for m=1:flist.Count%フォルダの画像ファイルを全部処理
    [cimg,crect] = imcrop(imread(cell2mat(flist.ImageLocation(m))));%トリミング
    if(isempty(cimg)==0)
    rect(n,1:4)=round(crect);%imcropは小数ででてくるので。
    imgs(n,1)=flist.ImageLocation(m);%ファイルパスを記録
    if(cropedsave==1);[~,fn,ext]=fileparts(cell2mat(flist.ImageLocation(n)));imwrite(cimg,[fn '_c' ext],'bmp');end;
    n=n+1;
    end
end
imset = table(imgs,rect);%テーブル形式に変換。
imset(n:end,:)=[];%空行列を削除
cd(homedir);
save([imgdir(max(strfind(imgdir,'\'))+1:end) '.mat'],'imset');
close all;