function set_faces11_borderpoints_hat_beard
%function set_faces11_borderpoints_hat_beard
%
%from AAMToolbox create a new model based on faces8.temp_dat
%(This will automatically create
%cd into the required project folder and run the function
%
%set points 159 to 166 to the border of the cropped image
template_name='Faces11';
pathname=fullfile('PointModels',template_name);
d=dir(fullfile(pathname,'*_pm.mat'));
for i=1:length(d)
    filename=d(i).name;
    disp(sprintf('working on %s file',filename));
    q=load(fullfile(pathname,filename));
    pts2=q.pts;
    if size(q.pts,2)==2
        x=q.pts(:,1);
        y=q.pts(:,2);
    else
        disp('changing box points')
        x=q.pts(1:2:end);
        y=q.pts(2:2:end);
        file=filename(1:findstr(filename,'_pm')-1);
        im=imread(fullfile('Cropped',[file,'.jpg']));
        mask1=im(:,:,1)>250 & im(:,:,2)>250 & im(:,:,3)>250;
        mask=imdilate(mask1,ones(7));
        bb=regionprops(double(~mask),'boundingbox');
        bb=bb.BoundingBox;
        i=159;
        x(i)=round(bb(1)); y(i)=round(bb(2));% top left
        i=i+1;
        x(i)=round(bb(1)+bb(3)/2); y(i)=round(bb(2));% top middle
        i=i+1;
        x(i)=round(bb(1)+bb(3)); y(i)=round(bb(2));% top right
        i=i+1;
        x(i)=round(bb(1)+bb(3)); y(i)=round(bb(2)+bb(4)/2);% right middle
        i=i+1;
        x(i)=round(bb(1)+bb(3)); y(i)=round(bb(2)+bb(4));% bottom right
        i=i+1;
        x(i)=round(bb(1)+bb(3)/2); y(i)=round(bb(2)+bb(4));% bottom middle
        i=i+1;
        x(i)=round(bb(1)); y(i)=round(bb(2)+bb(4));% bottom left
        i=i+1;
        x(i)=round(bb(1)); y(i)=round(bb(2)+bb(4)/2);% left middle
        % now equivalence the hat points with hair
        hs=1;k=1;
        ii=199;jj=135;x(ii)=x(jj);y(ii)=y(jj); 
        hat_set(hs)=ii;hs=hs+1; % top centre
        hair_set(k)=jj;k=k+1;
        ii=189;jj=135;x(ii)=x(jj);y(ii)=y(jj); 
        hat_set(hs)=ii;hs=hs+1; % middle
        hair_set(k)=jj;k=k+1;
        ii=ii-1;jj=jj+1;x(ii)=x(jj);y(ii)=y(jj); 
        hat_set(hs)=ii;hs=hs+1; % left 1
        hair_set(k)=jj;k=k+1;
        ii=ii-1;jj=jj+1;x(ii)=x(jj);y(ii)=y(jj); 
        hat_set(hs)=ii;hs=hs+1; % left 2
        hair_set(k)=jj;k=k+1;
        ii=ii-1;jj=jj+1;x(ii)=x(jj);y(ii)=y(jj); 
        hat_set(hs)=ii;hs=hs+1; % left 3
        hair_set(k)=jj;k=k+1;
        ii=ii-1;jj=jj+1;x(ii)=x(jj);y(ii)=y(jj); 
        hat_set(hs)=ii;hs=hs+1; % left 4
        hair_set(k)=jj;k=k+1;
        ii=189;jj=135;
        ii=ii+1;jj=jj-1;x(ii)=x(jj);y(ii)=y(jj); 
        hat_set(hs)=ii;hs=hs+1; % right 1
        hair_set(k)=jj;k=k+1;
        ii=ii+1;jj=jj-1;x(ii)=x(jj);y(ii)=y(jj); 
        hat_set(hs)=ii;hs=hs+1; % right 2
        hair_set(k)=jj;k=k+1;
        ii=ii+1;jj=jj-1;x(ii)=x(jj);y(ii)=y(jj); 
        hat_set(hs)=ii;hs=hs+1; % right 3
        hair_set(k)=jj;k=k+1;
        ii=ii+1;jj=jj-1;x(ii)=x(jj);y(ii)=y(jj); 
        hat_set(hs)=ii;hs=hs+1; % right 4
        hair_set(k)=jj;k=k+1;
        ii=185;jj=139;
        ii=ii-1;jj=jj;x(ii)=x(jj);y(ii)=y(jj); 
        hat_set(hs)=ii;hs=hs+1; % 1
        ii=ii-1;jj=jj;x(ii)=x(jj);y(ii)=y(jj); 
        hat_set(hs)=ii;hs=hs+1; % 2
        ii=ii-1;jj=jj;x(ii)=x(jj);y(ii)=y(jj); 
        hat_set(hs)=i;hs=hs+1; % 3
        ii=ii;jj=jj-1;x(ii)=x(jj);y(ii)=y(jj); 
        hat_set(hs)=ii;hs=hs+1; % 4
        ii=194;jj=131;
        ii=ii;jj=jj+1;x(ii)=x(jj);y(ii)=y(jj); 
        hat_set(hs)=ii;hs=hs+1; % 1
        ii=ii;jj=jj+1;x(ii)=x(jj);y(ii)=y(jj); 
        hat_set(hs)=ii;hs=hs+1; % 2
        ii=ii;jj=jj+1;x(ii)=x(jj);y(ii)=y(jj); 
        hat_set(hs)=ii;hs=hs+1; % 3
        ii=ii;jj=jj+1;x(ii)=x(jj);y(ii)=y(jj); 
        hat_set(hs)=ii;hs=hs+1; % 4
        for ii=130:-1:127,
            hair_set(k)=ii;k=k+1;
        end
        for ii=130:-1:127,
            hair_set(k)=ii;k=k+1;
        end
        k=1;
        for ii=1:32,
            face_border_set(k)=ii;k=k+1;
        end
        k=1;
        for ii=34:45,
            mouth_set(k)=ii;k=k+1;
        end
        for ii=153:158,
            mouth_set(k)=ii;k=k+1;
        end
        k=1;
        for ii=147:152,
            muzzle_set(k)=ii;k=k+1;
        end
        ii=33;muzzle_set(k)=ii;k=k+1;
        k=1;
        for ii=46:52,
             nose_set(k)=ii;k=k+1;
        end
        nose_set(k)=145;k=k+1;
        nose_set(k)=146;k=k+1;
        nose_set(k)=53;k=k+1;
        nose_set(k)=86;k=k+1;
        nose_set(k)=85;k=k+1;
        nose_set(k)=87;k=k+1;
        k=1;
        for ii=96:118,
             left_eye_set(k)=ii;k=k+1;
        end
        k=1;
        for ii=54:76,
             right_eye_set(k)=ii;k=k+1;
        end
        k=1;
        for ii=77:84,
             eyebrows_set(k)=ii;k=k+1;
        end
        for ii=88:95,
             eyebrows_set(k)=ii;k=k+1;
        end
        k=1;
        for ii=171:175,
             ears_set(k)=ii;k=k+1;
        end
        for ii=176:180,
             ears_set(k)=ii;k=k+1;
        end
        k=1;
        ii=144;shoulders_neck_set(k)=ii;k=k+1;
        ii=169;shoulders_neck_set(k)=ii;k=k+1;
        ii=126;shoulders_neck_set(k)=ii;k=k+1;
        ii=170;shoulders_neck_set(k)=ii;k=k+1;
        for ii=119:125,
             ears_set(k)=ii;k=k+1;
        end
        ii=167;shoulders_neck_set(k)=ii;k=k+1;
        ii=168;shoulders_neck_set(k)=ii;k=k+1;
        k=1;
        for ii=159:166,
             border_set(k)=ii;k=k+1;
        end
        edit_sets{1}.indexes=ears_set;
        edit_sets{1}.label='Ears';
        edit_sets{2}.indexes=eyebrows_set;
        edit_sets{2}.label='Eyebrows';
        edit_sets{3}.indexes=left_eye_set;
        edit_sets{3}.label='Left eye';
        edit_sets{4}.indexes=right_eye_set;
        edit_sets{4}.label='Right eye';
        edit_sets{5}.indexes=hair_set;
        edit_sets{5}.label='Hair';
        edit_sets{6}.indexes=muzzle_set;
        edit_sets{6}.label='Muzzle';
        edit_sets{7}.indexes=mouth_set;
        edit_sets{7}.label='Mouth';
        edit_sets{8}.indexes=shoulders_neck_set;
        edit_sets{8}.label='Shoulders neck';
        edit_sets{9}.indexes=border_set;
        edit_sets{9}.label='Border';
    end
    pts2(1:2:end)=x;
    pts2(2:2:end)=y;
    pts=pts2(:);
%    save(fullfile(pathname,filename),'pts');
end
% now insert the edit_sets into the template
filename=fullfile('Templates',[template_name,'.edit_sets']);
save(filename,'edit_sets');

    
    