Code covered by the BSD License  

Highlights from
Canny edge detection in 2-D and 3-D


4.8 | 5 ratings Rate this file 227 Downloads (last 30 days) File Size: 18.2 KB File ID: #45459
image thumbnail

Canny edge detection in 2-D and 3-D



10 Feb 2014 (Updated )

A Canny edge detector for two-dimensional image and three-dimensional volume data.

| Watch this File

File Information

This is an implementation of the Canny edge detector, extended to operate on 3-D as well as 2-D data. The function features:
* 3-D capability, using the natural extension of the standard 2-D algorithm.

* A sub-pixel location estimation option.

* Symmetric differences for gradient estimation, so edge positions do not suffer from a systematic bias, with the option to use nearest-neighbour differences.

* Anisotropic smoothing if required, for example for volumes where the resolution differs between axes.

* A flexible range of thresholding options, including simple defaults and precise distribution-based selection. Different images can be treated consistently using absolute thresholds.

* Options for fine-tuning non-maximum suppression.

* Efficient use of the separability of the Gaussian for filtering.

* Independent functions for the different stages of the processing pipeline. This simplifies development of new algorithms; some of the functions (e.g. smoothing) are also useful in other contexts.

All the functions in the submission should be downloaded and placed on the MATLAB path in order to use the CANNY function.

Required Products Image Processing Toolbox
MATLAB release MATLAB 8.2 (R2013b)
Tags for This File   Please login to tag files.
Please login to add a comment or rating.
Comments and Ratings (20)
19 Nov 2014 David Young

Hi Murari, please see my response to Önder below.

19 Nov 2014 Murari Mandal

Hi David, i am using matlab13a and getting this error-

No appropriate method, property, or field addParameter for class inputParser.

Error in canny (line 207)
inp.addParameter('Region', 'same');

19 Nov 2014 Murari Mandal

Hi David, i am using matlab13a and getting this error-

No appropriate method, property, or field addParameter for class inputParser.

Error in canny (line 207)
inp.addParameter('Region', 'same');

23 Sep 2014 David Young

Hi Siddharth, It looks as if checkattributes is not on your path. Please make sure that you downloaded the complete submission and all the files are in a folder on your MATLAB path. It's best to download the zip file and unpack it rather than downloading individual functions.

22 Sep 2014 Siddharth


I got an error while using this program. Do you know what am i doing wrong?

Error using gsmoothn>checkinputs (line 128)
The value of 'Region' is invalid. Undefined function 'checkattributes' for input
arguments of type 'cell'.


09 Sep 2014 David Young

Hi tong luo, This looks like a version problem also. Please try replacing this line in the definition of checkthresh:

{'nonnan' 'real' 'finite' 'nonnegative' 'nondecreasing'}) && ...

with this:

{}) && ...

This removes some checking, but that probably will not matter, and I hope will avoid the warning.

I am concerned, though, that the warning does not account for the result you observe. Please let me know if that continues to be a problem.

09 Sep 2014 tong luo

Hi David,

I use matlab 2011b, and replace inp.addParameter, but when use your demo code for 2d pout.tif, the result image e is all 0 value, the reported warn is below, thank you in advance for the debug :

Warning: Failed to find attribute in list.
> In checkattributes at 11
In canny>@(t)checkattributes(t,{'numeric'},{'nonnan','real','finite','nonnegative','nondecreasing'})&&(isempty(t)||isscalar(t)||isequal(size(t),[1,2])) at 202
In canny at 232

09 Jul 2014 David Young

Hi Önder, I think the only change you need to make is to replace inp.addParameter with inp.addParamValue wherever it occurs. If there are other problems, please let me know what goes wrong.

09 Jul 2014 Önder

Hi David,

im a pretty new Matlab user.
Is there a possibilty to use this code with Matlab 7 R2010b ? Because I cant update to a higher version
best wishes

08 Jul 2014 Daniel Leib

Thanks David - I didn't even realize Peter's existed on the system I was using. That explains the confusion!

Yours worked just fine in 3D after converting to double.

07 Jul 2014 David Young

Daniel: Looks like you are using Peter Kovesi's Canny function, not mine. Best check what is on your path. Incidentally, mine won't work with uint16 inputs - it only accepts double or single arrays.

07 Jul 2014 Daniel Leib

Hi David,

Thanks for the reply. Here's the complete error that it's kicking out; the input array img is 1024x1024x421 of int16 type (no other options specified):

[gradientD1 orD1] = canny(img);

Undefined function 'conv2' for input arguments of type 'double' and attributes 'full 3d real'.

Error in derivative5 (line 92)
varargout{n} = conv2(p, d1, im, 'same');

Error in canny (line 54)
[Ix, Iy] = derivative5(im,'x','y'); % Get derivatives.

Error in HausdorffThicknessSratch (line 24)
[gradientD1 orD1] = canny(img);

04 Jul 2014 David Young

Daniel Leib: Please can you give details of the error, including the calling code and the full error message. The function is already designed to work on 3-D volumes, so it won't be as simple as swapping convn for conv2.

02 Jul 2014 Daniel Leib

Hi David,

I gave your code a try on a 2D image and it worked wonderfully, but if I try to run it on a 3D grayscale volume representing CT data it fails using conv2. What alterations would be appropriate to swap those calls out for convn?


02 Jul 2014 Christian

works well, thank you!
@nan: I'm using R2012b and it worked for me! thanks!

26 May 2014 nan zhao

@David, for an old version of MATLAB, replace addParameter by addParamValue. Then it should work.

21 May 2014 David Young

David, your problem is most likely that you are using an old version of MATLAB. If you update to a recent version, you will find that inputParser is on your path. I prepared the submission using R2013b.

21 May 2014 David

I apologize I am new to Matlab. I get an error regarding the input parser.

No appropriate method, property, or field addParameter for class inputParser.

Any help would be greatly appreciated.

12 May 2014 Chada Keatmanee

It is working perfectly

25 Apr 2014 Mattias

Great work!

13 Feb 2014

Simplified the code. Faster and more flexible nonmaximum suppression. Change to the ordering of elements of the vector of smoothing constants in the 2-D case, for greater consistency.

05 Mar 2014

Sub-pixel estimation option; nearest-neighbour differencing option.

Contact us