function [pix0,candidate_pix,neighbor_record,summation,neighbor_record2,summation2] = growRegion2D(pix0,dF,candidate_pix,filter,neighbor_record,summation,neighbor_record2,summation2,sigma,minIntesity)

[H,W] = size(dF);

if(~exist('minIntesity','var'))
    minIntesity = 0;
end

N = sum(filter(:));

n_neighbors = double(neighbor_record(candidate_pix));
predicted = summation(candidate_pix);
predicted = predicted./n_neighbors;
n_neighbors2 = double(neighbor_record2(candidate_pix));
predicted2 = summation2(candidate_pix);
predicted2 = predicted2./n_neighbors2;

%% judge
maxIntensity = max(dF(pix0));
prior1 = (n_neighbors/N);%.*(maxIntensity-dF(candidate_pix))/maxIntensity;
prior0 = 1 - prior1;
thresholds = min(predicted,max(max(minIntesity,predicted/10),(predicted-predicted2)/2 - sigma^2./(predicted-predicted2).*log(prior1./prior0)))+predicted2;
selected = dF(candidate_pix)>thresholds;
add_pixels = candidate_pix(selected);

%% update
pix0 = [pix0;add_pixels];
dw = zeros(9,1);
dh = zeros(9,1);
cnt = 1;
for x = -1:1
    for y = -1:1
        dw(cnt) = x;
        dh(cnt) = y;
        cnt = cnt + 1;
    end
end

candidate_pix = [];
for kk = 1:9
    [ih0,iw0] = ind2sub([H,W],add_pixels);
    ih1 = max(min(ih0+dh(kk),H),1);
    iw1 = max(min(iw0+dw(kk),W),1);
    pix1 = sub2ind([H,W],ih1,iw1);
    candidate_pix = [pix1;candidate_pix];
end
candidate_pix = setdiff(candidate_pix,pix0);
candidate_pix = unique(candidate_pix);

%% update matrix
[dx,dy] = ind2sub(size(filter),find(filter>0));
radius = (size(filter,1)-1)/2;
dx = dx - radius - 1;
dy = dy - radius - 1;

intensities = dF(add_pixels);
for i = 1:numel(dx)
    [ih,iw] = ind2sub([H,W],add_pixels);
    ih0 = min(max(ih + dx(i),1),H);
    iw0 = min(max(iw + dy(i),1),W);
    add_pix_shift = sub2ind([H,W],ih0,iw0);
    summation(add_pix_shift) = summation(add_pix_shift) + intensities;
    neighbor_record(add_pix_shift) = neighbor_record(add_pix_shift)+1;
%     summation2(add_pix_shift) = summation2(add_pix_shift) - intensities;
%     neighbor_record2(add_pix_shift) = neighbor_record2(add_pix_shift)-1;
end




end