%   Distributed as part of the "Co-occurrence Filter" paper:
%   It is an academic code, distributed under the GNU GPL license:
%   http://www.gnu.org/copyleft/gpl.html
%  
%  
%   Version: 1.0
%   Date: March 27th, 2017
%   Roy Josef Jevnisek: Roy.J.Jevnisek@gmail.com


% Processing Parameters:
nBins   = 32;   % number of bins (for the quantization)
hws     = 7;    % filter - half window size

% Read Data:
im.rgb = imread('Field.png'); 
im.lab = rgb2lab(im.rgb);
im.idx = quantize(im.rgb, nBins);
im.sz  = size(im.rgb);

% Collect Co-occurrence Statistics:
pab = collectPab(im.idx, ones(im.sz(1:2)), nBins);
pmi = pab.^(1/2) ./ ( sum(pab(:)).' * sum(pab(:)) + eps );

% Smooth:  
f = fspecial('gaussian', 2 * hws, 2 * sqrt(hws) + 1);
res = lab2rgb(coFilter(im.lab, im.idx, pmi, f ) );

% Visualize: 
figure(1); imshow(im.rgb); title('input image'); 
figure(2); imshow(res); title('CoF smoothed image');