Page 88 -
P. 88

62    Chapter 2 ■ Edge-Detection Techniques


                           2.9    Source Code for the Canny Edge Detector


                             /* Canny edge detection */
                             #include “stdio.h“
                             #include “cv.h“
                             #include “highgui.h“

                             /* Scale floating point magnitudes and angles to 8 bits */
                             #define ORI_SCALE 40.0
                             #define MAG_SCALE 20.0
                             #define PI 3.1415926535

                             /* Biggest possible filter mask */
                             #define MAX_MASK_SIZE 20

                             /* Fraction of pixels that should be above the HIGH threshold */
                             float ratio = 0.1f;
                             int WIDTH = 0;

                             int range (IMAGE x, int i, int j)
                             {
                                    if ( (i>=0) && (i<x->info->nr) && (j>=0) && (j<x->info->nc) )
                                          return 1;
                                    else return 0;
                             }

                             float norm (float x, float y)
                             {
                                    return (float) sqrt ( (double)(x*x + y*y) );
                             }

                             void canny (float s, IMAGE im, IMAGE mag, IMAGE ori)
                             {
                                    int width;
                                    float **smx,**smy;
                                    float **dx,**dy;
                                    int i,j,n;
                                    float gau[MAX_MASK_SIZE], dgau[MAX_MASK_SIZE], z;

                             /* Create a Gaussian and a derivative of Gaussian filter mask */
                                    for(i=0; i<MAX_MASK_SIZE; i++)
                                    {
                                      gau[i] = meanGauss ((float)i, s);
                                      if (gau[i] < 0.005)
                                      {
                                          width = i;
                                          break;
                                      }
   83   84   85   86   87   88   89   90   91   92   93