Sobel Gradient Edge Detection



 
 

Color Convention
 Blue  Comments 
 Red  Generic Terms
 Yelow  Reserved  words and Commands 
 Brown  Defines
  Link  Link to other functions

 

#include "VsipTypes.h"

/****************************************************************************
 *
 *  Function    : VsipSobelGradienteEdgeDetection
 *
 *  Description : Computes the Sobel Gradient of the edges
 *                The difference between the output (dest) size and
 *                input (src) size must be 0 or +/- 2
 *
 *  Parameters  : src - input image object
 *                rsz - row size of the output image
 *                csz - column size of the output image
 *
 *  Returns     : Sobel gradient image
 *
 *  Note        : The byte version gives overflow
 *
 ****************************************************************************/
$generic

   $VsipSobelGradienteEdgeDetection = 'VsipSobelGradienteEdgeDetectionBit',
        $InType = 'uint1',  $OutType = 'uint8', $CompType = 'int12',
        $SqrtFunc = 'sq_root32', $Per = 'AuxPerimeterBit';
   $VsipSobelGradienteEdgeDetection = 'VsipSobelGradienteEdgeDetectionByte',
        $InType = 'uint8',  $OutType = 'uint8', $CompType = 'int25',
        $SqrtFunc = 'sq_root32', $Per = 'AuxPerimeterByte';
   $VsipSobelGradienteEdgeDetection = 'VsipSobelGradienteEdgeDetectionInt',
        $InType = 'int32',  $OutType = 'int32', $CompType = 'int32',
        $SqrtFunc = 'sq_root32', $Per = 'AuxPerimeterInt';
   $VsipSobelGradienteEdgeDetection = 'VsipSobelGradienteEdgeDetectionFloat',
        $InType = 'float',  $OutType = 'float', $CompType = 'float',
        $SqrtFunc = 'sqrt', $Per = 'AuxPerimeterFloat';

$in

$OutType[:,:] $VsipSobelGradienteEdgeDetection($InType src[:,:],
                                            VsipIndexesType rsz,
                                            VsipIndexesType csz)
{

  // ***** recover the src size *****
  VsipIndexesType rs, VsipIndexesType cs = extents(src);

  // ***** test the size of the image and dest (see header)  *****
  assert(((rs > 2) && (cs > 2)),
         "ERROR: Image must be at least 3x3. (",rs,"x",cs,")\n");
  assert((((rsz == rs) && (csz == cs)) ||
          ((rsz == rs - 2) && (csz == cs - 2)) ||
          ((rsz == rs + 2) && (csz == cs + 2))),
         "ERROR: Invalid destination size (",rsz,",",csz,").\n");
 

  // ***** defines the Sobel's masks *****
  int3 vertmask[3,3] = {{-1,0,1}, {-2,0,2}, {-1,0,1}};
  int3 horzmask[3,3] = {{1,2,1}, {0,0,0}, {-1,-2,-1}};

  // ***** creates the necessary perimeter around src *****
  $OutType persrc[:,:] =
       if (rsz == rs - 2)
         return(src)
       elif (rsz == rs)
         return($Per(src, 1, 1, 1, 1, 0))
       else
         return($Per(src, 2, 2, 2, 2, 0));
 

  // ***** computes the gradient for the modified src  *****
  $OutType result[:,:] =
       forwindow win[3,3] in persrc
            { // ***** computes the edge for horizontal and vertical masks  *****
              // ***** and returns the sqr root of the sum of their squares *****
              $CompType vert =
                for elem1 in win dot elem2 in vertmask
                return(sum(($CompType)elem1*elem2));

              $CompType horz =
                for elem3 in win dot elem4 in horzmask
                return(sum(($CompType)elem3*elem4));
            }
       return(array($SqrtFunc(vert*vert+horz*horz)));

} return (result);

$end_generic