a b/functions/functions_Kovesi/gaussfilt.m
1
% GAUSSFILT -  Small wrapper function for convenient Gaussian filtering
2
%
3
% Usage:  smim = gaussfilt(im, sigma)
4
%
5
% Arguments:   im - Image to be smoothed.
6
%           sigma - Standard deviation of Gaussian filter.
7
%
8
% Returns:   smim - Smoothed image.
9
%
10
% If called with sigma = 0 the function immediately returns with im assigned
11
% to smim
12
%
13
% See also:  INTEGGAUSSFILT
14
15
% Peter Kovesi
16
% Centre for Explortion Targeting
17
% The University of Western Australia
18
% http://www.csse.uwa.edu.au/~pk/research/matlabfns/
19
20
% March 2010
21
% June  2013  - Provision for multi-channel images
22
23
function smim = gaussfilt(im, sigma)
24
 
25
    if sigma < eps
26
        smim = im;
27
        return;
28
    end
29
    
30
    % If needed convert im to double
31
    if ~strcmp(class(im),'double')
32
        im = double(im);  
33
    end
34
    
35
    sze = max(ceil(6*sigma), 1);
36
    if ~mod(sze,2)    % Ensure filter size is odd
37
        sze = sze+1;
38
    end
39
    
40
    h = fspecial('gaussian', [sze sze], sigma);
41
42
    % Apply filter to all image channels
43
    smim = zeros(size(im));
44
    for n = 1:size(im,3)
45
        smim(:,:,n) = filter2(h, im(:,:,n));
46
    end
47