No BSD License  

Highlights from
GeoML

image thumbnail
from GeoML by Gabriele Lombardi
A general morphable template tool for image segmentation.

gaussianFilter(varargin)
function imgo = gaussianFilter(varargin)
%GAUSSIANFILTER  Filters an image using the gaussian derivative filter
%
%
% function imgo = gaussianFilter(img, sigma, sigmaMultiplier, mean)
%
%  This function computes the gaussian filter on an image using
% the convolution of the sampled gaussian function.
%
%  Params
%  ------
% IN:
%  img              = The image to be filtered
%  sigma            = The sigma value (def=1)
%  sigmaMultiplier  = Used to compute the filter size (def=4)
%  mean             = The gaussian mean (def=0)
% OUT:
%  imgo             = The filtered image
%
%  Pre
%  ---
% - The sigma value must be positive
%
%  Post
%  ----
% - The returned image is a filtered copy of the input image of the same
%  size.
%
%  SeeAlso
%  -------
% gaussianDerivative
%
%  Examples
%  --------
%   Blurring an image using gaussian:
% >> imshow(gaussianFilter(img));

% Parsing of params
[img, sigma, sigmaMultiplier, mean] = ParseParams(varargin{:});

% Dimension can be computed using the multiplier:
dim = round(sigmaMultiplier*sigma);

% Compute the filter set
blur = gauss(-dim:dim,sigma);

% Operate on a single colorplane at a time
imgo = zeros(size(img));
for plane=1:size(img,3)
    imgo(:,:,plane) = FilterPlane(img(:,:,plane),blur);
end

% ------------------------- FUNCTIONS -------------------------

% Filter a single plane
function imgo = FilterPlane(img,blur)

% Filter horizzontally
imgo = filter2(blur,img);

% Filter vertically
imgo = filter2(blur',imgo);

% -----------------------------------------------------------------

function [img, sigma, sigmaMultiplier, mean] = ParseParams(varargin)

% Minimum number of parameters is 1 (the image)
if nargin<1 error('Too few parameters'); end
if nargin>4 error('Too many parameters'); end

% Set up variables
varnames = {'img', 'sigma', 'sigmaMultiplier', 'mean'};
for ind=1:nargin
    eval([varnames{ind} ' = varargin{ind} ;']);
end

% Default parameters
if nargin<2 sigma=1; end
if nargin<3 sigmaMultiplier=4; end
if nargin<4 mean=0; end

% Check the image given to convert it in grayscale
if size(img,3)>1
    img = rgb2gray(img);
end

% Check image parameter
if not(isa(img,'double'))
    img = double(img)/255;
end

Contact us at files@mathworks.com