MeVisLab Toolbox Reference
|
Class to manage a filtering kernel for images. More...
#include <mlKernel.h>
Public Types | |
enum | KernModifier { KERN_SET = 0 , KERN_MULT , KERN_ADD , KERN_INVDIV , KERN_INVSUB , KERN_SQR , KERN_SQRT , KERN_POW , KERN_LOG , KERN_NORMALIZE , KERN_GAUSS , KERN_SPHERE , KERN_MIRROR , KERN_MIRROR_X , KERN_MIRROR_Y , KERN_MIRROR_Z , KERN_MIRROR_C , KERN_MIRROR_T , KERN_MIRROR_U , NUM_KERN_MODIFYERS } |
Manipulation modes for kernel values with a given value v: More... | |
Public Member Functions | |
TKernel () | |
Constructor. Builds an empty kernel. | |
TKernel (const TKernel &kern) | |
Copy constructor. Builds a kernel with contents of kern . | |
virtual | ~TKernel () |
Destructor. Cleans up and removes instance of kernel. | |
const TKernel & | operator= (const TKernel &kern) |
Assignment operator. Sets current instance to contents of kern . | |
void | reset () |
Reset kernel to construction state. | |
size_t | getTabSize () const |
Returns the current number of kernel elements. | |
const ImageVector * | getCoordTab (size_t dim=0) const |
Gets a table of coordinates pointing to all kernel elements which are currently defined. | |
const KDATATYPE * | getValueTab (size_t dim=0) const |
Gets the table of kernel element values which are currently defined. | |
KDATATYPE | getValueTabSum () const |
Return the sum of all kernel element values. | |
KDATATYPE | getMinValue () const |
Return the minimum value of all kernel element values If kernel value table is empty then 0 is returned. | |
KDATATYPE | getMaxValue () const |
Return the maximum value of all kernel element values If kernel value table is empty then 1 is returned. | |
KDATATYPE | getNegativeValueSum () const |
Return the sum of all negative kernel element values. | |
KDATATYPE | getPositiveValueSum () const |
Return the sum of all positive kernel element values. | |
void | manipulateKernelElements (KernModifier mode, KDATATYPE v) |
Modify all kernel element values with the value v . | |
const ImageVector & | getExtent () const |
Returns the kernel extents in 6D. | |
const ImageVector & | getNegativeExtent () const |
The extent of the kernel to both sides. | |
const ImageVector & | getPositiveExtent () const |
See getNegativeExtent() . | |
MLint | findIndex (const ImageVector &pos) const |
Return index to kernel element if it exists; otherwise return -1. | |
void | setPartialKernel (size_t numElems, ImageVector *const coords, KDATATYPE *const values=nullptr) |
Defines a set of local kernel coordinates and optionally a set of values which define the kernel elements. | |
std::string | getKernel (bool asLines=false, MLint fieldWidth=0, MLint precision=10) const |
Returns the current kernel elements and values as string: The string needs has the following format: . | |
std::string | setKernel (const std::string &kernString) |
Defines elements and values of the kernel matrix by a string. | |
void | setKernel (const ImageVector &ext, const KDATATYPE *const values=nullptr, bool *const mask=nullptr) |
Defines a complete kernel matrix whose extents are defined by ext . | |
template<typename KDATATYPE2 > | |
void | setKernel (const ImageVector &ext, const KDATATYPE2 *const xaxis, const KDATATYPE2 *const yaxis, const KDATATYPE2 *const zaxis, const KDATATYPE2 *const caxis, const KDATATYPE2 *const taxis, const KDATATYPE2 *const uaxis, bool normalize) |
Defines a complete kernel matrix whose extents are defined by ext . | |
void | setSeparableKernel (const std::vector< typename std::vector< KDATATYPE > > &separableRows) |
Create kernel coordinate and value tables in separable table format, that means a 2-D kernel where each rows describes the elements of 1-D kernels. | |
void | resizeKernel (const ImageVector &ext, KDATATYPE newVal=0) |
Resizes the kernel to a new state and tries to maintain the previous elements if possible. | |
void | fillUndefinedElements (KDATATYPE newVal=0) |
Fill all undefined kernel elements with newVal . | |
void | makeCircular () |
Takes the current kernel, computes radii from the extents of the kernel and removes all kernel elements which are outside the ellipsoid defined by these radii. | |
void | mirror (int dimension=-1) |
Applies a point symmetric mirroring of all kernel elements. | |
void | setGauss (const ImageVector &ext) |
Replaces the current kernel by a normalized gauss kernel. | |
Support for an interpretation as separable kernel. | |
void | setSeparable (bool isSeparableVal) |
Set/unset a flag to indicate that the first 6 rows of the first kernel slice is considered as 1-D axes of a separable filter kernel. | |
bool | isSeparable () const |
Indicates whether the first 6 rows of the first kernel slice are interpreted as 1-D axes of a separable filter kernel; default is false. | |
ImageVector | getSeparableDimEntries () const |
Returns a ImageVector with the number of entries of the separable kernel for the dimensions 0,...,5. | |
ImageVector | getSeparableOneDimExtents () const |
Returns a vector where the components 0,...,5 contain the extent of the region spanned by the 6 separated 1-D kernels if the kernel is interpreted as a separable kernel. | |
size_t | getSeparableDimIndex (size_t dim=0) const |
Returns the index to entries of valueTab or coordTab which are related to the 1-D separable kernel for the dimension dim where dim must be from [0,5]. | |
const std::vector< ImageVector > & | getSeparableCoordTab () const |
Returns the table of with all coordinates for filtering with separable kernels. | |
Static Public Member Functions | |
static MLint | coordToIndex (MLint x, MLint y, MLint z, MLint c, MLint t, MLint u, const ImageVector &size) |
Converts the coordinate (x,y,z,c,t,u) into the kernel to an index into an array with 6D extents given by size . | |
static MLint | coordToIndex (const ImageVector &p, const ImageVector &size) |
Converts the coordinate into the kernel with extents size to an index. | |
static ImageVector | indexToCoord (MLint idx, const ImageVector &ext) |
Converts an index into an array with extents ext to a coordinate. | |
static ImageVector | calculateNegativeExtentFromExtent (const ImageVector &ext) |
Calculate the negative extent of a kernel from a kernel extent. | |
static ImageVector | calculatePositiveExtentFromExtent (const ImageVector &ext) |
Calculate the positive extent of a kernel from a kernel extent. | |
static MLldouble | binomialcoeff (MLint n, MLint k) |
Calculate binomial coefficients for (n) (k) | |
static std::vector< KDATATYPE > | get1DGauss (size_t numSamples, bool normalize=true) |
Returns a vector with numSample values binomial coefficients. | |
Protected Member Functions | |
void | _init () |
Initialization. Should be called only by constructors. | |
void | _clearTables () |
Clear all internal (dynamic) tables. | |
void | _addCoordinate (const ImageVector &pos, KDATATYPE value, bool update) |
Add a new coordinate with value to the coordinate and value table. | |
void | _calculateRealKernelExt () |
Calculate the correct maximum kernel extent for used kernel elements. | |
void | _validateSeparabilityInfos () const |
This method updates all members which are needed in query methods for separability properties of the kernel. | |
void | _invalidateSeparableInfos () |
Invalidate separability information. | |
Class to manage a filtering kernel for images.
This file contains the TKernel
class to manage a 6D kernel which can be applied to images. It handles a set of coordinates (see getCoordTab()
) and values for those coordinates (see getValueTab()
). This permits the specification of kernels with gaps or only a few defined elements. So big kernels with few elements can be manipulated and applied fast. A set of operations is available on a kernel instance which includes arithmetics on the kernel values, gauss presets, normalization, different kernel set and get routines to created/save partially defined kernels, get/set methods to load/save kernels as strings or arrays and much more.
This TKernel
class is implemented as template dependent on KDATATYPE to have different precisions for kernel elements. Normally the kernel is instantiated with double as KDATATYPE. You can also instantiate this class as MLfloat or MLldouble kernel. Instantiating this class with integer types as KDATATYPE is legal, however results of numeric operations are not rounded but cast. Also note that the usage of integer for KDATATYPE is not really useful since pure integer kernel operations are rare and some operations would suffer, because of the mentioned cast operations, because of integer overflows when many kernel elements are summed up or when creating default initializations where the sum of (all equal valued) kernel elements shall be one which is normally impossible with integer types.
The same consideration also holds for non scalar voxel values which could be used as kernel element type KDATATYPE; however no tests have been applied for that. At least conversions from/to strings will reduce values to scalars.
Since this class does not have any support to apply the kernel to the image since this is usually the algorithmic part of the modules. However there are some helper functions available in mlKernelTools
for standard convolutions which can be called directly from the modules filtering (sub)images.
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 73 of file mlKernel.h.
Manipulation modes for kernel values with a given value v:
KERN_SET
: Set all kernel elements to v
.KERN_MULT
: Multiply all kernel elements with v
.KERN_ADD
: Add v
to kernel elements.KERN_INVDIV
: Each kernel element is replaced by v
divided by the kernel element. Zero kernel elements are left unchanged.KERN_INVSUB
: Each kernel element is replaced by v
minus the kernel element.KERN_SQR
: Compute squares of all kernel elements.KERN_SQRT
: Compute square roots of all kernel elements. Negative kernel elements are left unchanged.KERN_POW
: Compute all kernel elements raised to the power of v
.KERN_LOG
: Compute logarithm of v
of all kernel elements.KERN_NORMALIZE
: Multiply the all kernel element values with a value so that their sum is 1. If sum is zero then values are left unchanged.KERN_GAUSS
: Set all kernel elements to binomial values and normalize.KERN_SPHERE
: Throw away corners of the kernel value table to make it approximately spherical.KERN_MIRROR
: Applies a point symmetric mirroring of all kernel elements.KERN_MIRROR_X
: Applies a point symmetric mirroring of all kernel elements in x direction.KERN_MIRROR_Y
: Applies a point symmetric mirroring of all kernel elements in y direction.KERN_MIRROR_Z
: Applies a point symmetric mirroring of all kernel elements in z direction.KERN_MIRROR_C
: Applies a point symmetric mirroring of all kernel elements in c direction.KERN_MIRROR_T
: Applies a point symmetric mirroring of all kernel elements in t direction.KERN_MIRROR_U
: Applies a point symmetric mirroring of all kernel elements in u direction. Definition at line 102 of file mlKernel.h.
|
inline |
Constructor. Builds an empty kernel.
Definition at line 553 of file mlKernel.h.
|
inline |
Copy constructor. Builds a kernel with contents of kern
.
Definition at line 562 of file mlKernel.h.
References mlrange_cast().
|
inlinevirtual |
Destructor. Cleans up and removes instance of kernel.
Definition at line 572 of file mlKernel.h.
|
protected |
Add a new coordinate with value to the coordinate and value table.
Still not tested! If update is true (default) then the kernel extents are updated.
Definition at line 1850 of file mlKernel.h.
References ML_NO_MEMORY, ML_PRINT_FATAL_ERROR, and mlrange_cast().
|
protected |
Calculate the correct maximum kernel extent for used kernel elements.
The maximum extent in all dimensions is reduced as much that all kernel coordinates just remain in valid extent. Leading empty entries in the kernel are not removed. Empty kernels are considered as kernels with extent (1,1,1,1,1,1).
Definition at line 1908 of file mlKernel.h.
References ML_PRINT_FATAL_ERROR, and ML_PROGRAMMING_ERROR.
|
protected |
Clear all internal (dynamic) tables.
Definition at line 1827 of file mlKernel.h.
|
protected |
Initialization. Should be called only by constructors.
Definition at line 1800 of file mlKernel.h.
|
protected |
Invalidate separability information.
Definition at line 2057 of file mlKernel.h.
|
protected |
This method updates all members which are needed in query methods for separability properties of the kernel.
Note: This method changes mutable members since it is required for get-methods on information about separability information.
Definition at line 2070 of file mlKernel.h.
References mlrange_cast().
|
static |
Calculate binomial coefficients for (n) (k)
Definition at line 1718 of file mlKernel.h.
References mlrange_cast().
|
inlinestatic |
Calculate the negative extent of a kernel from a kernel extent.
Definition at line 989 of file mlKernel.h.
References mlrange_cast().
|
inlinestatic |
Calculate the positive extent of a kernel from a kernel extent.
Definition at line 998 of file mlKernel.h.
References mlrange_cast().
|
inlinestatic |
Converts the coordinate into
the kernel with extents size
to an index.
Definition at line 971 of file mlKernel.h.
|
inlinestatic |
Converts the coordinate (x,y,z,c,t,u) into the kernel to an index into an array with 6D extents given by size
.
Definition at line 962 of file mlKernel.h.
void ml::TKernel< KDATATYPE >::fillUndefinedElements | ( | KDATATYPE | newVal = 0 | ) |
Fill all undefined kernel elements with newVal
.
Definition at line 1543 of file mlKernel.h.
References mlrange_cast().
MLint ml::TKernel< KDATATYPE >::findIndex | ( | const ImageVector & | pos | ) | const |
Return index to kernel element if it exists; otherwise return -1.
Note that this method needs to search; so it's not very efficient.
Definition at line 1009 of file mlKernel.h.
|
static |
Returns a vector with numSample
values binomial coefficients.
If numSamples
is passed as 0 then an empty vector is returned. if normalize
is passed true (the default) then all values are normalized to the sum of absolute values 1.
Definition at line 1742 of file mlKernel.h.
References ml::mlAbs(), and mlrange_cast().
|
inline |
Gets a table of coordinates pointing to all kernel elements which are currently defined.
The size of the returned table is given by getTabSize(). In the case of separable filtering it returns those coordinates which are valid for the current pass of separable kernel filtering. It is then a subset of the array given by getSeparableCoordTab()
.
Definition at line 644 of file mlKernel.h.
|
inline |
Returns the kernel extents in 6D.
It defines the rectangular region in which all coordinates returned by getCoordTab()
are found. Note that the returned region might be larger than required e.g. after removing elements from kernel. In the case of separable filtering it returns a vector where the components 0,...,5 contain the extent of the region spanned by the 6 separated 1-D kernels. Note that components for those dimensions where no filtering takes place are set to 1.
Definition at line 913 of file mlKernel.h.
std::string ml::TKernel< KDATATYPE >::getKernel | ( | bool | asLines = false, |
MLint | fieldWidth = 0, | ||
MLint | precision = 10 ) const |
Returns the current kernel elements and values as string: The string needs has the following format:
.
(x_0, y_0, z_0, c_0, t_0, u_0):v_0
...
(x_n, y_n, z_n, c_n, t_n, u_n):v_n
where the coordinates left from ':' specify the 6d coordinate of the kernel element; the value v_x right from ':' specifies the value of the kernel element. If asLines is true then a second string format is used:
(*, y_0, z_0, c_0, t_0, u_0):x_0 ... x_n
...
(*, y_n, z_n, c_n, t_n, u_n):y_n ... y_n
So all kernel elements of a row are saved in a string line; so the x coordinates becomes invalid and is set as an asterisk. To have a minimum field width pass fieldWidth
and the digits after the period if given by precision
. Note that these settings are used only if asLines is true.
Definition at line 1097 of file mlKernel.h.
References mlrange_cast(), and ml::TVector< TVectorBase >::print().
KDATATYPE ml::TKernel< KDATATYPE >::getMaxValue | ( | ) | const |
Return the maximum value of all kernel element values If kernel value table is empty then 1 is returned.
Definition at line 691 of file mlKernel.h.
References mlrange_cast().
KDATATYPE ml::TKernel< KDATATYPE >::getMinValue | ( | ) | const |
Return the minimum value of all kernel element values If kernel value table is empty then 0 is returned.
Definition at line 678 of file mlKernel.h.
References mlrange_cast().
|
inline |
The extent of the kernel to both sides.
The sum of both +1 is the extent of the kernel. Using getNegativeExtent()
as negative extent of an image and getPositiveExtent()
as positive extent increment for the image then the kernel can be placed correctly on all normal image voxels without having voxel accesses out of range.
Definition at line 931 of file mlKernel.h.
KDATATYPE ml::TKernel< KDATATYPE >::getNegativeValueSum | ( | ) | const |
Return the sum of all negative kernel element values.
If kernel value table is empty then 0 is returned.
Definition at line 705 of file mlKernel.h.
References mlrange_cast().
|
inline |
See getNegativeExtent()
.
Definition at line 946 of file mlKernel.h.
KDATATYPE ml::TKernel< KDATATYPE >::getPositiveValueSum | ( | ) | const |
Return the sum of all positive kernel element values.
If kernel value table is empty then 0 is returned.
Definition at line 720 of file mlKernel.h.
References mlrange_cast().
const std::vector< ImageVector > & ml::TKernel< KDATATYPE >::getSeparableCoordTab | ( | ) | const |
Returns the table of with all coordinates for filtering with separable kernels.
Definition at line 2047 of file mlKernel.h.
|
inline |
Returns a ImageVector with the number of entries of the separable kernel for the dimensions 0,...,5.
That means the n-th index contains the number of valueTab entries of row n, where n is from [0,...,5].
Definition at line 1978 of file mlKernel.h.
size_t ml::TKernel< KDATATYPE >::getSeparableDimIndex | ( | size_t | dim = 0 | ) | const |
Returns the index to entries of valueTab or coordTab which are related to the 1-D separable kernel for the dimension dim
where dim
must be from [0,5].
Smaller values are clamped to 0, higher to 5. With the default parameter the index to the entries for the 1-D kernel of the x-dimension is returned.
Definition at line 2007 of file mlKernel.h.
References ML_PRINT_FATAL_ERROR, and ML_PROGRAMMING_ERROR.
|
inline |
Returns a vector where the components 0,...,5 contain the extent of the region spanned by the 6 separated 1-D kernels if the kernel is interpreted as a separable kernel.
Note that components of the extent are 0 for those dimensions where no entries are available.
Definition at line 1992 of file mlKernel.h.
|
inline |
Returns the current number of kernel elements.
Definition at line 624 of file mlKernel.h.
|
inline |
Gets the table of kernel element values which are currently defined.
The size of the returned table is given by getTabSize(). In the case of separable filtering the subset of kernel elements for the pass dim
can be requested.
Definition at line 656 of file mlKernel.h.
KDATATYPE ml::TKernel< KDATATYPE >::getValueTabSum | ( | ) | const |
Return the sum of all kernel element values.
Definition at line 665 of file mlKernel.h.
References mlrange_cast().
|
inlinestatic |
Converts an index into an array with extents ext
to a coordinate.
Definition at line 980 of file mlKernel.h.
References mlrange_cast().
|
inline |
Indicates whether the first 6 rows of the first kernel slice are interpreted as 1-D axes of a separable filter kernel; default is false.
Definition at line 1967 of file mlKernel.h.
void ml::TKernel< KDATATYPE >::makeCircular | ( | ) |
Takes the current kernel, computes radii from the extents of the kernel and removes all kernel elements which are outside the ellipsoid defined by these radii.
Note that filter properties of kernel changes by this operation.
Definition at line 1585 of file mlKernel.h.
References mlrange_cast().
void ml::TKernel< KDATATYPE >::manipulateKernelElements | ( | KernModifier | mode, |
KDATATYPE | v ) |
Modify all kernel element values with the value v
.
Definition at line 732 of file mlKernel.h.
References ML_BAD_PARAMETER, ML_PRINT_ERROR, ML_PROGRAMMING_ERROR, mlrange_cast(), MLValueIs0WOM(), and MLValuesDifferWOM().
Applies a point symmetric mirroring of all kernel elements.
The dimension
specifies a mirroring dimension if in range [0,5], otherwise mirroring is applied in all dimensions. Default is -1.
Definition at line 1660 of file mlKernel.h.
References mlrange_cast().
const TKernel< KDATATYPE > & ml::TKernel< KDATATYPE >::operator= | ( | const TKernel< KDATATYPE > & | kern | ) |
Assignment operator. Sets current instance to contents of kern
.
Definition at line 581 of file mlKernel.h.
References mlrange_cast().
void ml::TKernel< KDATATYPE >::reset | ( | ) |
Reset kernel to construction state.
Definition at line 610 of file mlKernel.h.
void ml::TKernel< KDATATYPE >::resizeKernel | ( | const ImageVector & | ext, |
KDATATYPE | newVal = 0 ) |
Resizes the kernel to a new state and tries to maintain the previous elements if possible.
Note that new kernel size may differ from desired one if empty areas are at border of the resized kernel so that only a smaller real kernel remains. Regions which are created newly get kernel elements with value newVal
.
Definition at line 1489 of file mlKernel.h.
References mlrange_cast().
void ml::TKernel< KDATATYPE >::setGauss | ( | const ImageVector & | ext | ) |
Replaces the current kernel by a normalized gauss kernel.
Definition at line 1774 of file mlKernel.h.
References mlrange_cast().
void ml::TKernel< KDATATYPE >::setKernel | ( | const ImageVector & | ext, |
const KDATATYPE *const | values = nullptr, | ||
bool *const | mask = nullptr ) |
Defines a complete kernel matrix whose extents are defined by ext
.
If desired the set of kernel values can be passed in values
. Otherwise all kernel values are set to 1.0/(KDATATYPE)ext.compMul(), i.e. to the number of entries of the matrix. If desired a set of mask values can be specified. If mask
[n] is true then tab[n] is included in the kernel; otherwise it's not part of the kernel. Internally the specified kernel matrix is handled as a table of kernel elements. Note that values
and mask
must have ext.compMul() elements if they are defined.
Definition at line 1363 of file mlKernel.h.
References ML_CALCULATION_ERROR, ML_NO_MEMORY, ML_PRINT_FATAL_ERROR, and mlrange_cast().
|
inline |
Defines a complete kernel matrix whose extents are defined by ext
.
The values of the kernel elements are products of the corresponding values along the six axis given as parameters. This is a convenient way to build a kernel from separated axis. If desired the kernel is normalized. Note: The following function definition must be implemented in the class definition See BUG: LNK2001 on Member Function When Use Nested Class Template, Article ID: Q128789
Definition at line 296 of file mlKernel.h.
References ML_FATAL_MEMORY_ERROR, and mlrange_cast().
std::string ml::TKernel< KDATATYPE >::setKernel | ( | const std::string & | kernString | ) |
Defines elements and values of the kernel matrix by a string.
The string needs to have the following format:
(x_0, y_0, z_0, c_0, t_0, u_0):v_0
...
(x_n, y_n, z_n, c_n, t_n, u_n):v_n
where the coordinates left from ':' specify the 6d coordinate of the kernel element; the value v_x right from ':' specifies the value of the kernel element. Only one coordinate entry is scanned per line. So big kernels with a few elements can be set easily. As long as lines with kernel elements are found elements are set in the kernel table. Return string is empty on successful scan. On errors it contains the number of error lines.
A second way to specify more kernel elements at once is with the following string line format:
(*, y_0, z_0, c_0, t_0, u_0):x_0, ... ,x_n
...
(*, y_n, z_n, c_n, t_n, u_n):y_n, ... ,y_n
So all kernel elements of a row are found in a string line. Note that the x coordinates becomes invalid and must set as an asterisk. Empty elements in the kernel can be left empty before between and after commas.
Definition at line 1215 of file mlKernel.h.
References ML_FATAL_MEMORY_ERROR, and mlrange_cast().
void ml::TKernel< KDATATYPE >::setPartialKernel | ( | size_t | numElems, |
ImageVector *const | coords, | ||
KDATATYPE *const | values = nullptr ) |
Defines a set of local kernel coordinates and optionally a set of values which define the kernel elements.
numElems
defines the number of coordinates. If desired the corresponding set of kernel values can be passed in values
. Otherwise all kernel values are set to 1.0/(KDATATYPE)_tabSize. Note that coords
and values
have to contain numElems
entries if passed.
Definition at line 1030 of file mlKernel.h.
References mlrange_cast().
|
inline |
Set/unset a flag to indicate that the first 6 rows of the first kernel slice is considered as 1-D axes of a separable filter kernel.
Note that this flag only denotes how applications shall interpret this kernel; however it does NOT change any behaviour of this class.
Definition at line 1953 of file mlKernel.h.
References mlrange_cast().
void ml::TKernel< KDATATYPE >::setSeparableKernel | ( | const std::vector< typename std::vector< KDATATYPE > > & | separableRows | ) |
Create kernel coordinate and value tables in separable table format, that means a 2-D kernel where each rows describes the elements of 1-D kernels.
At most 6 rows make sense, because the maximum kernel dimension is 6. It is legal to leave out any axis by passing empty vectors to define lower dimensional separable kernels. The separability flag is set to true.
Definition at line 1436 of file mlKernel.h.
References mlrange_cast().