function L = LIS_smooth(R, sigma)
% LIS_smooth       Find labels after Gaussian smoothing of data cost matrix R
%                  Labeled Image Segmentation (LIS) smoothing.
%
%  use: Lg = LIS_smooth(R, sigma);
% ----------------------------------------------------------------------------
%  Lg      Label image of size IxJ, Lg(i,j) in 1,2,...,M
%  R       Data cost matrix, size IxJxM (double)
%  sigma   Standard deviation for Gaussian smoothing filter
% ----------------------------------------------------------------------------
% ex:
% clear all; tim=4; sigma=3; R=LIS_getR(tim+100); [I,J,M]=size(R);
% Lg = LIS_smooth(R, sigma);

%----------------------------------------------------------------------
% Copyright (c) 2012.  Karl Skretting.  All rights reserved.
% University of Stavanger, Signal Processing Group
% Mail:  karl.skretting@uis.no   Homepage:  http://www.ux.uis.no/~karlsk/
% 
% HISTORY:  dd.mm.yyyy
% Ver. 1.0  01.11.2012  Made function
% Ver. 1.1  15.11.2012  Some small updates
%----------------------------------------------------------------------

if (sigma <= 0.1)
    [~, L] = min(R,[],3);  
else
    H = fspecial('gaussian',floor(6*sigma+1),sigma);
    M = size(R,3);
    Rf = double(R);
    for m = 1:M 
        Rf(:,:,m) = filter2(H, Rf(:,:,m)); 
    end
    [~, L] = min(Rf,[],3);  
end

return

