Grayscale Morphological Dilation



 
 
 

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

 

#include "VsipTypes.h"

/****************************************************************************
 *
 *  Function    : VsipGrayscaleMorphologicalDilation
 *
 *  Description : Computes the Gray Scale morphological dilation
 *                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

   $VsipGrayscaleMorphologicalDilation = 'VsipGrayscaleMorphologicalDilationByte',
         $IOType = 'uint8', $Per = 'AuxPerimeterByte',    $Min = '0', $Cast = '(uint2)';
   $VsipGrayscaleMorphologicalDilation = 'VsipGrayscaleMorphologicalDilationInt',
         $IOType = 'int32', $Per = 'AuxPerimeterInt',     $Min = '0', $Cast = '(uint16)';
   $VsipGrayscaleMorphologicalDilation = 'VsipGrayscaleMorphologicalDilationFloat',
         $IOType = 'float', $Per = 'AuxPerimeterFloat',   $Min = '-2147483648',
         $Cast = '';
   $VsipGrayscaleMorphologicalDilation = 'VsipGrayscaleMorphologicalDilationDouble',
         $IOType = 'double', $Per = 'AuxPerimeterDouble', $Min = '-1e37',
         $Cast = '';

$in

$IOType[:,:] $VsipGrayscaleMorphologicalDilation($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, $Min));
 

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

           VsipIndexesType r, _ = extents(maxvals);

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

} return (result);

$end_generic