contrib/oxl/osl/osl_canny_gradient.cxx
Go to the documentation of this file.
00001 // This is oxl/osl/osl_canny_gradient.cxx
00002 #ifdef VCL_NEEDS_PRAGMA_INTERFACE
00003 #pragma implementation
00004 #endif
00005 //:
00006 // \file
00007 // \author fsm
00008 
00009 #include "osl_canny_gradient.h"
00010 #include <vcl_cmath.h>
00011 
00012 // smooth_  is the (smoothed, presumably) input image.
00013 // dx_, dy_ are the output x, y gradient images.
00014 // grad_    is the output
00015 
00016 // FIXME: why does it do dx[x][y] = smooth[x+1][y] - smooth[x-1][y]
00017 //                   but dy[x][y] = smooth[x][y-1] - smooth[x][y+1] ?
00018 
00019 void osl_canny_gradient(int xsize_, int ysize_,
00020                         float const * const * smooth_,
00021                         float * const * dx_,
00022                         float * const * dy_,
00023                         float * const * grad_)
00024 {
00025   //  Topline
00026   dy_[0][0]=(smooth_[0][0]-smooth_[0][1])*2;   // left edge
00027   dx_[0][0]=(smooth_[1][0]-smooth_[0][0])*2;
00028   for (int x=1; x<xsize_-1; ++x) {
00029     dy_[x][0]=(smooth_[x  ][0]-smooth_[x  ][1])*2;
00030     dx_[x][0]= smooth_[x+1][0]-smooth_[x-1][0];
00031   }   // end for x
00032   dy_[xsize_-1][0]=(smooth_[xsize_-1][0]-smooth_[xsize_-1][1])*2;   // right edge
00033   dx_[xsize_-1][0]=(smooth_[xsize_-1][0]-smooth_[xsize_-2][0])*2;
00034 
00035 
00036   // Middle lines
00037   for (int y=1; y<ysize_-1; ++y) {
00038     dy_[0][y]= smooth_[0][y-1]-smooth_[0][y+1];   // left edge
00039     dx_[0][y]=(smooth_[1][y  ]-smooth_[0][y  ])*2;
00040     for (int x=1; x<xsize_-1; ++x) {
00041       dy_[x][y]=smooth_[x  ][y-1]-smooth_[x  ][y+1];
00042       dx_[x][y]=smooth_[x+1][y  ]-smooth_[x-1][y  ];
00043     }   // end for x
00044     dy_[xsize_-1][y]= smooth_[xsize_-1][y-1]-smooth_[xsize_-1][y+1];   // right edge
00045     dx_[xsize_-1][y]=(smooth_[xsize_-1][y  ]-smooth_[xsize_-2][y  ])*2;
00046   }   // end for y
00047 
00048 
00049   // Bottom line
00050   dy_[0][ysize_-1]=(smooth_[0][ysize_-2]-smooth_[0][ysize_-1])*2;   // left edge
00051   dx_[0][ysize_-1]=(smooth_[1][ysize_-1]-smooth_[0][ysize_-1])*2;
00052   for (int x=1; x<xsize_-1; ++x) {
00053     dy_[x][ysize_-1]=(smooth_[x  ][ysize_-2]-smooth_[x  ][ysize_-1])*2;
00054     dx_[x][ysize_-1]= smooth_[x+1][ysize_-1]-smooth_[x-1][ysize_-1];
00055   }   // end for x
00056   dy_[xsize_-1][ysize_-1]=(smooth_[xsize_-1][ysize_-2]-smooth_[xsize_-1][ysize_-1])*2;   // right edge
00057   dx_[xsize_-1][ysize_-1]=(smooth_[xsize_-1][ysize_-1]-smooth_[xsize_-2][ysize_-1])*2;
00058 
00059 
00060   //  Magnitude for entire image
00061   for (int y=0; y<ysize_; ++y)
00062     for (int x=0; x<xsize_; ++x)
00063       grad_[x][y] = (float)vcl_sqrt(dx_[x][y]*dx_[x][y] + dy_[x][y]*dy_[x][y]);
00064 }
00065 
00066 // taken from osl_canny_rothwell.cxx
00067 void osl_canny_gradient_central(int xsize_, int ysize_,
00068                                 float const * const * smooth_,
00069                                 float * const * dx_,
00070                                 float * const * dy_,
00071                                 float * const * grad_)
00072 {
00073   for (int x=1; x<xsize_-1; ++x) {
00074     for (int y=1; y<ysize_-1; ++y) {
00075       dx_[x][y] = smooth_[x+1][y] - smooth_[x-1][y];
00076       dy_[x][y] = smooth_[x][y+1] - smooth_[x][y-1];
00077       grad_[x][y] = (float)vcl_sqrt(dx_[x][y]*dx_[x][y] + dy_[x][y]*dy_[x][y]);
00078     }
00079   }
00080 }