function accept_grid(hObject, eventdata)

global h var
%
newimage = var.newimage;
nBR = var.nBR;
nBL = var.nBL;
nTL = var.nTL;
Vpoints=var.Vpoints;
Hpoints=var.Hpoints;
points=var.points;
pat2=var.pat2;
Vstep=var.Vstep;
Hstep=var.Hstep;


plotting=0;

%%%%%%% Convolution %%%%%%%%%%
ConvImg = real(ifft2(fft2(newimage) .* fft2(pat2,size(newimage,1),size(newimage,2))));
ShiftConvImg = ConvImg(floor(size(pat2,1)/2):end,floor(size(pat2,2)/2):end);
RsizeConvImg = 0*newimage;
RsizeConvImg(1:size(ShiftConvImg,1),1:size(ShiftConvImg,2)) = ShiftConvImg;

var.RsizeConvImg = RsizeConvImg;
subplot(1,1,1)
% h.plot = pcolor(RsizeConvImg);
% if plotting, set(h.plot,'edgecolor','none'), end
% colormap jet
% h.plot = imagesc(RsizeConvImg);
% if plotting, set(h.plot,'edgecolor','none'), end
% set(gca,'YDir','normal');
% colormap jet

hold on
if plotting, plot([nBL(1) nTL(1) nBR(1)], [nBL(2) nTL(2) nBR(2)],'oy'), end
hold off


%%%%%% integrate value in the boxes of convolution
hold on
clear intensity position

cutoff=3;

for i=1:(Vpoints/2)
    for j=1:(Hpoints/2)
        if plotting, plot([points(2*i-1,2*j-1,1)+cutoff points(2*i-1,2*j,1)-cutoff points(2*i,2*j,1)-cutoff ...
                points(2*i,2*j-1,1)+cutoff points(2*i-1,2*j-1,1)+cutoff], ...
                [points(2*i-1,2*j-1,2)+cutoff points(2*i-1,2*j,2)+cutoff points(2*i,2*j,2)-cutoff ...
                points(2*i,2*j-1,2)-cutoff points(2*i-1,2*j-1,2)+cutoff], ...
                '-', 'color', [.6 .6 .6]), end
        
        var.intensity(i,j) = sum(sum(RsizeConvImg(...
            points(2*i-1,2*j-1,2)+cutoff:points(2*i,2*j,2)-cutoff, ...
            points(2*i-1,2*j-1,1)+cutoff:points(2*i,2*j,1)-cutoff)));
        var.intensity(i,j) = var.intensity(i,j)/sum(sum(RsizeConvImg(...
            points(2*i-1,2*j-1,2)+cutoff:points(2*i,2*j,2)-cutoff, ...
            points(2*i-1,2*j-1,1)+cutoff:points(2*i,2*j,1)-cutoff)>-1));
        
        var.xpos(i,j) = mean([points(2*i-1,2*j-1,1);
            points(2*i-1,2*j,1);
            points(2*i,2*j,1);
            points(2*i,2*j-1,1)]);
        var.ypos(i,j) = mean([points(2*i-1,2*j-1,2);
            points(2*i-1,2*j,2);
            points(2*i,2*j,2);
            points(2*i,2*j-1,2)]);
    end
end



%%%%%%%%% integration of the individual points intensities
cutoff = 5;
var.allpoints = zeros(0,1);
var.PointSum = zeros((Vpoints/2),(Hpoints/2));
for i=1:(Vpoints/2)
    for j=1:(Hpoints/2)
        for i2=1:2
            for j2=1:2
                % i2=1 = lower , j2=1 = left
                xrange = var.xpos(i,j) + (j2-1.5)*Hstep + ...
                    [-floor((Hstep-cutoff)/2):floor((Hstep-cutoff)/2)];
                xrange = round(xrange);
                yrange = var.ypos(i,j) + (i2-1.5)*Vstep + ...
                    [-floor((Vstep-cutoff)/2):floor((Vstep-cutoff)/2)];
                yrange = round(yrange);
                
                var.pointint{i,j}((i2-1)*2+j2) = sum(sum(newimage(yrange,xrange)))/(length(xrange)*length(yrange));
                var.pointbox{i,j,(i2-1)*2+j2} = [ ...
                    min(xrange) min(yrange);
                    min(xrange) max(yrange);
                    max(xrange) max(yrange);
                    max(xrange) min(yrange)];
                
                %                 var.allpoints = [var.allpoints;
                %                     sum(sum(newimage(yrange,xrange)))/(length(xrange)*length(yrange))];
                %
                
                
%                 if plotting,plot(var.pointbox{i,j,(i2-1)*2+j2}([1:4 1],1),var.pointbox{i,j,(i2-1)*2+j2}([1:4 1],2),'y'),end
            end
        end
        var.PointSum(i,j) = (sum(var.pointint{i,j}) - 0.5*min(var.pointint{i,j}) -0.5*max(var.pointint{i,j})) / ...
            mean(var.pointint{i,j});
    end
end

hold off



%%%%%% Clustering of the convoluted intensities

% definition of exterior and interior positions
if var.correction==2
    ext = [ones(Hpoints/2,1) [1:(Hpoints/2)]';
        (Vpoints/2)*ones(Hpoints/2,1) [1:(Hpoints/2)]';
        [2:(Vpoints/2)-1]' ones(Vpoints/2-2,1);
        [2:(Vpoints/2)-1]' (Hpoints/2)*ones(Vpoints/2-2,1);
        ];
    for i=1:(Vpoints/2)-2
        inte((i-1)*(Hpoints/2-2)+[1:((Hpoints/2)-2)],:) = [(i+1)*ones(Hpoints/2-2,1) [2:Hpoints/2-1]'];
    end
else
    ext=[];
    for i=1:(Vpoints/2)
        for j=1:(Hpoints/2)
            inte(j+(i-1)*(Hpoints/2),:)= [i j];
        end
    end
end

var.inte = inte;
var.ext = ext;

var.nclustext = 6;
var.nclustinte = 10;
var.thres_frac = 1.2;

% gathering the Conv intensities of interior and exterior positions

for i=1:length(ext)
    intext(i) = var.intensity(ext(i,1),ext(i,2));
end
for i=1:length(inte)
    intinte(i) = var.intensity(inte(i,1),inte(i,2));
end

% clustering of interior and exterior separately
for ii=(3-var.correction):2
    switch ii
        case 1   % exterior case
            allints = intext';
            allpos = ext;
            nclust = var.nclustext;
        case 2
            allints = intinte';
            allpos = inte;
            nclust = var.nclustinte;
    end
    
    T = clusterdata(allints,'maxclust',nclust);
    clear nbT meanclust
    
    for i=1:nclust
        nbT(i) = sum(T==i); %to know the size of the biggest cluster
        meanclust(i) = mean(allints(T==i));
    end
    
    [junk,order] = sort(meanclust);
    tempT = T;
    for i=1:nclust
        T(tempT==order(i)) = i;
    end
    nbT = nbT(order);
    meanclust = meanclust(order);
    
    [nbaseclust,baseclust] = max(nbT);
    
    ConvThres1 = max(allints(T==baseclust))*var.thres_frac;
    
% application of the threshold for the interior and exterior
    for i=1:length(allints)
        var.ConvClust(allpos(i,1),allpos(i,2)) = T(i)+(ii-1)*var.nclustext;
        if allints(i)<ConvThres1
            var.ConvClass(allpos(i,1),allpos(i,2)) = 1;

        else
            var.ConvClass(allpos(i,1),allpos(i,2)) = 3;
        end
    end
    
    switch ii
        case 1   % exterior
            var.ExtConvThres1 = ConvThres1;
            var.New_ExtConvThres1 = ConvThres1;
%        
        case 2
            var.InteConvThres1 = ConvThres1;
            var.New_InteConvThres1 = ConvThres1;

    end
end


%%%%%% clustering of individual intensities


% gathering the Conv intensities of interior and exterior positions
if var.correction==2
    for i=1:length(ext)
        %pointintext(4*(i-1)+[1:4]) = var.pointint{ext(i,1),ext(i,2)}(:);
        PSext(i) = var.PointSum(ext(i,1),ext(i,2));
    end
else
    pointintext = zeros(0,2);
    PSext = zeros(0,1);
end
for i=1:length(inte)
    %pointintinte(4*(i-1)+[1:4]) = var.pointint{inte(i,1),inte(i,2)}(:);
    PSinte(i) = var.PointSum(inte(i,1),inte(i,2));
end

% clustering of interior and exterior separately
var.allpoints = [PSext' ; PSinte'];
var.PointClust = zeros(0,1);
for ii=(3-var.correction):2
    switch ii
        case 1   % exterior case
            %allpoints = pointintext';
            allpoints = PSext';
            allpos = ext;
            nclust = var.nclustext;
        case 2
            allpoints = PSinte';
            allpos = inte;
            nclust = var.nclustinte;
    end
    
    
    T = clusterdata(allpoints,'maxclust',nclust);
    
    clear nbT meanclust
    
    for i=1:nclust
        nbT(i) = sum(T==i);
        meanclust(i) = mean(allpoints(T==i));
    end
    
    [junk,order] = sort(meanclust);
    tempT = T;
    for i=1:nclust
        T(tempT==order(i)) = i;
    end
    nbT = nbT(order);
    meanclust = meanclust(order);
    [nbaseclust,baseclust] = max(nbT);
    
%     PointThres1 = 2.95; %min(allpoints(T==baseclust))/thres_frac;
     PointThres2 = 2.96;
    
    for i=1:length(allpos)
        var.ConvClust(allpos(i,1),allpos(i,2)) = T(i)+(ii-1)*var.nclustext;
        var.PointClass(allpos(i,1),allpos(i,2)) = ...
            (var.PointSum(allpos(i,1),allpos(i,2))>=PointThres2);
    end
    
    switch ii
        case 1   % exterior
           var.ExtPSThres2 = PointThres2;
           var.New_ExtPSThres2 = PointThres2;
        case 2
           var.IntePSThres2 = PointThres2;
           var.New_IntePSThres2 = PointThres2;
    end
end

if var.correction==1
    var.ExtConvThres1 = ConvThres1;
    var.New_ExtConvThres1 = ConvThres1;
    var.ExtPSThres2 = PointThres2;
    var.New_ExtPSThres2 = PointThres2;
end


hold on
if plotting,plot([nBL(1) nTL(1) nBR(1)], [nBL(2) nTL(2) nBR(2)],'oy'),end
hold off




set(h.buttons.gridbutton,'visible','on','backgroundcolor','w')
set(h.buttons.accept_grid,'visible','on','backgroundcolor','w')
set(h.buttons.reject_grid,'visible','on','backgroundcolor','w')
set(h.frame.Grid,'backgroundcolor','w')
set(h.buttons.show_interactions,'backgroundcolor','r','visible','off')
set(h.buttons.change_inte,'backgroundcolor','r','visible','on')


if var.correction==2
set(h.buttons.change_ext,'backgroundcolor','r','visible','on')

else
set(h.buttons.change_ext,'backgroundcolor','r','visible','off')

end


show_interactions
