% function GetRes(mpd)
%To extract image resolution using scale bar

%Load dataset lists
if ~exist('mpd')
    mpd=uigetdir(pwd,['Select the main project folder']);
end
load([mpd '\dataset.mat']);
sizeref=input('What is the length of the reference object in the pictures (in inches)');
for s=1:size(imgpth,1);
    c_data=dataset(s,:);
    c_imgpth=imgpth(s,:);
    c_resfol=resfol(s,:);
    if c_data(end)=='0'
        disp(['Processing ' c_data '...']);
        im=imread(c_imgpth);
        
        % Display image
        im=imread(c_imgpth);
        close all; fo=figure;
        warning('off','images:imshow:magnificationMustBeFitForDockedFigure')
        imshow(im); hold on; set(gcf,'NumberTitle','off','Name',c_data)
        
        % Get scale/resolution from label
        ok='x';
        while ~isempty(ok)
            title('Select  left then parallel right side of the 1.5" label','fontweight','bold','fontsize',12)
            [x1 y1]=ginput(1);
            lft=plot(x1,y1,'.y','MarkerSize',12);
            [x2 y2]=ginput(1);
            rgt=plot(x2,y2,'.y','MarkerSize',12);
            ln=plot([x1;x2],[y1;y2],'-y','LineWidth',2);
            ok=input('Press enter if okay, x to redo: ','s');
            if ~isempty(ok)
                set(lft,'visible','off')
                set(rgt,'visible','off')
                set(ln,'visible','off')
            end
            
            % Calculate resolution
            res_units='(cm)';
            res=sizeref*2.54*1/sqrt((y2-y1)^2+(x2-x1)^2); %1.5in/label x 2.54cm/in x label/pixels = cm/pix
        end
    end
    save([c_resfol '/imageresolution.mat'],'x1','y1','x2','y2','res','res_units');
end