function [after_conv,Kernel] = bg_conv_gauss_corr(bg_mask,convd,thresh)
    %% to smooth the sharp peaks or bottoms on the boundary

    xdim = size(bg_mask,1);
    ydim = size(bg_mask,2);
    
    [Y, X] = meshgrid(0:1:ydim-1, 0:1:xdim-1);
    
    % Kernel is a big plot of Gaussian function at the image region
    Kernel = exp(-1*((Y-ceil((ydim-1)/2)).^2+(X-ceil((xdim-1)/2)).^2)*log(2)/(convd)^2);
    
    mask_pad = zeros(2*xdim-1, 2*ydim-1);
    mask_pad(floor((xdim+1)/2):floor((xdim+1)/2)+xdim-1,floor((ydim+1)/2):floor((ydim+1)/2)+ydim-1) = bg_mask; % just padding with zeros
   
    Ker_pad = zeros(2*xdim-1, 2*ydim-1);
    Ker_pad(floor((xdim+1)/2):floor((xdim+1)/2)+xdim-1,floor((ydim+1)/2):floor((ydim+1)/2)+ydim-1) = Kernel; % just padding with zeros
    
    % convolution theorem: F(f*g)=F(f)*F(g), F is the fourier transform
    % operation
    Conv = fftshift(ifftn(fftn(ifftshift(mask_pad)).*fftn(ifftshift(Ker_pad))));  
    
    % crop the original region
    after_conv = Conv(floor((xdim+1)/2):floor((xdim+1)/2)+xdim-1,floor((ydim+1)/2):floor((ydim+1)/2)+ydim-1);
        
    after_conv = after_conv > thresh;
end
                
                
