Grayscale Morphological Erosion



 
 
 

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

 

#include "VsipTypes.h"

/****************************************************************************
 *
 *  Function    : VsipGrayscaleMorphologicalErosion
 *
 *  Description : Computes the Gray Scale morphological erosion
 *                The kernel (nxn) size must be odd and greater than one
 *                The difference between the output (dest) size and
 *                input (src) size must be 0 or -(n - 1)
 *
 *  Parameters  : src - input image object
 *                kernel - nxn kernel object
 *                rsz - row size of the output image
 *                csz - column size of the output image
 *
 *  Returns     : output image
 *
 ****************************************************************************/
$generic

   $VsipGrayscaleMorphologicalErosion = 'VsipGrayscaleMorphologicalErosionByte',
         $IOType = 'uint8', $Per = 'AuxPerimeterByte',    $Max = '1', $Cast = '(int2)';
   $VsipGrayscaleMorphologicalErosion = 'VsipGrayscaleMorphologicalErosionInt',
         $IOType = 'int32', $Per = 'AuxPerimeterInt',     $Max = '255', $Cast = '(int16)';
   $VsipGrayscaleMorphologicalErosion = 'VsipGrayscaleMorphologicalErosionFloat',
         $IOType = 'float', $Per = 'AuxPerimeterFloat',   $Max = '2147483647',
         $Cast = '';
   $VsipGrayscaleMorphologicalErosion = 'VsipGrayscaleMorphologicalErosionDouble',
         $IOType = 'double', $Per = 'AuxPerimeterDouble', $Max = '1e37',
         $Cast = '';
 

$in

$IOType[:,:] $VsipGrayscaleMorphologicalErosion($IOType src[:,:],
                  $IOType kernel[:,:], VsipIndexesType rsz, VsipIndexesType csz)
{

  // ***** recover the src and kernel sizes *****
  VsipIndexesType rs, VsipIndexesType cs = extents(src);
  VsipIndexesType rk, VsipIndexesType ck  = extents(kernel);

  // ***** test the image, kernel and dest sizes (see header) *****
  assert(((rs > 0) && (cs > 0)),
         "ERROR: Image can not have zero dimension. (",rs,"x",cs,")\n");
  assert((rk == ck),
         "ERROR: Kernel must be square matrix. (",rk,"x",ck,").\n");
  assert(((rk > 1) && ((rk % 2) != 0)),
         "ERROR: N must be odd and greater than 1 (",rk,").\n");
  assert((((rsz == rs) && (csz == cs)) ||
          ((rsz == rs - (rk - 1)) && (csz == cs - (ck - 1)))),
         "ERROR: Invalid destination size (",rsz,",",csz,").\n");
 

  // ***** reflects the kernel over the origin *****
  $IOType invkernel[:,:] =
        for elem in kernel at (VsipIndexesType i, VsipIndexesType j)
        return(array(kernel[rk - 1 - i, ck - 1 - j]));

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

  // ***** computes the erosion for the modified src *****
  $IOType result[:,:] =
       forwindow win[rk,ck] in persrc
            { $IOType minvals[:,1] =
                for elem1 in win dot elem2 in invkernel
                        { bool b = (elem2 == 0 ? 0:1);
                        }
                return(vals_at_mins($Cast elem1-elem2,{elem1}, b));

           VsipIndexesType r, _ = extents(minvals);

              $IOType val = minvals[r-1,0];
             }
        return(array(val));

} return (result);

$end_generic