% function SmoothPrimary(mpd)
%Smooth out primary root coordinates

%Load dataset lists
if ~exist('mpd')
    mpd=uigetdir(pwd,['Select the main project folder']);
end; load([mpd '\dataset.mat']);

for s=1:size(imgpth,1);
    c_data=dataset(s,:);
    c_imgpth=[mpd imgpth(s,end-35:end)];
    c_resfol=[mpd resfol(s,end-19:end)];
    if str2num(c_data(end))>0
        disp(['Processing ' c_data '...']);
        load([c_resfol '\primary.mat'])
        im=imread(c_imgpth); imshow(im); hold on
        plot(prix,priy,'.y')
        
        %% make sure nothing was doubled over
        [C ia ic]=unique([prix priy],'rows'); %
        txyg=[prix(ia) priy(ia)];
        
        %% Smooth out
        if length(txyg)>100
            [xys]=smoothline2(txyg,100,'median'); %remove jagged spots
        else
            xys=txyg;
        end
        if length(txyg)>25
            [xys]=smoothline2(xys,25,'mean'); %remove jagged spots
            prixyS=xys(1:4:end,:);
        else
            xys=txyg;
            prixyS=xys;
        end
                
        plot(prixyS(:,1),prixyS(:,2),'m','LineWidth',2);
        figure(gcf)
        ok=input('Press enter to continue: ');
        save([c_resfol '/primaryS.mat'],'prixyS');
    end
end
disp('Complete')



