function [finalObjects,varargout] = separateObjects(colorMap,segMasks,objectMap,salientRegion,sThresh)
    if(nargin < 5),
        sThresh = 2.8;
    end
    if(sThresh <= 1),
%         perfect region
        bestk = 1;
    else
        bestk = 0;
    end

    meanSalience = mean([salientRegion.value]);
    

    removeRegions = find([salientRegion.value]'< sThresh*meanSalience);
    salientRegion(removeRegions) = [];

    [sortedEx, order]   = sortrows([[salientRegion.value]' [salientRegion.size]'],[-2 -1]);
    sortedRegion        = salientRegion(order);

    for k = 1:size(sortedRegion,2),
        stats       = regionprops(segMasks{sortedRegion(k).loc},'Centroid','Extrema');
        centers     = stats.Centroid;
        extrema     = stats.Extrema;
        distances   = sqrt( (extrema(:,1) - centers(1)).^2 + (extrema(:,2) - centers(2)).^2);
        meanDist    = mean(distances);
        sortedRegion(k).centerX = centers(1);
        sortedRegion(k).centerY = centers(2);
        sortedRegion(k).dist    = meanDist;
        clear stats;
    end

    %% Distance each center is from other centers
    centerLocs = [[sortedRegion.centerX]' [sortedRegion.centerY]'];
    for k = 1:size(sortedRegion,2),
        distances   = sqrt( (centerLocs(:,1) - sortedRegion(k).centerX).^2 + (centerLocs(:,2) - sortedRegion(k).centerY).^2);
        nearestN    = find(distances <= sortedRegion(k).dist);
        % Remove current point from nearest neighbors
        nearestN(nearestN == k) = [];

        regionColors = colorMap(segMasks{sortedRegion(k).loc});
        curHist = hist(regionColors,[1:10])';
        curInReg    = find(curHist);
        curNotInReg = find(~curHist);
        sortedRegion(k).hist = curHist;

        nearestInReg = false(numel(nearestN),1);
        for j = 1:numel(nearestN);
            regionC_NN = colorMap(segMasks{sortedRegion(nearestN(j)).loc});
            inReg   = sum(isMember(regionC_NN,curInReg));
            outReg  = sum(isMember(regionC_NN,curNotInReg));
            nearestInReg(j) = inReg > outReg;
        end
        nearestN(~nearestInReg) = [];
        sortedRegion(k).numNN = numel(nearestN);
        sortedRegion(k).NNloc = [sortedRegion(k).loc; [sortedRegion(nearestN).loc]'];
    end  
    
    %%
    
    %% Remove from sortedRegion all regions without nearest neighbors
    clear removeRegions;
    %%
    removeRegions = false(size(sortedRegion,2),1);
    for k = 1:size(sortedRegion,2),
        if (sortedRegion(k).numNN == 0)
            if k~=bestk,
                removeRegions(k) = true;
            end
        end
    end
    sortedRegion(removeRegions) = [];
    %%
    regionsLeft = size(sortedRegion,2);
    expand      = zeros(regionsLeft,1);
    for k = 1:regionsLeft,
        connected = zeros(regionsLeft,1);
        for j =1:regionsLeft,
            connected(j) = sum(ismember(sortedRegion(j).NNloc,sortedRegion(k).NNloc));
        end
        connected(k) = 0;
        expand(k) = nnz(connected);
    end
    [sorted,sInd] = sort(expand,'descend');
    %%
    delete = false(regionsLeft,1);
    for p = 1:regionsLeft,
        k = sInd(p);
        connected = zeros(regionsLeft,1);
        for j =1:regionsLeft,
            if (delete(j)),
                connected(j) =0;
            else
                connected(j) = sum(ismember(sortedRegion(j).NNloc,sortedRegion(k).NNloc));
            end
        end
        connected(k) = 0;
        expand = find(connected);

        curLoc = sortedRegion(k).NNloc;

        curColor    = find(sortedRegion(k).hist);
        curNotColor = find(~sortedRegion(k).hist);
        if (~isempty(expand)),
            for n = 1:numel(expand),
                if (~delete(expand)),
                    testLoc     = sortedRegion(expand(n)).NNloc;
                    verifLoc    = false(numel(testLoc),1);
                        for t = 1:numel(testLoc),
                            unkownReg = colorMap(segMasks{testLoc(t)});
                            inReg   = sum(isMember(unkownReg,curColor));
                            outReg  = sum(isMember(unkownReg,curNotColor));
                            verifLoc(t) = inReg > outReg;
                        end
                     if (nnz(~verifLoc) > 0),
                          sortedRegion(expand(n)).NNloc = testLoc(~verifLoc);
                     else
                         sortedRegion(expand(n)).NNloc = 0;
                         delete(expand(n)) = true;
                     end
                     curLoc = union(curLoc, testLoc(verifLoc));
                end
            end
        end
        sortedRegion(k).NNloc = curLoc;
    end
    %% Delete Duplicates

    sortedRegion(delete) = [];

    %%
    regionsLeft = size(sortedRegion,2);
    objects = struct('salience',zeros(regionsLeft,1),'mask',cell(regionsLeft,1),'size',zeros(regionsLeft,1),'centerX',zeros(regionsLeft,1),'centerY',zeros(regionsLeft,1));
    [row,col] = size(segMasks{1});
    for k = 1:regionsLeft,
        objectMask = false(row,col);
        locations = sortedRegion(k).NNloc;
        for j = 1:numel(locations),
            objectMask(segMasks{locations(j)}) = true;
        end
        avgSalience = mean(objectMap(objectMask));
        objects(k).mask = objectMask;
        objects(k).size = nnz(objectMask);
        stats = regionprops(objectMask,'Centroid','MajorAxisLength','MinorAxisLength');
        centers = stats.Centroid;
        objects(k).centerX = centers(1);
        objects(k).centerY = centers(2);
        major = stats.MajorAxisLength;
        minor = stats.MinorAxisLength;
        ratioAxes = min(major,minor)/max(major,minor);        
        objects(k).salience = ratioAxes*avgSalience;
        clear stats;
    end
    %% normalize Salience
    maxSalience = max([objects.salience]');
    %%
    for k = 1:size(objects,1),
        objects(k).salience = objects(k).salience/maxSalience;
    end

    %% Remove Less Salient Objects
    meanSalience = mean([objects.salience]);
    if (meanSalience > .95),
        meanSalience = 0.95;
    end
    %%
    if (size(objects,1) > 1)
        rInd = find([objects.salience]' <= meanSalience);
        objects(rInd) = [];
    end
    %%
    [sorted,finalOrder] = sortrows([[objects.size]' [objects.salience]'],[-1 -2]);

    finalObjects = objects(finalOrder);
    
    %% nice salience map just of objects
    salientObjects = zeros(row,col);
    for k = 1:size(finalObjects,1),
        salientObjects(finalObjects(k).mask) = finalObjects(k).salience;
    end
    varargout{1} = salientObjects;
end