8#ifndef META_OCEAN_CV_NON_MAXIMUM_SUPPRESSION_H
9#define META_OCEAN_CV_NON_MAXIMUM_SUPPRESSION_H
40 template <
typename TCoordinate,
typename TStrength>
62 inline const TStrength&
strength()
const;
71 template <
bool tLeftLargerThanRight = true>
83 template <
typename TCoordinate,
typename TStrength>
117 template <
typename TCoordinate,
typename TStrength>
145 inline unsigned int x()
const;
198 inline unsigned int width()
const;
204 inline unsigned int height()
const;
210 inline unsigned int yOffset()
const;
220 inline void addCandidate(
const unsigned int x,
const unsigned int y,
const T& strength);
235 void addCandidates(
const T* values,
const unsigned int valuesPaddingElements,
const unsigned int firstColumn,
const unsigned int numberColumns,
const unsigned int firstRow,
const unsigned int numberRows,
const T& minimalThreshold,
Worker* worker);
259 template <
typename TCoordinate,
typename TStrength,
bool tStrictMaximum = true>
305 template <
typename TCoordinate,
typename TStrength,
bool tStrictMaximum>
318 template <
typename TFloat>
331 template <
typename TFloat>
347 void addCandidatesSubset(
const T* values,
const unsigned int valuesStrideElements,
const unsigned int firstColumn,
const unsigned int numberColumns,
const T* minimalThreshold,
const unsigned int firstRow,
const unsigned int numberRows);
361 template <
typename TCoordinate,
typename TStrength,
bool tStrictMaximum>
373template <
typename TCoordinate,
typename TStrength>
381template <
typename TCoordinate,
typename TStrength>
387template <
typename TCoordinate,
typename TStrength>
388template <
bool tLeftLargerThanRight>
391 if constexpr (tLeftLargerThanRight)
432 *
this = std::move(nonMaximumSuppression);
437 width_(nonMaximumSuppression.width_),
438 rows_(nonMaximumSuppression.rows_)
478 if (
rows_[y].empty())
480 rows_[y].reserve(128);
483 rows_[y].emplace_back(x, strength);
487void NonMaximumSuppressionT<T>::addCandidates(
const T* values,
const unsigned int valuesPaddingElements,
const unsigned int firstColumn,
const unsigned int numberColumns,
const unsigned int firstRow,
const unsigned int numberRows,
const T& minimalThreshold,
Worker* worker)
489 ocean_assert(values !=
nullptr);
491 ocean_assert(firstColumn + numberColumns <=
width_);
495 const unsigned int valuesStrideElements =
width_ + valuesPaddingElements;
499 worker->
executeFunction(
Worker::Function::create(*
this, &
NonMaximumSuppressionT<T>::addCandidatesSubset, values, valuesStrideElements, firstColumn, numberColumns, &minimalThreshold, 0u, 0u), firstRow, numberRows, 5u, 6u, 20u);
503 addCandidatesSubset(values, valuesStrideElements, firstColumn, numberColumns, &minimalThreshold, firstRow, numberRows);
514 while (!suppressionRow.empty())
516 if (suppressionRow.back().x() >= x)
518 suppressionRow.pop_back();
528template <
typename TCoordinate,
typename TStrength,
bool tStrictMaximum>
531 ocean_assert(firstColumn + numberColumns <=
width_);
534 if (firstColumn + numberColumns >
width_)
544 strengthPositions.reserve(strengthPositions.size() + 128);
546 if (worker !=
nullptr)
549 worker->
executeFunction(
Worker::Function::create(*
this, &
NonMaximumSuppressionT<T>::suppressNonMaximumSubset<TCoordinate, TStrength, tStrictMaximum>, &strengthPositions, firstColumn, numberColumns, &lock, positionCallback, 0u, 0u), firstRow, numberRows, 5u, 6u, 3u);
553 suppressNonMaximumSubset<TCoordinate, TStrength, tStrictMaximum>(&strengthPositions, firstColumn, numberColumns,
nullptr, positionCallback, firstRow, numberRows);
562 ocean_assert(firstColumn + numberColumns <=
width_);
563 ocean_assert(firstRow + numberRows <= (
unsigned int)(
rows_.
endIndex()));
565 const unsigned int endColumn = firstColumn + numberColumns;
567 for (
unsigned int y = firstRow; y < firstRow + numberRows; ++y)
573 if (candidate.x() < firstColumn)
578 if (candidate.x() >= endColumn)
583 strengthPositions.emplace_back(candidate.x(), y, candidate.strength());
600 if (
this != &nonMaximumSuppression)
603 nonMaximumSuppression.width_ = 0u;
605 rows_ = std::move(nonMaximumSuppression.rows_);
614 if (
this != &nonMaximumSuppression)
624template <
typename TCoordinate,
typename TStrength,
bool tStrictMaximum>
632 const unsigned int horizontalBins = std::max(1u, (
width + binSize - 1u) / binSize);
633 const unsigned int verticalBins = std::max(1u, (
height + binSize - 1u) / binSize);
635 ocean_assert(binSize * horizontalBins >=
width);
636 ocean_assert(binSize * verticalBins >=
height);
642 for (
size_t n = 0; n < strengthPositions.size(); ++n)
646 ocean_assert((
unsigned int)(position.
x()) <
width);
647 ocean_assert((
unsigned int)(position.
y()) <
height);
649 const unsigned int xBin = (
unsigned int)(position.
x()) / binSize;
650 const unsigned int yBin = (
unsigned int)(position.
y()) / binSize;
652 ocean_assert(xBin <= horizontalBins);
653 ocean_assert(yBin <= verticalBins);
655 indexGroups[yBin * horizontalBins + xBin].emplace_back(
Index32(n));
658 std::vector<uint8_t> validPositions(strengthPositions.size(), 1u);
662 for (
size_t nCandidate = 0; nCandidate < strengthPositions.size(); ++nCandidate)
664 if (validPositions[nCandidate] == 0u)
672 const unsigned int xCandidateBin = (
unsigned int)(candidatePosition.
x()) / binSize;
673 const unsigned int yCandidateBin = (
unsigned int)(candidatePosition.
y()) / binSize;
675 ocean_assert(xCandidateBin <= horizontalBins);
676 ocean_assert(yCandidateBin <= verticalBins);
678 bool checkNextCandidate =
false;
680 for (
unsigned int yBin = (
unsigned int)(max(0,
int(yCandidateBin) - 1)); !checkNextCandidate && yBin < min(yCandidateBin + 2u, verticalBins); ++yBin)
682 for (
unsigned int xBin = (
unsigned int)(max(0,
int(xCandidateBin) - 1)); !checkNextCandidate && xBin < min(xCandidateBin + 2u, horizontalBins); ++xBin)
684 const Indices32& indices = indexGroups[yBin * horizontalBins + xBin];
686 for (
const Index32& nTest : indices)
688 if (nTest ==
Index32(nCandidate))
697 if (candidatePosition.
sqrDistance(testPosition) <= sqrRadius)
701 validPositions[nTest] = 0u;
705 validPositions[nCandidate] = 0u;
707 checkNextCandidate =
true;
714 if constexpr (tStrictMaximum)
718 validPositions[nCandidate] = 0u;
719 validPositions[nTest] = 0u;
721 checkNextCandidate =
true;
728 if (candidatePosition.
y() < testPosition.
y() || (candidatePosition.
y() == testPosition.
y() && candidatePosition.
x() < testPosition.
x()))
732 validPositions[nCandidate] = 0u;
734 checkNextCandidate =
true;
739 ocean_assert(testPosition.
y() < candidatePosition.
y() || (testPosition.
y() == candidatePosition.
y() && testPosition.
x() < candidatePosition.
x()));
743 validPositions[nTest] = 0u;
754 remainingPositions.reserve(strengthPositions.size());
758 ocean_assert(validIndices->empty());
760 validIndices->clear();
761 validIndices->reserve(strengthPositions.size());
763 for (
size_t n = 0; n < validPositions.size(); ++n)
765 if (validPositions[n])
767 remainingPositions.emplace_back(strengthPositions[n]);
768 validIndices->emplace_back(
Index32(n));
774 for (
size_t n = 0; n < validPositions.size(); ++n)
776 if (validPositions[n])
778 remainingPositions.emplace_back(strengthPositions[n]);
783 return remainingPositions;
787template <
typename TFloat>
790 static_assert(std::is_floating_point<TFloat>::value,
"Invalid floating point data type!");
803 const TFloat df = (TFloat(rightValue) - TFloat(leftValue)) * TFloat(0.5);
806 const TFloat dff = TFloat(leftValue) + TFloat(rightValue) - TFloat(middleValue) * TFloat(2);
810 location = TFloat(0);
814 const TFloat x = -df / dff;
816 if (x < TFloat(-1) || x > TFloat(1))
826template <
typename TFloat>
829 static_assert(std::is_floating_point<TFloat>::value,
"Invalid floating point data type!");
831 const T& value00 = topValues[0];
832 const T& value01 = topValues[1];
833 const T& value02 = topValues[2];
835 const T& value10 = centerValues[0];
836 const T& value11 = centerValues[1];
837 const T& value12 = centerValues[2];
839 const T& value20 = bottomValues[0];
840 const T& value21 = bottomValues[1];
841 const T& value22 = bottomValues[2];
845 ocean_assert(value11 >= value00 && value11 >= value01 && value11 >= value02);
846 ocean_assert(value11 >= value10 && value11 >= value12);
847 ocean_assert(value11 >= value20 && value11 >= value21 && value11 >= value22);
851 const TFloat dx = TFloat(value12 - value10) * TFloat(0.5);
852 const TFloat dy = TFloat(value21 - value01) * TFloat(0.5);
855 const TFloat dxx = TFloat(value12 + value10 - value11 * TFloat(2));
856 const TFloat dyy = TFloat(value21 + value01 - value11 * TFloat(2));
862 const TFloat dxy = TFloat(value22 + value00 - value20 - value02) * TFloat(0.25);
864 const TFloat denominator = dxx * dyy - dxy * dxy;
872 const TFloat factor = TFloat(1) / denominator;
874 const TFloat offsetX = -(dyy * dx - dxy * dy) * factor;
875 const TFloat offsetY = -(dxx * dy - dxy * dx) * factor;
877 if (offsetX < TFloat(-1) || offsetX > TFloat(1) || offsetY < TFloat(-1) || offsetY > TFloat(1))
887void NonMaximumSuppressionT<T>::addCandidatesSubset(
const T* values,
const unsigned int valuesStrideElements,
const unsigned int firstColumn,
const unsigned int numberColumns,
const T* minimalThreshold,
const unsigned int firstRow,
const unsigned int numberRows)
889 ocean_assert(values !=
nullptr);
890 ocean_assert(valuesStrideElements >=
width_);
891 ocean_assert(firstColumn + numberColumns <=
width_);
896 const T localThreshold = *minimalThreshold;
898 values += firstRow * valuesStrideElements;
900 for (
unsigned int y = firstRow; y < firstRow + numberRows; ++y)
902 for (
unsigned int x = firstColumn; x < firstColumn + numberColumns; ++x)
904 if (values[x] >= localThreshold)
910 values += valuesStrideElements;
915template <
typename TCoordinate,
typename TStrength,
bool tStrictMaximum>
918 ocean_assert(strengthPositions);
920 ocean_assert(firstColumn + numberColumns <=
width_);
922 ocean_assert(firstRow + numberRows <= (
unsigned int)
rows_.
endIndex());
924 if (numberColumns < 3u || numberRows < 3u)
929 const unsigned int firstCenterColumn = max(1u, firstColumn);
930 const unsigned int endCenterColumn = min(firstColumn + numberColumns,
width_ - 1u);
932 const unsigned int firstCenterRow = max((
unsigned int)
rows_.
firstIndex() + 1u, firstRow);
933 const unsigned int endCenterRow = min(firstRow + numberRows, (
unsigned int)
rows_.
lastIndex());
935 ocean_assert(firstCenterRow >= 1u);
938 localStrengthPositions.reserve(100);
940 for (
unsigned int y = firstCenterRow; y < endCenterRow; ++y)
946 typename StrengthCandidateRow::const_iterator i0 = row0.
begin();
947 typename StrengthCandidateRow::const_iterator i2 = row2.begin();
949 typename StrengthCandidateRow::const_iterator i1Minus = row1.end();
950 typename StrengthCandidateRow::const_iterator i1Plus = row1.size() > 1 ? row1.begin() + 1 : row1.end();
952 for (
typename StrengthCandidateRow::const_iterator i1 = row1.begin(); i1 != row1.end(); ++i1)
954 ocean_assert(i1->x() >= 0u && i1->x() + 1u <=
width_);
957 if (i1->x() >= firstCenterColumn && i1->x() < endCenterColumn && (i1Minus == row1.end() || i1Minus->x() + 1u != i1->x() || (tStrictMaximum && i1Minus->strength() < i1->strength()) || (!tStrictMaximum && i1Minus->strength() <= i1->strength())))
960 if (i1Plus == row1.end() || i1Plus->x() != i1->x() + 1u || i1Plus->strength() < i1->strength())
963 while (i0 != row0.end())
965 if (i0->x() + 1u < i1->x())
976 ocean_assert(i0 == row0.end() || i0->x() + 1u >= i1->x());
978 if (i0 != row0.end() && i0->x() <= i1->x() + 1u)
980 ocean_assert(i0->x() + 1u == i1->x() || i0->x() == i1->x() || i0->x() - 1u == i1->x());
982 if ((tStrictMaximum && i0->strength() >= i1->strength()) || (!tStrictMaximum && i0->strength() > i1->strength()))
989 const typename StrengthCandidateRow::const_iterator i0Plus = i0 + 1;
991 if (i0Plus != row0.end() && i0Plus->x() <= i1->x() + 1u)
993 if ((tStrictMaximum && i0Plus->strength() >= i1->strength()) || (!tStrictMaximum && i0Plus->strength() > i1->strength()))
1000 const typename StrengthCandidateRow::const_iterator i0PlusPlus = i0Plus + 1;
1002 if (i0PlusPlus != row0.end() && i0PlusPlus->x() <= i1->x() + 1u)
1004 ocean_assert(i0PlusPlus->x() == i1->x() + 1u);
1006 if ((tStrictMaximum && i0PlusPlus->strength() >= i1->strength()) || (!tStrictMaximum && i0PlusPlus->strength() > i1->strength()))
1016 while (i2 != row2.end())
1018 if (i2->x() + 1u < i1->x())
1029 ocean_assert(i2 == row2.end() || i2->x() + 1u >= i1->x());
1031 if (i2 != row2.end() && i2->x() <= i1->x() + 1u)
1033 ocean_assert(i2->x() + 1u == i1->x() || i2->x() == i1->x() || i2->x() - 1u == i1->x());
1035 if (i2->x() + 1u == i1->x())
1039 if ((tStrictMaximum && i2->strength() >= i1->strength()) || (!tStrictMaximum && i2->strength() > i1->strength()))
1046 if (i2->strength() >= i1->strength())
1054 const typename StrengthCandidateRow::const_iterator i2Plus = i2 + 1;
1056 if (i2Plus != row2.end() && i2Plus->x() <= i1->x() + 1u)
1058 if (i2Plus->strength() >= i1->strength())
1065 const typename StrengthCandidateRow::const_iterator i2PlusPlus = i2Plus + 1;
1067 if (i2PlusPlus != row2.end() && i2PlusPlus->x() <= i1->x() + 1u)
1069 ocean_assert(i2PlusPlus->x() == i1->x() + 1u);
1071 if (i2PlusPlus->strength() >= i1->strength())
1079 if (positionCallback)
1082 TStrength preciseStrength;
1083 if ((*positionCallback)(i1->x(), y, i1->strength(), preciseX, preciseY, preciseStrength))
1085 localStrengthPositions.emplace_back(preciseX, preciseY, preciseStrength);
1099 if (i1Plus != row1.end())
1108 strengthPositions->insert(strengthPositions->end(), localStrengthPositions.begin(), localStrengthPositions.end());
This class extends a 2D position by a third parameter storing a strength value.
Definition NonMaximumSuppression.h:42
const TStrength & strength() const
Returns the strength parameter of this object.
Definition NonMaximumSuppression.h:382
StrengthPosition()=default
Creates a new object with default strength parameter.
TStrength strength_
Strength parameter of this object.
Definition NonMaximumSuppression.h:77
static bool compareStrength(const StrengthPosition< TCoordinate, TStrength > &left, const StrengthPosition< TCoordinate, TStrength > &right)
Compares the strength value of two objects.
Definition NonMaximumSuppression.h:389
This class provides base functionality and type definitions for non-maximum-suppression operations.
Definition NonMaximumSuppression.h:32
std::vector< StrengthPosition< TCoordinate, TStrength > > StrengthPositions
Definition of a vector holding strength pixel positions.
Definition NonMaximumSuppression.h:84
This class holds the horizontal position and strength parameter of an interest pixel.
Definition NonMaximumSuppression.h:126
StrengthCandidate()
Creates a new candidate object.
Definition NonMaximumSuppression.h:402
unsigned int positionX_
Horizontal position of this object.
Definition NonMaximumSuppression.h:156
const T & strength() const
Returns the strength parameter of this object.
Definition NonMaximumSuppression.h:424
unsigned int x() const
Returns the horizontal position of this candidate object.
Definition NonMaximumSuppression.h:418
T strength_
Strength parameter of this object.
Definition NonMaximumSuppression.h:159
This class implements the possibility to find local maximum in a 2D array by applying a non-maximum-s...
Definition NonMaximumSuppression.h:104
unsigned int yOffset() const
Returns the optional offset in the vertical direction.
Definition NonMaximumSuppression.h:464
void suppressNonMaximumSubset(StrengthPositions< TCoordinate, TStrength > *strengthPositions, const unsigned int firstColumn, const unsigned int firstRow, Lock *lock, const PositionCallback< TCoordinate, TStrength > *positionCallback, const unsigned int numberColumns, const unsigned int numberRows) const
Applies a non-maximum-suppression search on a subset of a given 2D frame in a 3x3 neighborhood (eight...
Definition NonMaximumSuppression.h:916
void reset()
Removes the gathered non-maximum suppression information so that this object can be reused again (for...
Definition NonMaximumSuppression.h:589
static bool determinePrecisePeakLocation2(const T *const topValues, const T *const centerValues, const T *const bottomValues, VectorT2< TFloat > &location)
Determines the precise peak location in 2D space for nine discrete neighboring measurements at locati...
Definition NonMaximumSuppression.h:827
void addCandidate(const unsigned int x, const unsigned int y, const T &strength)
Adds a new candidate to this object.
Definition NonMaximumSuppression.h:472
static StrengthPositions< TCoordinate, TStrength > suppressNonMaximum(const unsigned int width, const unsigned int height, const StrengthPositions< TCoordinate, TStrength > &strengthPositions, const TCoordinate radius, Indices32 *validIndices=nullptr)
Applies a non-maximum-suppression based on already existing strength positions (just with a custom su...
void removeCandidatesRightFrom(const unsigned int x, const unsigned int y)
Removes all candidates from a specified row having a horizontal location equal or larger than a speci...
Definition NonMaximumSuppression.h:508
void candidates(const unsigned int firstColumn, const unsigned int numberColumns, const unsigned int firstRow, const unsigned int numberRows, StrengthPositions< unsigned int, T > &strengthPositions)
Returns all gathered candidates of this object.
Definition NonMaximumSuppression.h:560
unsigned int height() const
Returns the height of this object.
Definition NonMaximumSuppression.h:458
bool suppressNonMaximum(const unsigned int firstColumn, const unsigned int numberColumns, const unsigned int firstRow, const unsigned int numberRows, StrengthPositions< TCoordinate, TStrength > &strengthPositions, Worker *worker=nullptr, const PositionCallback< TCoordinate, TStrength > *positionCallback=nullptr) const
Applies a non-maximum-suppression search on a given 2D frame in a 3x3 neighborhood (eight neighbors).
Definition NonMaximumSuppression.h:529
static bool determinePrecisePeakLocation1(const T &leftValue, const T &middleValue, const T &rightValue, TFloat &location)
Determines the precise peak location in 1D space for three discrete neighboring measurements at locat...
Definition NonMaximumSuppression.h:788
NonMaximumSuppressionT< T > & operator=(NonMaximumSuppressionT< T > &&nonMaximumSuppression)
Move operator.
Definition NonMaximumSuppression.h:598
std::vector< StrengthCandidate > StrengthCandidateRow
Definition of a vector holding strength candidate objects.
Definition NonMaximumSuppression.h:165
NonMaximumSuppressionT(NonMaximumSuppressionT< T > &&nonMaximumSuppression) noexcept
Move constructor.
Definition NonMaximumSuppression.h:430
void addCandidates(const T *values, const unsigned int valuesPaddingElements, const unsigned int firstColumn, const unsigned int numberColumns, const unsigned int firstRow, const unsigned int numberRows, const T &minimalThreshold, Worker *worker)
Adds new candidates to this object from a given buffer providing one value for each bin/pixel of this...
Definition NonMaximumSuppression.h:487
void addCandidatesSubset(const T *values, const unsigned int valuesStrideElements, const unsigned int firstColumn, const unsigned int numberColumns, const T *minimalThreshold, const unsigned int firstRow, const unsigned int numberRows)
Adds new candidates to this object from a subset of a given buffer providing one value for each bin/p...
Definition NonMaximumSuppression.h:887
StrengthCandidateRows rows_
All candidate rows.
Definition NonMaximumSuppression.h:370
unsigned int width_
Width of this object.
Definition NonMaximumSuppression.h:367
unsigned int width() const
Returns the width of this object.
Definition NonMaximumSuppression.h:452
This class implements a container for callback functions.
Definition Callback.h:3454
static Caller< void > create(CT &object, typename MemberFunctionPointerMaker< CT, void, NullClass, NullClass, NullClass, NullClass, NullClass, NullClass, NullClass, NullClass, NullClass, NullClass, NullClass, NullClass, NullClass, NullClass, NullClass, NullClass, NullClass, NullClass, NullClass, NullClass >::Type function)
Creates a new caller container for a member function with no function parameter.
Definition Caller.h:3024
This class implements a recursive lock object.
Definition Lock.h:31
This class provides basic numeric functionalities.
Definition Numeric.h:57
This class implements an optional recursive scoped lock object locking the lock object only if it's d...
Definition Lock.h:387
bool isValidIndex(const Index index) const
Returns whether a specific index is valid for this vector and matches to the current offset layout.
Definition ShiftVector.h:646
size_t size() const
Returns the number of elements that are stored by this object.
Definition ShiftVector.h:490
Index endIndex() const
Returns the index of the element behind the last (excluding) element of this object.
Definition ShiftVector.h:435
std::ptrdiff_t Index
Definition of an element index.
Definition ShiftVector.h:38
void clear()
Clears this object, the specified index shift will be untouched.
Definition ShiftVector.h:658
Iterator begin()
Returns the iterator for the first data element.
Definition ShiftVector.h:670
Index lastIndex() const
Returns the index of the last (including) element of this object.
Definition ShiftVector.h:422
Index firstIndex() const
Returns the index of the first element of this object.
Definition ShiftVector.h:416
This class implements a vector with two elements.
Definition Vector2.h:96
const TCoordinate & x() const noexcept
Returns the x value.
Definition Vector2.h:710
const TCoordinate & y() const noexcept
Returns the y value.
Definition Vector2.h:722
T sqrDistance(const VectorT2< T > &right) const
Returns the square distance between this 2D position and a second 2D position.
Definition Vector2.h:645
This class implements a worker able to distribute function calls over different threads.
Definition Worker.h:33
bool executeFunction(const Function &function, const unsigned int first, const unsigned int size, const unsigned int firstIndex=(unsigned int)(-1), const unsigned int sizeIndex=(unsigned int)(-1), const unsigned int minimalIterations=1u, const unsigned int threadIndex=(unsigned int)(-1))
Executes a callback function separable by two function parameters.
std::vector< Index32 > Indices32
Definition of a vector holding 32 bit index values.
Definition Base.h:96
std::vector< Indices32 > IndexGroups32
Definition of a vector holding 32 bit indices, so we have groups of indices.
Definition Base.h:102
uint32_t Index32
Definition of a 32 bit index value.
Definition Base.h:84
The namespace covering the entire Ocean framework.
Definition Accessor.h:15