8#ifndef META_OCEAN_CV_NON_MAXIMUM_SUPPRESSION_H
9#define META_OCEAN_CV_NON_MAXIMUM_SUPPRESSION_H
50 template <
typename TCoordinate,
typename TStrength>
72 inline const TStrength&
strength()
const;
81 template <
bool tLeftLargerThanRight = true>
93 template <
typename TCoordinate,
typename TStrength>
106 template <
typename TCoordinate,
typename TStrength>
134 inline unsigned int x()
const;
187 inline unsigned int width()
const;
193 inline unsigned int height()
const;
199 inline unsigned int yOffset()
const;
209 inline void addCandidate(
const unsigned int x,
const unsigned int y,
const T& strength);
224 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);
247 template <
typename TCoordinate,
typename TStrength,
bool tStrictMaximum = true>
293 template <
typename TCoordinate,
typename TStrength,
bool tStrictMaximum>
306 template <
typename TFloat>
319 template <
typename TFloat>
335 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);
349 template <
typename TCoordinate,
typename TStrength,
bool tStrictMaximum>
362template <
typename TCoordinate,
typename TStrength>
371template <
typename TCoordinate,
typename TStrength>
378template <
typename TCoordinate,
typename TStrength>
379template <
bool tLeftLargerThanRight>
382 if constexpr (tLeftLargerThanRight)
423 *
this = std::move(nonMaximumSuppression);
428 width_(nonMaximumSuppression.width_),
429 rows_(nonMaximumSuppression.rows_)
469 if (
rows_[y].empty())
471 rows_[y].reserve(128);
474 rows_[y].emplace_back(x, strength);
478void 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)
480 ocean_assert(values !=
nullptr);
482 ocean_assert(firstColumn + numberColumns <=
width_);
486 const unsigned int valuesStrideElements =
width_ + valuesPaddingElements;
490 worker->
executeFunction(
Worker::Function::create(*
this, &
NonMaximumSuppression<T>::addCandidatesSubset, values, valuesStrideElements, firstColumn, numberColumns, &minimalThreshold, 0u, 0u), firstRow, numberRows, 5u, 6u, 20u);
494 addCandidatesSubset(values, valuesStrideElements, firstColumn, numberColumns, &minimalThreshold, firstRow, numberRows);
505 while (!suppressionRow.empty())
507 if (suppressionRow.back().x() >= x)
509 suppressionRow.pop_back();
519template <
typename TCoordinate,
typename TStrength,
bool tStrictMaximum>
522 ocean_assert(firstColumn + numberColumns <=
width_);
531 worker->
executeFunction(
Worker::Function::create(*
this, &
NonMaximumSuppression<T>::suppressNonMaximumSubset<TCoordinate, TStrength, tStrictMaximum>, &result, firstColumn, numberColumns, &lock, positionCallback, 0u, 0u), firstRow, numberRows, 5u, 6u, 3u);
535 suppressNonMaximumSubset<TCoordinate, TStrength, tStrictMaximum>(&result, firstColumn, numberColumns,
nullptr, positionCallback, firstRow, numberRows);
544 ocean_assert(firstColumn + numberColumns <=
width_);
545 ocean_assert(firstRow + numberRows <= (
unsigned int)(
rows_.
endIndex()));
547 const unsigned int endColumn = firstColumn + numberColumns;
549 for (
unsigned int y = firstRow; y < firstRow + numberRows; ++y)
555 if (candidate.x() < firstColumn)
560 if (candidate.x() >= endColumn)
565 strengthPositions.emplace_back(candidate.x(), y, candidate.strength());
582 if (
this != &nonMaximumSuppression)
585 nonMaximumSuppression.width_ = 0u;
587 rows_ = std::move(nonMaximumSuppression.rows_);
596 if (
this != &nonMaximumSuppression)
606template <
typename TCoordinate,
typename TStrength,
bool tStrictMaximum>
614 const unsigned int horizontalBins = std::max(1u, (
width + binSize - 1u) / binSize);
615 const unsigned int verticalBins = std::max(1u, (
height + binSize - 1u) / binSize);
617 ocean_assert(binSize * horizontalBins >=
width);
618 ocean_assert(binSize * verticalBins >=
height);
624 for (
size_t n = 0; n < strengthPositions.size(); ++n)
628 ocean_assert((
unsigned int)(position.
x()) <
width);
629 ocean_assert((
unsigned int)(position.
y()) <
height);
631 const unsigned int xBin = (
unsigned int)(position.
x()) / binSize;
632 const unsigned int yBin = (
unsigned int)(position.
y()) / binSize;
634 ocean_assert(xBin <= horizontalBins);
635 ocean_assert(yBin <= verticalBins);
637 indexGroups[yBin * horizontalBins + xBin].emplace_back(
Index32(n));
640 std::vector<uint8_t> validPositions(strengthPositions.size(), 1u);
644 for (
size_t nCandidate = 0; nCandidate < strengthPositions.size(); ++nCandidate)
646 if (validPositions[nCandidate] == 0u)
654 const unsigned int xCandidateBin = (
unsigned int)(candidatePosition.
x()) / binSize;
655 const unsigned int yCandidateBin = (
unsigned int)(candidatePosition.
y()) / binSize;
657 ocean_assert(xCandidateBin <= horizontalBins);
658 ocean_assert(yCandidateBin <= verticalBins);
660 bool checkNextCandidate =
false;
662 for (
unsigned int yBin = (
unsigned int)(max(0,
int(yCandidateBin) - 1)); !checkNextCandidate && yBin < min(yCandidateBin + 2u, verticalBins); ++yBin)
664 for (
unsigned int xBin = (
unsigned int)(max(0,
int(xCandidateBin) - 1)); !checkNextCandidate && xBin < min(xCandidateBin + 2u, horizontalBins); ++xBin)
666 const Indices32& indices = indexGroups[yBin * horizontalBins + xBin];
668 for (
const Index32& nTest : indices)
670 if (nTest ==
Index32(nCandidate))
679 if (candidatePosition.
sqrDistance(testPosition) <= sqrRadius)
683 validPositions[nTest] = 0u;
687 validPositions[nCandidate] = 0u;
689 checkNextCandidate =
true;
696 if constexpr (tStrictMaximum)
700 validPositions[nCandidate] = 0u;
701 validPositions[nTest] = 0u;
703 checkNextCandidate =
true;
710 if (candidatePosition.
y() < testPosition.
y() || (candidatePosition.
y() == testPosition.
y() && candidatePosition.
x() < testPosition.
x()))
714 validPositions[nCandidate] = 0u;
716 checkNextCandidate =
true;
721 ocean_assert(testPosition.
y() < candidatePosition.
y() || (testPosition.
y() == candidatePosition.
y() && testPosition.
x() < candidatePosition.
x()));
725 validPositions[nTest] = 0u;
736 remainingPositions.reserve(strengthPositions.size());
740 ocean_assert(validIndices->empty());
742 validIndices->clear();
743 validIndices->reserve(strengthPositions.size());
745 for (
size_t n = 0; n < validPositions.size(); ++n)
747 if (validPositions[n])
749 remainingPositions.emplace_back(strengthPositions[n]);
750 validIndices->emplace_back(
Index32(n));
756 for (
size_t n = 0; n < validPositions.size(); ++n)
758 if (validPositions[n])
760 remainingPositions.emplace_back(strengthPositions[n]);
765 return remainingPositions;
769template <
typename TFloat>
772 static_assert(std::is_floating_point<TFloat>::value,
"Invalid floating point data type!");
785 const TFloat df = (TFloat(rightValue) - TFloat(leftValue)) * TFloat(0.5);
788 const TFloat dff = TFloat(leftValue) + TFloat(rightValue) - TFloat(middleValue) * TFloat(2);
792 location = TFloat(0);
796 const TFloat x = -df / dff;
798 if (x < TFloat(-1) || x > TFloat(1))
808template <
typename TFloat>
811 static_assert(std::is_floating_point<TFloat>::value,
"Invalid floating point data type!");
813 const T& value00 = topValues[0];
814 const T& value01 = topValues[1];
815 const T& value02 = topValues[2];
817 const T& value10 = centerValues[0];
818 const T& value11 = centerValues[1];
819 const T& value12 = centerValues[2];
821 const T& value20 = bottomValues[0];
822 const T& value21 = bottomValues[1];
823 const T& value22 = bottomValues[2];
827 ocean_assert(value11 >= value00 && value11 >= value01 && value11 >= value02);
828 ocean_assert(value11 >= value10 && value11 >= value12);
829 ocean_assert(value11 >= value20 && value11 >= value21 && value11 >= value22);
833 const TFloat dx = TFloat(value12 - value10) * TFloat(0.5);
834 const TFloat dy = TFloat(value21 - value01) * TFloat(0.5);
837 const TFloat dxx = TFloat(value12 + value10 - value11 * TFloat(2));
838 const TFloat dyy = TFloat(value21 + value01 - value11 * TFloat(2));
844 const TFloat dxy = TFloat(value22 + value00 - value20 - value02) * TFloat(0.25);
846 const TFloat denominator = dxx * dyy - dxy * dxy;
854 const TFloat factor = TFloat(1) / denominator;
856 const TFloat offsetX = -(dyy * dx - dxy * dy) * factor;
857 const TFloat offsetY = -(dxx * dy - dxy * dx) * factor;
859 if (offsetX < TFloat(-1) || offsetX > TFloat(1) || offsetY < TFloat(-1) || offsetY > TFloat(1))
869void 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)
871 ocean_assert(values !=
nullptr);
872 ocean_assert(valuesStrideElements >=
width_);
873 ocean_assert(firstColumn + numberColumns <=
width_);
878 const T localThreshold = *minimalThreshold;
880 values += firstRow * valuesStrideElements;
882 for (
unsigned int y = firstRow; y < firstRow + numberRows; ++y)
884 for (
unsigned int x = firstColumn; x < firstColumn + numberColumns; ++x)
886 if (values[x] >= localThreshold)
892 values += valuesStrideElements;
897template <
typename TCoordinate,
typename TStrength,
bool tStrictMaximum>
900 ocean_assert(strengthPositions);
902 ocean_assert(firstColumn + numberColumns <=
width_);
904 ocean_assert(firstRow + numberRows <= (
unsigned int)
rows_.
endIndex());
906 if (numberColumns < 3u || numberRows < 3u)
911 const unsigned int firstCenterColumn = max(1u, firstColumn);
912 const unsigned int endCenterColumn = min(firstColumn + numberColumns,
width_ - 1u);
914 const unsigned int firstCenterRow = max((
unsigned int)
rows_.
firstIndex() + 1u, firstRow);
915 const unsigned int endCenterRow = min(firstRow + numberRows, (
unsigned int)
rows_.
lastIndex());
917 ocean_assert(firstCenterRow >= 1u);
920 localStrengthPositions.reserve(100);
922 for (
unsigned int y = firstCenterRow; y < endCenterRow; ++y)
928 typename StrengthCandidateRow::const_iterator i0 = row0.
begin();
929 typename StrengthCandidateRow::const_iterator i2 = row2.begin();
931 typename StrengthCandidateRow::const_iterator i1Minus = row1.end();
932 typename StrengthCandidateRow::const_iterator i1Plus = row1.size() > 1 ? row1.begin() + 1 : row1.end();
934 for (
typename StrengthCandidateRow::const_iterator i1 = row1.begin(); i1 != row1.end(); ++i1)
936 ocean_assert(i1->x() >= 0u && i1->x() + 1u <=
width_);
939 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())))
942 if (i1Plus == row1.end() || i1Plus->x() != i1->x() + 1u || i1Plus->strength() < i1->strength())
945 while (i0 != row0.end())
947 if (i0->x() + 1u < i1->x())
958 ocean_assert(i0 == row0.end() || i0->x() + 1u >= i1->x());
960 if (i0 != row0.end() && i0->x() <= i1->x() + 1u)
962 ocean_assert(i0->x() + 1u == i1->x() || i0->x() == i1->x() || i0->x() - 1u == i1->x());
964 if ((tStrictMaximum && i0->strength() >= i1->strength()) || (!tStrictMaximum && i0->strength() > i1->strength()))
971 const typename StrengthCandidateRow::const_iterator i0Plus = i0 + 1;
973 if (i0Plus != row0.end() && i0Plus->x() <= i1->x() + 1u)
975 if ((tStrictMaximum && i0Plus->strength() >= i1->strength()) || (!tStrictMaximum && i0Plus->strength() > i1->strength()))
982 const typename StrengthCandidateRow::const_iterator i0PlusPlus = i0Plus + 1;
984 if (i0PlusPlus != row0.end() && i0PlusPlus->x() <= i1->x() + 1u)
986 ocean_assert(i0PlusPlus->x() == i1->x() + 1u);
988 if ((tStrictMaximum && i0PlusPlus->strength() >= i1->strength()) || (!tStrictMaximum && i0PlusPlus->strength() > i1->strength()))
998 while (i2 != row2.end())
1000 if (i2->x() + 1u < i1->x())
1011 ocean_assert(i2 == row2.end() || i2->x() + 1u >= i1->x());
1013 if (i2 != row2.end() && i2->x() <= i1->x() + 1u)
1015 ocean_assert(i2->x() + 1u == i1->x() || i2->x() == i1->x() || i2->x() - 1u == i1->x());
1017 if (i2->x() + 1u == i1->x())
1021 if ((tStrictMaximum && i2->strength() >= i1->strength()) || (!tStrictMaximum && i2->strength() > i1->strength()))
1028 if (i2->strength() >= i1->strength())
1036 const typename StrengthCandidateRow::const_iterator i2Plus = i2 + 1;
1038 if (i2Plus != row2.end() && i2Plus->x() <= i1->x() + 1u)
1040 if (i2Plus->strength() >= i1->strength())
1047 const typename StrengthCandidateRow::const_iterator i2PlusPlus = i2Plus + 1;
1049 if (i2PlusPlus != row2.end() && i2PlusPlus->x() <= i1->x() + 1u)
1051 ocean_assert(i2PlusPlus->x() == i1->x() + 1u);
1053 if (i2PlusPlus->strength() >= i1->strength())
1061 if (positionCallback)
1064 TStrength preciseStrength;
1065 if ((*positionCallback)(i1->x(), y, i1->strength(), preciseX, preciseY, preciseStrength))
1067 localStrengthPositions.emplace_back(preciseX, preciseY, preciseStrength);
1081 if (i1Plus != row1.end())
1090 strengthPositions->insert(strengthPositions->end(), localStrengthPositions.begin(), localStrengthPositions.end());
This class holds the horizontal position and strength parameter of an interest pixel.
Definition NonMaximumSuppression.h:115
StrengthCandidate()
Creates a new candidate object.
Definition NonMaximumSuppression.h:393
const T & strength() const
Returns the strength parameter of this object.
Definition NonMaximumSuppression.h:415
unsigned int x() const
Returns the horizontal position of this candidate object.
Definition NonMaximumSuppression.h:409
T strength_
Strength parameter of this object.
Definition NonMaximumSuppression.h:148
unsigned int positionX_
Horizontal position of this object.
Definition NonMaximumSuppression.h:145
This class extends a 2D position by a third parameter storing a strength value.
Definition NonMaximumSuppression.h:52
static bool compareStrength(const StrengthPosition< TCoordinate, TStrength > &left, const StrengthPosition< TCoordinate, TStrength > &right)
Compares the strength value of two objects.
Definition NonMaximumSuppression.h:380
StrengthPosition()=default
Creates a new object with default strength parameter.
const TStrength & strength() const
Returns the strength parameter of this object.
Definition NonMaximumSuppression.h:372
TStrength strength_
Strength parameter of this object.
Definition NonMaximumSuppression.h:87
This class implements the possibility to find local maximum in a 2D array by applying a non-maximum-s...
Definition NonMaximumSuppression.h:42
NonMaximumSuppression< T > & operator=(NonMaximumSuppression< T > &&nonMaximumSuppression)
Move operator.
Definition NonMaximumSuppression.h:580
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:355
StrengthCandidateRows rows_
All candidate rows.
Definition NonMaximumSuppression.h:358
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:542
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:770
void reset()
Removes the gathered non-maximum suppression information so that this object can be reused again (for...
Definition NonMaximumSuppression.h:571
unsigned int height() const
Returns the height of this object.
Definition NonMaximumSuppression.h:449
unsigned int yOffset() const
Returns the optional offset in the vertical direction.
Definition NonMaximumSuppression.h:455
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:898
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:869
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:499
std::vector< StrengthPosition< TCoordinate, TStrength > > StrengthPositions
Definition of a vector holding strength pixel positions.
Definition NonMaximumSuppression.h:94
void addCandidate(const unsigned int x, const unsigned int y, const T &strength)
Adds a new candidate to this object.
Definition NonMaximumSuppression.h:463
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:809
unsigned int width() const
Returns the width of this object.
Definition NonMaximumSuppression.h:443
NonMaximumSuppression(NonMaximumSuppression< T > &&nonMaximumSuppression) noexcept
Move constructor.
Definition NonMaximumSuppression.h:421
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:154
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:478
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