% function [imgpth resfol dataset mpd]=ListDataset2(mpd);
%Make list of entire data set for programs to run through
disp('You images should be on a black background, if you have non-black parts such as clear border')
disp('you will have to crop your images');
cropim=input('Do you need to crop images y/n','s');
if cropim=='y'
% global mpd
if ~exist('mpd')
    mpd=uigetdir(pwd,['Select the main project directory (that contains the original Images folder and where all other results will be saved)']);
end
% d_img=uigetdir(pwd,['Select the main image folder']);
% d_res=uigetdir(pwd,['Select/create the folder where you want to save the mat-file data']);

d_img=[mpd '\Images'];
d_res=[mpd '\Mat-files'];
d_imcrop=[mpd '\Images_cropped'];
imgpth=[];
resfol=[];
dataset=[];
imgpth_cropped=[];
if ~exist(d_imcrop,'dir');
    mkdir(d_imcrop);
end

cd(d_img);
pltf=dir;
for i1=3:size(pltf)
    pltimd=[d_img '\' pltf(i1).name];
    pltimd_cropped=[d_imcrop '\' pltf(i1).name];
    cd(pltimd)
    imgs=dir('*.jpg'); %go through images
    for i3=1:size(imgs,1)
        
        %% Get pic info
        groupID=imgs(i3).name(1:3);
        pltID=imgs(i3).name(5:7);
        age=imgs(i3).name(9:10);
        plane=imgs(i3).name(12:13);
        
        %% Make list of data and results folders
        dataset=cat(1,dataset,imgs(i3).name(1:13));
        imgpth=cat(1,imgpth,[pltimd '\' imgs(i3).name]);
        imgpth_cropped=cat(1,imgpth_cropped,[pltimd_cropped '\' imgs(i3).name]);
        
        im=imread(imgpth(i3,:));
        imshow(im);
        imcropped=imcrop(im);
        
        if ~exist(pltimd_cropped,'dir')
            mkdir(pltimd_cropped);
        end
        imwrite(imcropped,imgpth_cropped(i3,:));
       
    end
end


disp('A new folder has been created that contains your cropped images - rename your original image folder as Images_old and ')
disp('rename the cropped images folder Images')

else
    disp('Proceed to next program (ListDataset program)')
end
