%Display results
c_resfol=cd;
im=imread('S:\Lauren\Projects\Root nodule patterning (Frederique)\WTS29 processing\Images\WTS 037-29\WTS 037-29 P1.JPG');

load([c_resfol '/picinfo.mat'],'pltID','age','plane');
load([c_resfol '/nodattchG.mat'],'noddat');
load([c_resfol '/individrootsG.mat'],'attg','xygg');
load([c_resfol '/imageresolution.mat']);
load([c_resfol '/primaryS.mat'],'prixyS');
lat=figure; imshow(rgb2gray(im)); hold on

%%Add primary root to all roots data
prix=flipud(prixyS(:,1));
priy=flipud(prixyS(:,2));
xygg=[flipud(prixyS) zeros(length(prixyS),1);xygg];

colmatch=1:20/64:20;
matrgb=jet;
for rtID=0:max(xygg(:,3)); %% FOR EACH ROOT
    
    %%Coordinates of this root
    rtInd=find(xygg(:,3)==rtID);
    rxy=xygg(rtInd,1:2); %coordinates of this root
    rxy(isnan(rxy(:,2)),:)=[];
    if rtID>0 %add the attachment point
        rxy=[attg(rtID,:);rxy];
    end
    
    
    %%ROOT LENGTH
    dRt=0; %Distance of each point along the root
    for j=2:length(rxy)
        dRt(j,1)=dRt(j-1)+distance(rxy(j,1:2),rxy(j-1,1:2));
    end
    RtLength=dRt(end)*res; %Rt length
    %text(attg(k,1),attg(k,2)-50,num2str(ranked(k)),'color','w')
    %scatter(rxy(1:5:end,1), rxy(1:5:end,2),ones(size(rxy(1:5:end,2)))*3,RtLength+zeros(size(rxy(1:5:end,2))),'filled');
    [v colind]=min(abs(RtLength-colmatch));
%     plot(rxy(1:5:end,1), rxy(1:5:end,2),'color', matrgb(colind,:))
        plot(rxy(1:5:end,1), rxy(1:5:end,2),'g')
% caxis([0 20])
    
end

plot(noddat(:,1),noddat(:,2),'.m')
set(gca, 'YDir', 'reverse'); axis off

