View License

Download apps, toolboxes, and other File Exchange content using Add-On Explorer in MATLAB.

» Watch video

Highlights from
Gradients with Gaussian smoothing

5.0 | 3 ratings Rate this file 9 Downloads (last 30 days) File Size: 21.5 KB File ID: #34089 Version: 1.6

Gradients with Gaussian smoothing


David Young (view profile)


06 Dec 2011 (Updated )

Grey-level gradients are estimated using Gaussian smoothing followed by symmetric differencing.

| Watch this File

File Information

These functions carry out gradient estimation using Gaussian smoothing and symmetric differencing. They can be used to support, for example, the Canny edge detector, and may form the initial stage of many image and data processing operations.
The gradient functions accept different kinds of data:
  gradients_x: a vector

  gradients_xy: a 2-D array, typically an image. The two components of the gradient are returned.

  gradients_xyt: two 2-D arrays, each typically an image. Spatial and temporal gradients are returned.

  gradients_n: an N-D array. The gradient along each axis is returned.

The supporting smoothing functions carry out Gaussian smoothing, taking as inputs:

  gsmooth: a vector

  gsmooth2: a 2-D array

  gsmoothn: an N-D array

The functions offer anisotropic smoothing if required. (That is, a different smoothing constant for each axis.)

Particular care is taken of how elements close to the array boundaries are treated. By default, the output arrays are smaller than the input arrays so that only valid values need be computed. However, an option allows the outputs to be the same size as the inputs; values close to the boundaries are then computed by extrapolation of the image using reflection or tiling.

The convenience function exindex simplifies the code for extrapolation.


This file inspired Affine Optic Flow.

Required Products Image Processing Toolbox
MATLAB release MATLAB 7.13 (R2011b)
MATLAB Search Path
Tags for This File   Please login to tag files.
Please login to add a comment or rating.
Comments and Ratings (6)
16 Jan 2015 David Young

David Young (view profile)

Thanks Jess!

Comment only
15 Jan 2015 Jess

Jess (view profile)

I can't seem to undo my previous 1-star rating, but I'll give this a 5-star rating now! It is excellent! Thanks for posting and for fixing the file. My hat's off to David Young.

04 Nov 2014 David Young

David Young (view profile)

Thank you Jess. I had left checkattributes() out of the zip file. Now updated.

Comment only
04 Nov 2014 Jess

Jess (view profile)

I'll definitely boost my rating if you fix the bugs and let me know! Thanks for posting!

Comment only
04 Nov 2014 Jess

Jess (view profile)

This looks promising, but does not seem to work for 1D. I give it a simple vector, and it gives errors:

>> x=rand(1,20);
>> y = sort(x);
>> gradients_x(y);
Error using gsmoothn>checkinputs (line 128)
The value of 'Region' is invalid. Undefined function 'checkattributes' for input
arguments of type 'cell'.

Error in gsmoothn (line 70)
[sigmas, bcons, reg, convreg, ronly, d] = ...

Error in gradients_n (line 116)
asmth = gsmoothn(a, sigmas, 'Region', regdiff, 'Wrap', wraps);

Error in gradients_x (line 93)
[xg, region] = gradients_n(x, varargin{:});

01 Apr 2014 Akshay Gore

Akshay Gore (view profile)

07 Dec 2011 1.1

Updated to include user-friendly same size option and change to the argument order.

09 Dec 2011 1.3

Fixed bug that caused an error when the default region was used.

10 Feb 2014 1.4

Extended to cover N-D and 1-D data.

20 Mar 2014 1.5

Nearest-neighbour differencing added as an option. Some changes to argument sequences - region and wrapping now given as name-value parameters.

04 Nov 2014 1.6

Included checkattributes() in the submission (previously omitted in error).

Contact us