MeVisLabToolboxReference
ml::KernelBaseModule Class Reference

The module base class to apply kernels to an image. More...

#include <mlKernelBaseModule.h>

Inheritance diagram for ml::KernelBaseModule:
ml::Module ml::FieldContainer ml::Base ml::KernelBaseOp ml::KernelModule ml::BitMorphologyFilter ml::CloseGapFilter ml::CompassFilter ml::ConvolutionFilter ml::Correlation ml::ExtConvolutionFilter ml::KernelCurvatureEstimationFilter ml::KernelOp ml::LocalMaximaFilter ml::LoGFilter ml::MorphologyFilter ml::RankFilter ml::RobertsFilter ml::SigmaFilter ml::Sobel3DFilter ml::StdDeviationFilter ml::SurroundFilter ml::ZeroCrossingsFilter

List of all members.

Public Member Functions

Access to kernel.
KernelgetKernel ()
const KernelgetConstKernel () const
 Method to access the kernel as constant object. See also getKernel().

Protected Member Functions

 KernelBaseModule (int inputNum=1, int outputNum=1)
 Constructor.
virtual void _setCorrectCorrelationMinMax (bool imageIntervalUsed=false, MLdouble intervalMin=0, MLdouble intervalMax=0, MLint inIdx=0, MLint outIdx=0)
 This method calculates and sets the possible min/max values for output image outIdx if a correlation or convolution filtering is assumed on input inIdx.
virtual ImageVector _getCalculatedReferenceInputExt () const
 Return the reference extent of input image dependent on the current value of _referenceExtentFld.
virtual void _calcKernelPage (SubImage *outSubImg, int outIndex, SubImage *inSubImgs, int numInSubImgs, MLsoffset *indexTab, size_t indexTabSize, MLsoffset srcVoxelOffset, size_t loopIdx, size_t iteration, const ImageVector &ov1, const ImageVector &ov2, const ImageVector &pOf, size_t numVox)=0
 This method is not intended to be overloaded directly, because it is subject to change.
Methods overloaded from \c Module.
virtual void calculateOutputImageProperties (int outIndex)
 Computes the output image properties for output outIndex.
virtual SubImageBox calculateInputSubImageBox (int inIndex, const SubImageBox &outSubImgBox, int outIndex)
 Calculates the region of the input image needed for the calculation of the output image region defined by outSubImg and output index outIndex.
virtual void calcInSubImagePreparation (int outIndex, SubImage *inSubImgs)
 Does some preprocessing on input subimages if we have more than one.
virtual void calcOutSubImagePostProcessing (SubImage *outSubImg, int outIndex, SubImage *inSubImgs)
 Called to postProcess the output image outSubImg with the index outIndex from the input subimage inSubImgs.
virtual void _connectToAllOutputs (Field *field)
 Helper function to connect a field field to all output fields.

Friends

class KernelBaseOpCalculateOutputImageHandler

Standard control fields for all kernel modules.

enum  InputImageRefExtent {
  Overlap = 0, Input0_ExtWithoutFill, Input0_ExtWithFill, MaxExtentsWithoutFill,
  MaxExtentsWithFill, NumImageRefExtents
}
 Enumerator specifying the calculation mode for the extent of the reference extent of the input image. More...
EnumFieldgetBorderHandlingFld () const
 The field to handle the mode how kernel operations work on image borders.
DoubleFieldgetFillValueFld () const
 The field value is value written in input or output image borders where a border handling requires filling with a fill value.
EnumFieldgetReferenceExtentModeFld () const
 Returns how the reference extent for input images is calculated; valid values are InputImageRefExtent (except of NumImageRefExtents) and default is Overlap.

Detailed Description

The module base class to apply kernels to an image.

Fields for border handling and fill value are automatically created. Macros are available for simple usage and implementation of the templated kernel algorithm, see mlKernelMacros.h, KernelModule, KernelTools and KernelExample for more information and examples.

This class also creates a border handling field and a fill value field by default and connects image input 0 with the output image 0.

Also have a look to the more elaborated class KernelModule which implements much convenience functionality. It is typically used to derive from.

In the following paragraphs you find documentation which describes how to implement a kernel operation.

Since the implementation has to be done for all data types the filtering code needs to be implemented as template and a dispatcher is needed which calls the correct templated versions of this method.

The template is typically implemented in a template function called calcRow().

The dispatcher should be implemented by using macros from the file mlKernelMacros.h. For the header file typically CALC_ROW_H() is used and for implementations typically CALC_ROW_CPP(). The default implementation is pure virtual, i.e. the KernelBaseModule cannot be instantiated.

Note that there are also a more elaborated versions of CALC_ROW_H() and CALC_ROW_CPP() which provide more parameters, CALC_ROW_H_EXT() and CALC_ROW_CPP_EXT().

They pass more parameters to the template function which therefore requires the implementation of the template function calcRow() with more parameters.

Some other macros, CALC_ROW_CPP_ANY(CLASS_NAME, KERNEL_SUPER_CLASS, SWITCH_SELECTION), CALC_ROW_CPP_ANY_SUBIMG(CLASS_NAME, KERNEL_SUPER_CLASS, SWITCH_SELECTION), CALC_ROW_CPP_SUBIMG(CLASS_NAME, KERNEL_SUPER_CLASS), and CALC_ROW_CPP_SUBIMG_EXT(CLASS_NAME, KERNEL_SUPER_CLASS) provide additional functionality to compile kernel filter for a subset of data types or with the subimages in which the voxel rows are filtered. They require additional parameters and/or other signatures of the template function calcRow().

Please use only the macros for dispatching this call to the template function of a derived class since calcRow() parameters are subject to change.

A derived class typically would implement a kernel algorithm as follows:

CALC_ROW_H() implements the header code for the dispatcher to call the right template function from. Use CALC_ROW_CPP() in the implementation file to implement the standard stuff there.

outCursor is a pointer to the output image voxel in which needs to be written the result value of the kernel operation.

inCursor is the pointer to the input image voxel covered by the origin of the kernel. Note that the kernel does not necessarily have an entry at that position. Anyway, it represents a pointer to the input image voxel under the kernel element (0,0,0,0,0,0).

       // Moves write cursor forward.
       ++outCursor;

       // Moves forward the kernel origin pointer.
       ++inCursor;

numVox is the number of voxels which needs to be filtered by calcRow(). Loop numVox times to filter the row and move the cursor of the input and the output subimage in that loop

srcVoxelOffset is the offset from the current cursor of the input subimage to get access to that voxel which corresponds to the voxel position written to the output image. So

outCursor = inCursor[srcVoxOffset]

copies the input image to the output image without shifting. srcVoxelOffst can be considered as an offset to the kernel center. Note that the kernel does NOT necessarily have a valid entry there.

indexTabSize contains the number of elements in the kernel and is identical to getKernel().getTabSize(). The number of elements in getKernel().getCoordTab() and getKernel().getValueTab() are also identical.

indexTab contains a table of offsets from the current cursor of the input subimage (inCursor) to get access to the image voxels covered by kernel elements. So

inCursor[indexTab[3]]

is the input image voxel covered by the third kernel element.

If you use the macros CALC_ROW_H_EXT() and CALC_ROW_CPP_EXT() then you also get the following parameters:

outIndex specifying the index of the output image from which the row shall be processed.

separationPass specifying the index of the processing pass of the buffer if a separable kernel filtering is applied.

The following stuff is needed to be implement as a kernel operation:

In the header file:

    CALC_ROW_H();
    template <typename DATATYPE>
      void calcRow(MLsoffset    *indexTab,
                   size_t       indexTabSize,
                   MLsoffset    srcVoxelOffset,
                   size_t       numVox,
                   DATATYPE     *inCursor,
                   DATATYPE     *outCursor,
                   const ImageVector &rowStart);

The following code implements a standard correlation of the input image with an arbitrary kernel in the .cpp file:

    template <typename DATATYPE>
    void ClassName::calcRow(MLsoffset    *indexTab,
                            size_t       indexTabSize,
                            MLsoffset    srcVoxelOffset,
                            size_t       numVox,
                            DATATYPE     *inCursor,
                            DATATYPE     *outCursor,
                            const ImageVector &rowStart)
    {
      // Get fast pointer to tables of kernel values.
      const KernelDataType *valTab = getKernel().getValueTab();

      // Scan entire voxel line to be filtered.
      for (long i=0; i < numVox; i++){
        // Sum up the products of the kernel element values and the input image voxels
        // found in the input page covered by the kernel elements. Input image voxels
        // are found by offsetting the input image cursor with the indexes from \c indexTab.

        // Loop over all kernel elements and the input voxels covered by them.
        DATATYPE retVal = 0;
        for (size_t c=0; c < indexTabSize; ++c){
          retVal += inCursor[indexTab[c]] * valTab[c];
        }

        // Write result into output subimage.
        *outCursor = retVal;

        // Move read and write cursor forward.
        ++outCursor;
        ++inCursor;
      } // for
    } // calcRow

You can also use the KernelTools class to do the same stuff:

   template <typename DATATYPE>
   void ClassName::calcRow(MLsoffset    *indexTab,
                           size_t       indexTabSize,
                           MLsoffset    srcVoxelOffset,
                           size_t       numVox,
                           DATATYPE     *inCursor,
                           DATATYPE     *outCursor,
                           const ImageVector &/*rowStart*/)
   {
     KernelTools::correlateLine
       (inCursor, outCursor, numVox, getKernel().getValueTab(), indexTab, indexTabSize);
   }

For related code and more information see mlKernelExample.h, mlKernel.h, mlKernelBaseModule.h, mlKernelModule.h, mlKernelEditor.h, mlRankFilter.h, mlExtConvolutionFilter.h, mlKernelMacros, mlKernelTools.h and mlConvolutionFilter.h.

Definition at line 230 of file mlKernelBaseModule.h.


Member Enumeration Documentation

Enumerator specifying the calculation mode for the extent of the reference extent of the input image.

Enumerator:
Overlap 

Smallest component wise extent of all inputs (Overlap).

Input0_ExtWithoutFill 

Input 0 defines the reference extent of the input image, leaving undefined input areas undefined.

Input0_ExtWithFill 

Input 0 defines the reference extent of the input image, filling undefined input areas with fill value.

MaxExtentsWithoutFill 

Maximum component wise extent of all inputs, leaving undefined input areas undefined.

MaxExtentsWithFill 

Maximum component wise extent of all inputs, filling undefined input areas with fill value.

NumImageRefExtents 

Number of modes to determine reference extent of input image, not to be used as mode.

Definition at line 273 of file mlKernelBaseModule.h.


Constructor & Destructor Documentation

ml::KernelBaseModule::KernelBaseModule ( int  inputNum = 1,
int  outputNum = 1 
) [protected]

Constructor.

Initializes the fields, the members and the field connections with the field container. Default configuration is with one input and one output if no arguments are passed. The constructor is protected to avoid direct instantiations of this module.


Member Function Documentation

virtual void ml::KernelBaseModule::_calcKernelPage ( SubImage outSubImg,
int  outIndex,
SubImage inSubImgs,
int  numInSubImgs,
MLsoffset indexTab,
size_t  indexTabSize,
MLsoffset  srcVoxelOffset,
size_t  loopIdx,
size_t  iteration,
const ImageVector ov1,
const ImageVector ov2,
const ImageVector pOf,
size_t  numVox 
) [protected, pure virtual]

This method is not intended to be overloaded directly, because it is subject to change.

It will be overloaded by CALC_ROW macros. number of voxels in row to be processed.

Parameters:
outSubImgoutput subimage to be written
outIndexindex of output subimage
inSubImgsarray of input subimages
numInSubImgsnumber of input subimages
indexTaboffset table to input cursor
indexTabSizenumber of offsets in indexTab
srcVoxelOffsetoffset to input cursor to corresponding input voxel for output voxel
loopIdxcurrent pass of separable filtering
iterationcurrent pass of iterative filtering
ov1first corner voxel of output area to be written.
ov2second corner voxel of output area to be written.
pOfcoordinate shift between input kernel origin and written output voxel
virtual void ml::KernelBaseModule::_connectToAllOutputs ( Field field) [protected, virtual]

Helper function to connect a field field to all output fields.

NULL field pointers are legal and will be ignored.

virtual ImageVector ml::KernelBaseModule::_getCalculatedReferenceInputExt ( ) const [protected, virtual]

Return the reference extent of input image dependent on the current value of _referenceExtentFld.

virtual void ml::KernelBaseModule::_setCorrectCorrelationMinMax ( bool  imageIntervalUsed = false,
MLdouble  intervalMin = 0,
MLdouble  intervalMax = 0,
MLint  inIdx = 0,
MLint  outIdx = 0 
) [protected, virtual]

This method calculates and sets the possible min/max values for output image outIdx if a correlation or convolution filtering is assumed on input inIdx.

Often also an interval is used to limit filtering. The interval [intervalMin, intervalMax] can be used to specify voxel values which includes the voxels values to be filtered or excludes them if intervalMin is smaller than intervalMax, i.e. which remain unchanged by the filtering process. If imageIntervalUsed is passed as true then image interval and normal min/max values from input image are included for the case that voxels are simply copied and not filtered. Also exclusive intervals with intervalMax < intervalMin are considered correctly. Note that intervalMin and intervalMax only need to contain correct values if imageIntervalUsed is true; otherwise they're ignored. Only to be called in calculateOutputImageProperties()!

virtual void ml::KernelBaseModule::calcInSubImagePreparation ( int  outIndex,
SubImage inSubImgs 
) [protected, virtual]

Does some preprocessing on input subimages if we have more than one.

In modes Input0_ExtWithFill and MaxExtentsWithFill in _referenceExtentFld we might have undefined regions in input subimages which we fill with the fill value. Normally it is automatically called by CALC_ROW macros. So "overloading by hand" is typically not recommended.

virtual void ml::KernelBaseModule::calcOutSubImagePostProcessing ( SubImage outSubImg,
int  outIndex,
SubImage inSubImgs 
) [protected, virtual]

Called to postProcess the output image outSubImg with the index outIndex from the input subimage inSubImgs.

Currently it does nothing. Normally it is automatically called by CALC_ROW macros. So "overloading by hand" is typically not recommended.

Reimplemented in ml::KernelModule.

virtual SubImageBox ml::KernelBaseModule::calculateInputSubImageBox ( int  inIndex,
const SubImageBox outSubImgBox,
int  outIndex 
) [protected, virtual]

Calculates the region of the input image needed for the calculation of the output image region defined by outSubImg and output index outIndex.

The border handling is considered correctly in the calculation of the box of the input subimage.

Reimplemented from ml::Module.

virtual void ml::KernelBaseModule::calculateOutputImageProperties ( int  outIndex) [protected, virtual]

Computes the output image properties for output outIndex.

The world coordinates and the extents of the output image are corrected dependent on the border handling.

Reimplemented from ml::Module.

Reimplemented in ml::BitMorphologyFilter, ml::ConvolutionFilter, ml::ExtConvolutionFilter, ml::KernelCurvatureEstimationFilter, ml::KernelModule, ml::LocalMaximaFilter, ml::LoGFilter, ml::MorphologyFilter, ml::RankFilter, ml::RobertsFilter, ml::SigmaFilter, ml::Sobel3DFilter, ml::StdDeviationFilter, ml::SurroundFilter, and ml::ZeroCrossingsFilter.

EnumField* ml::KernelBaseModule::getBorderHandlingFld ( ) const [inline]

The field to handle the mode how kernel operations work on image borders.

See BorderHandling documentation in mlKernelTools.h. Default value of the field is NO_PAD.

Definition at line 259 of file mlKernelBaseModule.h.

const Kernel& ml::KernelBaseModule::getConstKernel ( ) const [inline]

Method to access the kernel as constant object. See also getKernel().

Definition at line 250 of file mlKernelBaseModule.h.

DoubleField* ml::KernelBaseModule::getFillValueFld ( ) const [inline]

The field value is value written in input or output image borders where a border handling requires filling with a fill value.

Default field value is 0.

Definition at line 264 of file mlKernelBaseModule.h.

Kernel& ml::KernelBaseModule::getKernel ( ) [inline]

Definition at line 247 of file mlKernelBaseModule.h.

EnumField& ml::KernelBaseModule::getReferenceExtentModeFld ( ) const [inline]

Returns how the reference extent for input images is calculated; valid values are InputImageRefExtent (except of NumImageRefExtents) and default is Overlap.

Definition at line 301 of file mlKernelBaseModule.h.


Friends And Related Function Documentation

friend class KernelBaseOpCalculateOutputImageHandler [friend]

Definition at line 232 of file mlKernelBaseModule.h.


The documentation for this class was generated from the following file: