73template <
typename KDATATYPE>
class TKernel
301 _invalidateSeparableInfos();
308 _calculateRealKernelExt();
338 manipulateKernelElements(KERN_NORMALIZE, 0);
521 mutable bool _areSeparabilityInfosUpToDate;
541 mutable std::vector<ImageVector> _separableCoordTab;
587 setPartialKernel(
kern._tabSize,
kern._coordTab,
kern._valueTab);
590 _isSeparable =
kern._isSeparable;
591 _areSeparabilityInfosUpToDate =
kern._areSeparabilityInfosUpToDate;
592 _separableDimEntries =
kern._separableDimEntries;
593 _separableOneDimExt =
kern._separableOneDimExt;
594 _separableExt =
kern._separableExt;
595 _separableNegExt =
kern._separableNegExt;
596 _separablePosExt =
kern._separablePosExt;
597 _separableCoordTab =
kern._separableCoordTab;
623 _validateSeparabilityInfos();
624 return getSeparableCoordTab().size();
641 return _isSeparable ? &(getSeparableCoordTab()[getSeparableDimIndex(dim)]) : _coordTab;
652 return _isSeparable ? (_valueTab + getSeparableDimIndex(dim)) : _valueTab;
662 for (
size_t i = 0;
i < _tabSize;
i++) {
676 for (
size_t i = 1;
i < _tabSize;
i++) {
677 if (_valueTab[
i] < v) {
692 for (
size_t i = 1;
i < _tabSize;
i++) {
693 if (_valueTab[
i] > v) {
708 for (
size_t i = 0;
i < _tabSize;
i++) {
710 if (!(_valueTab[
i] > 0)) {
724 for (
size_t i = 0;
i < _tabSize;
i++) {
725 if (_valueTab[
i] > 0) {
739 _invalidateSeparableInfos();
745 for (
size_t i = 0;
i < _tabSize;
i++) {
753 for (
size_t i = 0;
i < _tabSize;
i++) {
761 for (
size_t i = 0;
i < _tabSize;
i++) {
769 for (
size_t i = 0;
i < _tabSize;
i++) {
771 _valueTab[
i] = v / _valueTab[
i];
779 for (
size_t i = 0;
i < _tabSize;
i++) {
780 _valueTab[
i] = v - _valueTab[
i];
787 for (
size_t i = 0;
i < _tabSize;
i++) {
788 _valueTab[
i] *= _valueTab[
i];
796 for (
size_t i = 0;
i < _tabSize;
i++) {
801 _valueTab[
i] =
static_cast<KDATATYPE>(
sqrt(
static_cast<double>(_valueTab[
i])));
809 for (
size_t i = 0;
i < _tabSize;
i++) {
834 for (
size_t i = 0;
i < _tabSize;
i++) {
851 manipulateKernelElements(KERN_MULT,
static_cast<KDATATYPE>(1. / sum));
909 case NUM_KERN_MODIFYERS:
911 ML_PRINT_ERROR(
"TKernel<KDATATYPE>::manipulateKernelElements(mlKernModifier mode, KDATATYPE v)",
912 ML_PROGRAMMING_ERROR,
"NUM_KERN_MODIFYERS is an invalid parameter. Ignoring kernel manipulation.");
918 "Invalid parameter passed. Ignoring kernel manipulation.");
936 _validateSeparabilityInfos();
937 return _separableExt;
953 _validateSeparabilityInfos();
954 return _separableNegExt;
967 _validateSeparabilityInfos();
968 return _separablePosExt;
979template <
typename KDATATYPE>
982 return SubImage::coordToIndex(x, y, z, c, t, u, size);
990 return SubImage::coordToIndex(p, size);
998 return SubImage::indexToCoord(
idx,
ext);
1025 for (
size_t c = 0; c < _tabSize; c++) {
1026 if (_coordTab[c] == pos) {
1042template <
typename KDATATYPE>
1058 for (
size_t c = 0; c < _tabSize; c++) {
1059 _coordTab[c] =
coords[c];
1070 for (
size_t d = 0; d < _tabSize; d++) {
1078 _calculateRealKernelExt();
1113template <
typename KDATATYPE>
1116 std::string
str =
"";
1129 for (
MLint u = 0; u < getExtent().u; u++) {
1130 for (
MLint t = 0; t < getExtent().t; t++) {
1131 for (
MLint c = 0; c < getExtent().c; c++) {
1132 for (
MLint z = 0; z < getExtent().z; z++) {
1133 for (
MLint y = 0; y < getExtent().y; y++) {
1135 snprintf(
strLine, 255,
"(*,%lld,%lld,%lld,%lld,%lld):", y, z, c, t, u);
1148 for (
MLint x = 0; x < getExtent().x; x++) {
1157 strLine[0] = x == 0 ?
' ' :
',';
1191 for (
size_t c = 0; c < _tabSize; c++) {
1243 char* copy =
nullptr;
1259 if (
str[
str.length() - 1] !=
'\n') {
1264 MLint lineNumber = 0;
1267 MLint x = 0, y = 0, z = 0, c = 0, t = 0, u = 0, num = 0;
1269 num =
sscanf(
str.c_str(),
"(%lld,%lld,%lld,%lld,%lld,%lld):%lf", &x, &y, &z, &c, &t, &u, &
fValue);
1273 _addCoordinate(
ImageVector(x, y, z, c, t, u), value,
false);
1277 num =
sscanf(
str.c_str(),
"(*,%lld,%lld,%lld,%lld,%lld):", &y, &z, &c, &t, &u);
1280 const size_t cPos =
str.find(
':');
1281 if (
cPos != std::string::npos) {
1290 while (
go && (
str.length() != 0)) {
1291 const char ch =
str[0];
1299 else if (
'\n' ==
ch) {
1305 else if (
',' ==
ch) {
1344 Memory::freeMemory(copy);
1354 const size_t pos =
str.find(
'\n');
1355 if (pos != std::string::npos) {
1358 str.erase(0, pos + 1);
1371 _calculateRealKernelExt();
1387template <
typename KDATATYPE>
1399 for (
size_t c = 0; c <
maxElems; c++) {
1414 if (!_coordTab || !_valueTab) {
1418 "Cannot create kernel filter tables, so tables are reset. This will probably cause other errors.");
1421 if (_coordTab && _valueTab) {
1430 for (
size_t d = 0; d < _tabSize; d++) {
1438 for (
size_t e = 0; e <
maxElems; e++) {
1439 if (!mask || (mask && mask[e])) {
1440 _coordTab[
idx] = indexToCoord(e,
ext);
1445 if (
idx != _tabSize) {
1448 "Internal error. Cannot create kernel filter tables. This will probably cause other errors.");
1456 _calculateRealKernelExt();
1468template <
typename KDATATYPE>
1478 size_t numEntries = 0;
1479 for (
size_t e = 0; e <
numRows; ++e) {
1490 for (
size_t r = 0; r <
numRows; ++r) {
1515 _validateSeparabilityInfos();
1534 for (
MLint u = 0; u <
ext.u; u++) {
1535 for (
MLint t = 0; t <
ext.t; t++) {
1536 for (
MLint c = 0; c <
ext.c; c++) {
1537 for (
MLint z = 0; z <
ext.z; z++) {
1538 for (
MLint y = 0; y <
ext.y; y++) {
1539 for (
MLint x = 0; x <
ext.x; x++) {
1544 if ((u < getExtent().u) && (t < getExtent().t) && (c < getExtent().c) && (z < getExtent().z) &&
1545 (y < getExtent().y) && (x < getExtent().x))
1568 _calculateRealKernelExt();
1578 _invalidateSeparableInfos();
1581 for (
MLint u = 0; u < getExtent().u; u++) {
1582 for (
MLint t = 0; t < getExtent().t; t++) {
1583 for (
MLint c = 0; c < getExtent().c; c++) {
1584 for (
MLint z = 0; z < getExtent().z; z++) {
1585 for (
MLint y = 0; y < getExtent().y; y++) {
1586 for (
MLint x = 0; x < getExtent().x; x++) {
1605 _calculateRealKernelExt();
1618 _invalidateSeparableInfos();
1645 _coordTab =
nullptr;
1648 _valueTab =
nullptr;
1656 for (
size_t e = 0; e <
tabSize; e++) {
1668 _addCoordinate(v,
valueTab[e],
false);
1679 _calculateRealKernelExt();
1691 _invalidateSeparableInfos();
1695 _coordTab =
nullptr;
1698 _valueTab =
nullptr;
1738 _calculateRealKernelExt();
1754 if ((
k != 0) && (
k !=
n)) {
1755 for (
i = 1;
i < (
n + 1);
i++) {
1758 for (
i = 1;
i < (
k + 1);
i++) {
1761 for (
i = 1;
i < (
n -
k + 1);
i++) {
1779 std::vector<KDATATYPE> v;
1795 if (normalize && (sum > 0)) {
1812 _invalidateSeparableInfos();
1818 std::vector<KDATATYPE> vx = get1DGauss(
ext.x,
false);
1819 std::vector<KDATATYPE> vy = get1DGauss(
ext.y,
false);
1820 std::vector<KDATATYPE> vz = get1DGauss(
ext.z,
false);
1821 std::vector<KDATATYPE> vc = get1DGauss(
ext.c,
false);
1822 std::vector<KDATATYPE>
vt = get1DGauss(
ext.t,
false);
1823 std::vector<KDATATYPE>
vu = get1DGauss(
ext.u,
false);
1827 setKernel(
ext, &(vx[0]), &(vy[0]), &(vz[0]), &(vc[0]), &(
vt[0]), &(
vu[0]),
true);
1842 _valueTab =
nullptr;
1843 _coordTab =
nullptr;
1846 _isSeparable =
false;
1847 _areSeparabilityInfosUpToDate =
false;
1848 _separableDimEntries.set(0);
1849 _separableOneDimExt.set(0);
1850 _separableExt.set(0);
1851 _separableNegExt.set(0);
1852 _separablePosExt.set(0);
1853 _separableCoordTab.clear();
1866 _areSeparabilityInfosUpToDate =
false;
1870 _valueTab =
nullptr;
1872 _coordTab =
nullptr;
1881template <
typename KDATATYPE>
1889 _areSeparabilityInfosUpToDate =
false;
1904 "Cannot create kernel tables. Returning with invalid "
1905 "reset tables. This will probably cause other errors.");
1911#if defined(__clang__)
1921#pragma clang diagnostic push
1922#pragma clang diagnostic ignored "-Wnontrivial-memcall"
1923#elif defined(__GNUC__)
1929#pragma GCC diagnostic push
1930#pragma GCC diagnostic ignored "-Wclass-memaccess"
1933#if defined(__clang__)
1934#pragma clang diagnostic pop
1935#elif defined(__GNUC__)
1936#pragma GCC diagnostic pop
1955 _calculateRealKernelExt();
1973 if (getTabSize() > 0) {
1974 for (
size_t c = 0; c < getTabSize(); c++) {
1975 maximum = ImageVector::compMax(_coordTab[c], maximum);
1985 _negExt = calculateNegativeExtentFromExtent(_ext);
1986 _posExt = calculatePositiveExtentFromExtent(_ext);
1989 if (!_ext.allBiggerZero()) {
1991 "Kernel has invalid extents now. Continuing anyway.");
2013 _invalidateSeparableInfos();
2023 return _isSeparable;
2033 _validateSeparabilityInfos();
2034 return _separableDimEntries;
2046 _validateSeparabilityInfos();
2047 return _separableOneDimExt;
2060 _validateSeparabilityInfos();
2071 return static_cast<size_t>(_separableDimEntries.x);
2074 return static_cast<size_t>(_separableDimEntries.x + _separableDimEntries.y);
2077 return static_cast<size_t>(_separableDimEntries.x + _separableDimEntries.y + _separableDimEntries.z);
2080 return static_cast<size_t>(_separableDimEntries.x + _separableDimEntries.y + _separableDimEntries.z +
2081 _separableDimEntries.c);
2084 return static_cast<size_t>(_separableDimEntries.x + _separableDimEntries.y + _separableDimEntries.z +
2085 _separableDimEntries.c + _separableDimEntries.t);
2099 _validateSeparabilityInfos();
2100 return _separableCoordTab;
2108 _areSeparabilityInfosUpToDate =
false;
2121 if (_areSeparabilityInfosUpToDate) {
2124 if (!_areSeparabilityInfosUpToDate) {
2130 _separableDimEntries.set(0);
2131 _separableOneDimExt.set(0);
2132 _separableExt.set(0);
2133 _separableCoordTab.clear();
2136 while ((
idx < _tabSize) && (_coordTab[
idx].y < 6)) {
2146 _separableDimEntries[
entryY]++;
2165 _separableCoordTab.push_back(
sepCoord);
2173 _separableExt = _separableOneDimExt;
2174 _separableExt.fillSmallerComps(1, 1);
2182 for (
size_t e = 0; e <
idx; ++e) {
2185 for (
size_t d = 0; d < 6; ++d) {
2187 if (coord[d] == -1) {
2195 _separableNegExt = _separableExt /
ImageVector(2);
2199 _areSeparabilityInfosUpToDate =
true;
Class to manage a filtering kernel for images.
virtual ~TKernel()
Destructor. Cleans up and removes instance of kernel.
void mirror(int dimension=-1)
Applies a point symmetric mirroring of all 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: ...
const ImageVector & getPositiveExtent() const
See getNegativeExtent().
bool isSeparable() const
Indicates whether the first 6 rows of the first kernel slice are interpreted as 1-D axes of a separab...
static ImageVector indexToCoord(MLint idx, const ImageVector &ext)
Converts an index into an array with extents ext to a coordinate.
void makeCircular()
Takes the current kernel, computes radii from the extents of the kernel and removes all kernel elemen...
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.
void _invalidateSeparableInfos()
Invalidate separability information.
static MLldouble binomialcoeff(MLint n, MLint k)
Calculate binomial coefficients for (n) (k)
void _init()
Initialization. Should be called only by constructors.
void fillUndefinedElements(KDATATYPE newVal=0)
Fill all undefined kernel elements with newVal.
static MLint coordToIndex(const ImageVector &p, const ImageVector &size)
Converts the coordinate into the kernel with extents size to an index.
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 elem...
size_t getTabSize() const
Returns the current number of kernel elements.
ImageVector getSeparableDimEntries() const
Returns a ImageVector with the number of entries of the separable kernel for the dimensions 0,...
KDATATYPE getMinValue() const
Return the minimum value of all kernel element values If kernel value table is empty then 0 is return...
KDATATYPE getMaxValue() const
Return the maximum value of all kernel element values If kernel value table is empty then 1 is return...
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 axe...
const TKernel & operator=(const TKernel &kern)
Assignment operator. Sets current instance to contents of kern.
void manipulateKernelElements(KernModifier mode, KDATATYPE v)
Modify all kernel element values with the value v.
TKernel()
Constructor. Builds an empty kernel.
static std::vector< KDATATYPE > get1DGauss(size_t numSamples, bool normalize=true)
Returns a vector with numSample values binomial coefficients.
std::string setKernel(const std::string &kernString)
Defines elements and values of the kernel matrix by a string.
void reset()
Reset kernel to construction state.
const ImageVector & getExtent() const
Returns the kernel extents in 6D.
ImageVector getSeparableOneDimExtents() const
Returns a vector where the components 0,...,5 contain the extent of the region spanned by the 6 separ...
const std::vector< ImageVector > & getSeparableCoordTab() const
Returns the table of with all coordinates for filtering with separable kernels.
const KDATATYPE * getValueTab(size_t dim=0) const
Gets the table of kernel element values which are currently defined.
void _clearTables()
Clear all internal (dynamic) tables.
MLint findIndex(const ImageVector &pos) const
Return index to kernel element if it exists; otherwise return -1.
KernModifier
Manipulation modes for kernel values with a given value v:
KDATATYPE getValueTabSum() const
Return the sum of all kernel element values.
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.
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...
const ImageVector * getCoordTab(size_t dim=0) const
Gets a table of coordinates pointing to all kernel elements which are currently defined.
TKernel(const TKernel &kern)
Copy constructor. Builds a kernel with contents of kern.
void _addCoordinate(const ImageVector &pos, KDATATYPE value, bool update)
Add a new coordinate with value to the coordinate and value table.
const ImageVector & getNegativeExtent() const
The extent of the kernel to both sides.
void _calculateRealKernelExt()
Calculate the correct maximum kernel extent for used kernel elements.
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 fo...
void setGauss(const ImageVector &ext)
Replaces the current kernel by a normalized gauss kernel.
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 setKernel(const ImageVector &ext, const KDATATYPE *const values=nullptr, bool *const mask=nullptr)
Defines a complete kernel matrix whose extents are defined by ext.
void _validateSeparabilityInfos() const
This method updates all members which are needed in query methods for separability properties of the ...
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 ea...
void resizeKernel(const ImageVector &ext, KDATATYPE newVal=0)
Resizes the kernel to a new state and tries to maintain the previous elements if possible.
std::string print(const std::string &openStr="", const std::string &sepStr=" ", const std::string &termStr="", const int numDigits=-1) const
Returns the string openStr + v.array[0] + sepStr + ... + v.array[NumberOfDimensions-2] + sepStr + v....
bool MLValueIs0WOM(MLint8 a)
Returns true if value is 0; otherwise, it returns false.
bool MLValuesDifferWOM(MLint8 a, MLint8 b)
Returns true if values differ; otherwise, it returns false.
#define ML_CALCULATION_ERROR
This is an unspecific error used in some cases where the error is not very specific.
#define ML_PROGRAMMING_ERROR
A case occurred that should not appear and there are a variety of reasons, typically it is a programm...
#define ML_BAD_PARAMETER
A bad/invalid parameter (or even an inappropriate image) has been passed to a module or an algorithm,...
#define ML_NO_MEMORY
The system does not have enough memory to perform the desired operation; try to reduce application da...
#define ML_PRINT_FATAL_ERROR(FUNC_NAME, REASON, HANDLING)
Like ML_PRINT_FATAL_ERROR_DUMP(FUNC_NAME, REASON, HANDLING, RT_OBJ) without a runtime object to be du...
#define ML_PRINT_ERROR(FUNC_NAME, REASON, HANDLING)
Like ML_PRINT_ERROR_DUMP(FUNC_NAME, REASON, HANDLING, RT_OBJ) without a runtime object to be dumped.
Target mlrange_cast(Source arg)
Generic version of checked ML casts.
@ ML_FATAL_MEMORY_ERROR
On allocation failure, a fatal error print is done and NULL is returned.
MLint64 MLint
A signed ML integer type with at least 64 bits used for index calculations on very large images even ...
T mlAbs(T a)
Defines ML specific abs template since only type-dependent library functions exists.
MLdouble KernelDataType
Define the standard data type for kernel elements to be used in this library.