Monday, 4 August 2008

Canny Edge Detection

As part of my entry to the CUDA challenge I implemented a Gaussian Point Spread function and a Sobel filter.

I'm not sure how my project went - badly I suppose - as I had only obtained a 8800 GT device a week before deadline :( I'll post the full project here once judging has finished. Actually I will probably keep working on improving it as I beleive there is a lot of scope for a fully implemented version.

Back to this post... As I had already implemented most of the parts of a Canny Edge detection algorithm I thought I may as well go the whole hog and implement it.  Traditionally it involves a recursive function to trace the edges at the defined angles - not possible on a cuda device. Im not entirely sure I have it correct yet and have not compared to a traditionally implemented Canny yet.

Have a look at the (scaled down) images below and see what you think. In order:  original Image, Grayscaled with Gaussian 3x3 PSF applied,  Sobel Filter and finally Canny edge detection.

[caption id="attachment_70" align="alignleft" width="448" caption="Buttercup in Glen Coe"]Buttercup in Glen Coe[/caption]



[caption id="attachment_72" align="alignleft" width="448" caption="3x3 Gaussian PSF applied"]3x3 Gaussian PSF applied[/caption]

[caption id="attachment_73" align="alignleft" width="448" caption="Sobel filter applied to the Image"]Sobel filter applied to the Image[/caption]

[caption id="attachment_71" align="alignleft" width="448" caption="First attempt at non-recursive Canny Edge Detection"]First attempt at non-recursive Canny Edge Detection[/caption]


  1. Dear Sir/Madam,
    I am a university student studying Mechanical Enginnering in the Chinese University of Hong Kong. Recently I am doing a project concerning Canny Edge Detection. Can you give me more details about the programme and how you make it? The information you give me will be of great help to my final year project.
    Thank you very much for your help.

  2. Thanks for your comment :)

    I'll make a proper post later today or tomorrow detailing the procedure in more detail.
    Some of my methods for this are NOT optimized for GPU's though. In fact even my gaussian is not optimal. I recently had an interesting conversation with Eric Young from NVidia about optimizing a Gaussian Point Spread Function.
    Anyhow - post coming soon.

  3. will u plz send me source code for canny edge detection in VB ..............
    it willl b very usefull for me to do a project...........

  4. Hi Balaji,

    I no longer code in VB but you should have enough information in this post: and this one: to implement one yourself. As its a project you will probably learn a lot doing it yourself.

    VB might not be the best language to do this in though...


  5. hey what do you mean when you say that we will make it more sensitive to different angles by not resolving it in 4 directions

  6. Hi Akash,

    I think you are referring to this post:

    Basically we round the angle produced (theta) to the nearest 45 degrees - giving the vertical, horizontal and two diagonals. In the small kernel described it works well with just these 8 angles. If we are only looking for vertical or horizontal edges we would round just to these 90 degree steps.

    Likewise if we were looking for edges orientated at x degrees in an image we can adapt our rounding to be more sensitive to angles within this range. Although we would very likely need a wider kernel in order to get the precision we need to do this.


  7. I really hope to see your code, I'm studying CUDA and I'm working on computer vision.

  8. Hi Nguyen,

    Have a look at the Gaussian convolution code available at

    It gives a fairly optimal solution of the first step in the process.


  9. Can someone tell me how to apply canny edge detector in x and y-axis separately on an image. i need to find edges in the x-direction and y-direction separately as two edge images. plz guide.