8#ifndef META_OCEAN_CV_NON_MAXIMUM_SUPPRESSION_H
9#define META_OCEAN_CV_NON_MAXIMUM_SUPPRESSION_H
49 template <
typename TCoordinate,
typename TStrength>
71 inline const TStrength&
strength()
const;
80 template <
bool tLeftLargerThanRight = true>
92 template <
typename TCoordinate,
typename TStrength>
105 template <
typename TCoordinate,
typename TStrength>
133 inline unsigned int x()
const;
186 inline unsigned int width()
const;
192 inline unsigned int height()
const;
198 inline unsigned int yOffset()
const;
208 inline void addCandidate(
const unsigned int x,
const unsigned int y,
const T& strength);
223 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);
246 template <
typename TCoordinate,
typename TStrength,
bool tStrictMaximum = true>
292 template <
typename TCoordinate,
typename TStrength,
bool tStrictMaximum>
305 template <
typename TFloat>
318 template <
typename TFloat>
334 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);
348 template <
typename TCoordinate,
typename TStrength,
bool tStrictMaximum>
361template <
typename TCoordinate,
typename TStrength>
370template <
typename TCoordinate,
typename TStrength>
377template <
typename TCoordinate,
typename TStrength>
378template <
bool tLeftLargerThanRight>
381 if constexpr (tLeftLargerThanRight)
422 *
this = std::move(nonMaximumSuppression);
427 width_(nonMaximumSuppression.width_),
428 rows_(nonMaximumSuppression.rows_)
468 if (
rows_[y].empty())
470 rows_[y].reserve(128);
473 rows_[y].emplace_back(x, strength);
477void NonMaximumSuppression<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)
479 ocean_assert(values !=
nullptr);
481 ocean_assert(firstColumn + numberColumns <=
width_);
485 const unsigned int valuesStrideElements =
width_ + valuesPaddingElements;
489 worker->
executeFunction(
Worker::Function::create(*
this, &
NonMaximumSuppression<T>::addCandidatesSubset, values, valuesStrideElements, firstColumn, numberColumns, &minimalThreshold, 0u, 0u), firstRow, numberRows, 5u, 6u, 20u);
493 addCandidatesSubset(values, valuesStrideElements, firstColumn, numberColumns, &minimalThreshold, firstRow, numberRows);
504 while (!suppressionRow.empty())
506 if (suppressionRow.back().x() >= x)
508 suppressionRow.pop_back();
518template <
typename TCoordinate,
typename TStrength,
bool tStrictMaximum>
521 ocean_assert(firstColumn + numberColumns <=
width_);
530 worker->
executeFunction(
Worker::Function::create(*
this, &
NonMaximumSuppression<T>::suppressNonMaximumSubset<TCoordinate, TStrength, tStrictMaximum>, &result, firstColumn, numberColumns, &lock, positionCallback, 0u, 0u), firstRow, numberRows, 5u, 6u, 3u);
534 suppressNonMaximumSubset<TCoordinate, TStrength, tStrictMaximum>(&result, firstColumn, numberColumns,
nullptr, positionCallback, firstRow, numberRows);
543 ocean_assert(firstColumn + numberColumns <=
width_);
544 ocean_assert(firstRow + numberRows <= (
unsigned int)(
rows_.
endIndex()));
546 const unsigned int endColumn = firstColumn + numberColumns;
548 for (
unsigned int y = firstRow; y < firstRow + numberRows; ++y)
554 if (candidate.x() < firstColumn)
559 if (candidate.x() >= endColumn)
564 strengthPositions.emplace_back(candidate.x(), y, candidate.strength());
581 if (
this != &nonMaximumSuppression)
584 nonMaximumSuppression.width_ = 0u;
586 rows_ = std::move(nonMaximumSuppression.rows_);
595 if (
this != &nonMaximumSuppression)
605template <
typename TCoordinate,
typename TStrength,
bool tStrictMaximum>
613 const unsigned int horizontalBins = std::max(1u, (
width + binSize - 1u) / binSize);
614 const unsigned int verticalBins = std::max(1u, (
height + binSize - 1u) / binSize);
616 ocean_assert(binSize * horizontalBins >=
width);
617 ocean_assert(binSize * verticalBins >=
height);
623 for (
size_t n = 0; n < strengthPositions.size(); ++n)
627 ocean_assert((
unsigned int)(position.
x()) <
width);
628 ocean_assert((
unsigned int)(position.
y()) <
height);
630 const unsigned int xBin = (
unsigned int)(position.
x()) / binSize;
631 const unsigned int yBin = (
unsigned int)(position.
y()) / binSize;
633 ocean_assert(xBin <= horizontalBins);
634 ocean_assert(yBin <= verticalBins);
636 indexGroups[yBin * horizontalBins + xBin].emplace_back(
Index32(n));
639 std::vector<uint8_t> validPositions(strengthPositions.size(), 1u);
643 for (
size_t nCandidate = 0; nCandidate < strengthPositions.size(); ++nCandidate)
645 if (validPositions[nCandidate] == 0u)
653 const unsigned int xCandidateBin = (
unsigned int)(candidatePosition.
x()) / binSize;
654 const unsigned int yCandidateBin = (
unsigned int)(candidatePosition.
y()) / binSize;
656 ocean_assert(xCandidateBin <= horizontalBins);
657 ocean_assert(yCandidateBin <= verticalBins);
659 bool checkNextCandidate =
false;
661 for (
unsigned int yBin = (
unsigned int)(max(0,
int(yCandidateBin) - 1)); !checkNextCandidate && yBin < min(yCandidateBin + 2u, verticalBins); ++yBin)
663 for (
unsigned int xBin = (
unsigned int)(max(0,
int(xCandidateBin) - 1)); !checkNextCandidate && xBin < min(xCandidateBin + 2u, horizontalBins); ++xBin)
665 const Indices32& indices = indexGroups[yBin * horizontalBins + xBin];
667 for (
const Index32& nTest : indices)
669 if (nTest ==
Index32(nCandidate))
678 if (candidatePosition.
sqrDistance(testPosition) <= sqrRadius)
682 validPositions[nTest] = 0u;
686 validPositions[nCandidate] = 0u;
688 checkNextCandidate =
true;
695 if constexpr (tStrictMaximum)
699 validPositions[nCandidate] = 0u;
700 validPositions[nTest] = 0u;
702 checkNextCandidate =
true;
709 if (candidatePosition.
y() < testPosition.
y() || (candidatePosition.
y() == testPosition.
y() && candidatePosition.
x() < testPosition.
x()))
713 validPositions[nCandidate] = 0u;
715 checkNextCandidate =
true;
720 ocean_assert(testPosition.
y() < candidatePosition.
y() || (testPosition.
y() == candidatePosition.
y() && testPosition.
x() < candidatePosition.
x()));
724 validPositions[nTest] = 0u;
735 remainingPositions.reserve(strengthPositions.size());
739 ocean_assert(validIndices->empty());
741 validIndices->clear();
742 validIndices->reserve(strengthPositions.size());
744 for (
size_t n = 0; n < validPositions.size(); ++n)
746 if (validPositions[n])
748 remainingPositions.emplace_back(strengthPositions[n]);
749 validIndices->emplace_back(
Index32(n));
755 for (
size_t n = 0; n < validPositions.size(); ++n)
757 if (validPositions[n])
759 remainingPositions.emplace_back(strengthPositions[n]);
764 return remainingPositions;
768template <
typename TFloat>
771 static_assert(std::is_floating_point<TFloat>::value,
"Invalid floating point data type!");
784 const TFloat df = (TFloat(rightValue) - TFloat(leftValue)) * TFloat(0.5);
787 const TFloat dff = TFloat(leftValue) + TFloat(rightValue) - TFloat(middleValue) * TFloat(2);
791 location = TFloat(0);
795 const TFloat x = -df / dff;
797 if (x < TFloat(-1) || x > TFloat(1))
807template <
typename TFloat>
810 static_assert(std::is_floating_point<TFloat>::value,
"Invalid floating point data type!");
812 const T& value00 = topValues[0];
813 const T& value01 = topValues[1];
814 const T& value02 = topValues[2];
816 const T& value10 = centerValues[0];
817 const T& value11 = centerValues[1];
818 const T& value12 = centerValues[2];
820 const T& value20 = bottomValues[0];
821 const T& value21 = bottomValues[1];
822 const T& value22 = bottomValues[2];
826 ocean_assert(value11 >= value00 && value11 >= value01 && value11 >= value02);
827 ocean_assert(value11 >= value10 && value11 >= value12);
828 ocean_assert(value11 >= value20 && value11 >= value21 && value11 >= value22);
832 const TFloat dx = TFloat(value12 - value10) * TFloat(0.5);
833 const TFloat dy = TFloat(value21 - value01) * TFloat(0.5);
836 const TFloat dxx = TFloat(value12 + value10 - value11 * TFloat(2));
837 const TFloat dyy = TFloat(value21 + value01 - value11 * TFloat(2));
843 const TFloat dxy = TFloat(value22 + value00 - value20 - value02) * TFloat(0.25);
845 const TFloat denominator = dxx * dyy - dxy * dxy;
853 const TFloat factor = TFloat(1) / denominator;
855 const TFloat offsetX = -(dyy * dx - dxy * dy) * factor;
856 const TFloat offsetY = -(dxx * dy - dxy * dx) * factor;
858 if (offsetX < TFloat(-1) || offsetX > TFloat(1) || offsetY < TFloat(-1) || offsetY > TFloat(1))
868void NonMaximumSuppression<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)
870 ocean_assert(values !=
nullptr);
871 ocean_assert(valuesStrideElements >=
width_);
872 ocean_assert(firstColumn + numberColumns <=
width_);
877 const T localThreshold = *minimalThreshold;
879 values += firstRow * valuesStrideElements;
881 for (
unsigned int y = firstRow; y < firstRow + numberRows; ++y)
883 for (
unsigned int x = firstColumn; x < firstColumn + numberColumns; ++x)
885 if (values[x] >= localThreshold)
891 values += valuesStrideElements;
896template <
typename TCoordinate,
typename TStrength,
bool tStrictMaximum>
899 ocean_assert(strengthPositions);
901 ocean_assert(firstColumn + numberColumns <=
width_);
903 ocean_assert(firstRow + numberRows <= (
unsigned int)
rows_.
endIndex());
905 if (numberColumns < 3u || numberRows < 3u)
910 const unsigned int firstCenterColumn = max(1u, firstColumn);
911 const unsigned int endCenterColumn = min(firstColumn + numberColumns,
width_ - 1u);
913 const unsigned int firstCenterRow = max((
unsigned int)
rows_.
firstIndex() + 1u, firstRow);
914 const unsigned int endCenterRow = min(firstRow + numberRows, (
unsigned int)
rows_.
lastIndex());
916 ocean_assert(firstCenterRow >= 1u);
919 localStrengthPositions.reserve(100);
921 for (
unsigned int y = firstCenterRow; y < endCenterRow; ++y)
927 typename StrengthCandidateRow::const_iterator i0 = row0.
begin();
928 typename StrengthCandidateRow::const_iterator i2 = row2.begin();
930 typename StrengthCandidateRow::const_iterator i1Minus = row1.end();
931 typename StrengthCandidateRow::const_iterator i1Plus = row1.size() > 1 ? row1.begin() + 1 : row1.end();
933 for (
typename StrengthCandidateRow::const_iterator i1 = row1.begin(); i1 != row1.end(); ++i1)
935 ocean_assert(i1->x() >= 0u && i1->x() + 1u <=
width_);
938 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())))
941 if (i1Plus == row1.end() || i1Plus->x() != i1->x() + 1u || i1Plus->strength() < i1->strength())
944 while (i0 != row0.end())
946 if (i0->x() + 1u < i1->x())
957 ocean_assert(i0 == row0.end() || i0->x() + 1u >= i1->x());
959 if (i0 != row0.end() && i0->x() <= i1->x() + 1u)
961 ocean_assert(i0->x() + 1u == i1->x() || i0->x() == i1->x() || i0->x() - 1u == i1->x());
963 if ((tStrictMaximum && i0->strength() >= i1->strength()) || (!tStrictMaximum && i0->strength() > i1->strength()))
970 const typename StrengthCandidateRow::const_iterator i0Plus = i0 + 1;
972 if (i0Plus != row0.end() && i0Plus->x() <= i1->x() + 1u)
974 if ((tStrictMaximum && i0Plus->strength() >= i1->strength()) || (!tStrictMaximum && i0Plus->strength() > i1->strength()))
981 const typename StrengthCandidateRow::const_iterator i0PlusPlus = i0Plus + 1;
983 if (i0PlusPlus != row0.end() && i0PlusPlus->x() <= i1->x() + 1u)
985 ocean_assert(i0PlusPlus->x() == i1->x() + 1u);
987 if ((tStrictMaximum && i0PlusPlus->strength() >= i1->strength()) || (!tStrictMaximum && i0PlusPlus->strength() > i1->strength()))
997 while (i2 != row2.end())
999 if (i2->x() + 1u < i1->x())
1010 ocean_assert(i2 == row2.end() || i2->x() + 1u >= i1->x());
1012 if (i2 != row2.end() && i2->x() <= i1->x() + 1u)
1014 ocean_assert(i2->x() + 1u == i1->x() || i2->x() == i1->x() || i2->x() - 1u == i1->x());
1016 if (i2->x() + 1u == i1->x())
1020 if ((tStrictMaximum && i2->strength() >= i1->strength()) || (!tStrictMaximum && i2->strength() > i1->strength()))
1027 if (i2->strength() >= i1->strength())
1035 const typename StrengthCandidateRow::const_iterator i2Plus = i2 + 1;
1037 if (i2Plus != row2.end() && i2Plus->x() <= i1->x() + 1u)
1039 if (i2Plus->strength() >= i1->strength()) {
1045 const typename StrengthCandidateRow::const_iterator i2PlusPlus = i2Plus + 1;
1047 if (i2PlusPlus != row2.end() && i2PlusPlus->x() <= i1->x() + 1u)
1049 ocean_assert(i2PlusPlus->x() == i1->x() + 1u);
1051 if (i2PlusPlus->strength() >= i1->strength()) {
1058 if (positionCallback)
1061 TStrength preciseStrength;
1062 if ((*positionCallback)(i1->x(), y, i1->strength(), preciseX, preciseY, preciseStrength))
1064 localStrengthPositions.emplace_back(preciseX, preciseY, preciseStrength);
1078 if (i1Plus != row1.end()) {
1086 strengthPositions->insert(strengthPositions->end(), localStrengthPositions.begin(), localStrengthPositions.end());
This class holds the horizontal position and strength parameter of an interest pixel.
Definition NonMaximumSuppression.h:114
StrengthCandidate()
Creates a new candidate object.
Definition NonMaximumSuppression.h:392
const T & strength() const
Returns the strength parameter of this object.
Definition NonMaximumSuppression.h:414
unsigned int x() const
Returns the horizontal position of this candidate object.
Definition NonMaximumSuppression.h:408
T strength_
Strength parameter of this object.
Definition NonMaximumSuppression.h:147
unsigned int positionX_
Horizontal position of this object.
Definition NonMaximumSuppression.h:144
This class extends a 2D position by a third parameter storing a strength value.
Definition NonMaximumSuppression.h:51
static bool compareStrength(const StrengthPosition< TCoordinate, TStrength > &left, const StrengthPosition< TCoordinate, TStrength > &right)
Compares the strength value of two objects.
Definition NonMaximumSuppression.h:379
StrengthPosition()=default
Creates a new object with default strength parameter.
const TStrength & strength() const
Returns the strength parameter of this object.
Definition NonMaximumSuppression.h:371
TStrength strength_
Strength parameter of this object.
Definition NonMaximumSuppression.h:86
This class implements the possibility to find local maximum in a 2D array by applying a non-maximum-s...
Definition NonMaximumSuppression.h:41
NonMaximumSuppression< T > & operator=(NonMaximumSuppression< T > &&nonMaximumSuppression)
Move operator.
Definition NonMaximumSuppression.h:579
StrengthPositions< TCoordinate, TStrength > suppressNonMaximum(const unsigned int firstColumn, const unsigned int numberColumns, const unsigned int firstRow, const unsigned int numberRows, 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).
unsigned int width_
Width of this object.
Definition NonMaximumSuppression.h:354
StrengthCandidateRows rows_
All candidate rows.
Definition NonMaximumSuppression.h:357
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:541
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:769
void reset()
Removes the gathered non-maximum suppression information so that this object can be reused again (for...
Definition NonMaximumSuppression.h:570
unsigned int height() const
Returns the height of this object.
Definition NonMaximumSuppression.h:448
unsigned int yOffset() const
Returns the optional offset in the vertical direction.
Definition NonMaximumSuppression.h:454
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:897
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:868
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:498
std::vector< StrengthPosition< TCoordinate, TStrength > > StrengthPositions
Definition of a vector holding strength pixel positions.
Definition NonMaximumSuppression.h:93
void addCandidate(const unsigned int x, const unsigned int y, const T &strength)
Adds a new candidate to this object.
Definition NonMaximumSuppression.h:462
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:808
unsigned int width() const
Returns the width of this object.
Definition NonMaximumSuppression.h:442
NonMaximumSuppression(NonMaximumSuppression< T > &&nonMaximumSuppression) noexcept
Move constructor.
Definition NonMaximumSuppression.h:420
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...
std::vector< StrengthCandidate > StrengthCandidateRow
Definition of a vector holding strength candidate objects.
Definition NonMaximumSuppression.h:153
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:477
This class implements a container for callback functions.
Definition Callback.h:3456
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:325
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
void clear()
Clears this object, the specified index shift will be untouched.
Definition ShiftVector.h:658
std::ptrdiff_t Index
Definition of an element index.
Definition ShiftVector.h:38
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< Indices32 > IndexGroups32
Definition of a vector holding 32 bit indices, so we have groups of indices.
Definition Base.h:102
std::vector< Index32 > Indices32
Definition of a vector holding 32 bit index values.
Definition Base.h:96
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