function index_roi= mysplit(img,dst,mean_tresh)

img = double(img);

[x,y] = size(img);
index_roi= ones(x,y);



l_indici = {1};
t=2;
v_roi = {};
while length(l_indici)>0;
    
    ind = l_indici{1};
    l_indici(1) = [];
   
    img_roi = img(index_roi==ind);

    [i_vec,j_vec] = find(index_roi==ind);
    
    i_x = min(i_vec);
    f_x = max(i_vec);
    
    i_y = min(j_vec);
    f_y = max(j_vec);
    
    %lunghezza = length(img_roi);
    %standa =std(img_roi);
    if std(img_roi) > dst
        index_roi(i_x:i_x+floor((f_x-i_x)/2),i_y:i_y+floor((f_y-i_y)/2)) = t;
        l_indici{end+1} = t;
        t=t+1;
        
        index_roi(i_x:i_x+floor((f_x-i_x)/2),i_y+floor((f_y-i_y)/2)+1:f_y) = t;
        l_indici{end+1} = t;
        t=t+1;
        
        index_roi(i_x+floor((f_x-i_x)/2)+1:f_x,i_y+floor((f_y-i_y)/2)+1:f_y) = t;
        l_indici{end+1} = t;
        t=t+1;

        index_roi(i_x+floor((f_x-i_x)/2)+1:f_x,i_y:i_y+floor((f_y-i_y)/2)) = t;
        l_indici{end+1} = t;
        t=t+1;

    else
        if length(v_roi) > 0
            v_roi{end+1} = ind;
        else
            v_roi{1} = ind;
        end

    
    end

end

'END SPLIT'
s = strel('diamond',1);
while length(v_roi)>0
    
    bo = 0;
    v1 = v_roi{1};
    m1 = mean(img(index_roi==v1));
    
    l = length(v_roi)
    for i=2:l
        v2 =  v_roi{i};
        m2 = mean(img(index_roi==v2));
        
        standa = std(img(index_roi==v1 | index_roi==v2));
        if abs(m1-m2)<mean_tresh & standa <=dst
%             [x_vec1,y_vec1] = find(index_roi==v1);
%             [x_vec2,y_vec2] = find(index_roi==v2);
%             uno = [x_vec1,y_vec1];
%             due = [x_vec2,y_vec2];
%             
% %             imagesc(sr2+selected_roi)
% %             colormap(gray)
% %             waitforbuttonpress;
%             [k1,z1] = size(uno);
%             [k2,z2] = size(due);
%             ind = 0;
%             for ig=1:k1
%                 for jg=1:k2
%                     distanza = sum(abs(uno(ig,:)-due(jg,:)));
%                     if distanza <= 1
%                         break
%                     end
%                 end
%                 if distanza <= 1
%                     break
%                 end
%             end
%                     
%             if distanza<=1
              r1 = index_roi==v1;
              r2 = index_roi==v2;
              r1 = imdilate(r1,s);

              intersezione = r1 & r2;

              if sum(intersezione(:)) > 0
                index_roi(index_roi==v2)= v1;
                v_roi(i)=[];
                v_roi(1)=[];
                v_roi{end+1}=v1;
                bo = 1;
                
%                 R = zeros(x,y);
%                 l = length(v_roi);
%                 for iz =1:l
%                     R = R + list_roi{iz};
%                 end
%                 imagesc(R)
%                 colormap(jet)
% 
%                 waitforbuttonpress

                break;
            end
            
        end
        
        
    end
    
    if bo==0
        v_roi(1)=[];
    end
end

