8 #ifndef META_OCEAN_CV_SEGMENTATION_MASK_ANALYZER_H
9 #define META_OCEAN_CV_SEGMENTATION_MASK_ANALYZER_H
31 namespace Segmentation
61 inline MaskBlock(
const PixelPosition& position,
const unsigned int id,
const unsigned int size,
const bool border =
true);
73 inline unsigned int id()
const;
79 inline unsigned int size()
const;
85 inline bool border()
const;
92 inline bool operator<(
const MaskBlock& block)
const;
150 static constexpr uint32_t distanceVerticalHorizontalC = 1u;
152 static constexpr uint32_t distanceDiagonalC = 1u;
155 static constexpr uint32_t distanceVerticalHorizontalL1 = 1u;
157 static constexpr uint32_t distanceDiagonalL1 = 2u;
160 static constexpr
float distanceVerticalHorizontalL2 = 0.95509f;
162 static constexpr
float distanceDiagonalL2 = 1.3693f;
194 inline SweepMaskIsland(
const unsigned int currentRow,
const unsigned int start,
const unsigned int end);
209 inline bool hasIntersection(
const unsigned int start,
const unsigned int end,
const bool useNeighborhood4)
const;
217 inline void addSegment(
const unsigned int currentRow,
const unsigned int start,
const unsigned int end);
223 inline void nextRow();
263 template <
bool tMaskValueIsEqual,
typename T>
264 static inline bool hasMaskNeighbor4(
const T* mask,
const unsigned int width,
const unsigned int height,
const unsigned int maskPaddingElements,
const PixelPosition& position,
const T testValue);
279 template <
bool tMaskValueIsEqual,
typename T>
280 static inline bool hasMaskNeighbor4Center(
const T* mask,
const unsigned int width,
const unsigned int height,
const unsigned int maskPaddingElements,
const PixelPosition& position,
const T testValue);
295 template <
bool tMaskValueIsEqual,
typename T>
296 static inline bool hasMaskNeighbor5(
const T* mask,
const unsigned int width,
const unsigned int height,
const unsigned int maskPaddingElements,
const PixelPosition& position,
const T testValue);
311 template <
bool tMaskValueIsEqual,
typename T>
312 static inline bool hasMaskNeighbor5Center(
const T* mask,
const unsigned int width,
const unsigned int height,
const unsigned int maskPaddingElements,
const PixelPosition& position,
const T testValue);
327 template <
bool tMaskValueIsEqual,
typename T>
328 static inline bool hasMaskNeighbor8(
const T* mask,
const unsigned int width,
const unsigned int height,
const unsigned int maskPaddingElements,
const PixelPosition& position,
const T testValue);
343 template <
bool tMaskValueIsEqual,
typename T>
344 static inline bool hasMaskNeighbor8Center(
const T* mask,
const unsigned int width,
const unsigned int height,
const unsigned int maskPaddingElements,
const PixelPosition& position,
const T testValue);
359 template <
bool tMaskValueIsEqual,
typename T>
360 static inline bool hasMaskNeighbor9(
const T* mask,
const unsigned int width,
const unsigned int height,
const unsigned int maskPaddingElements,
const PixelPosition& position,
const T testValue);
375 template <
bool tMaskValueIsEqual,
typename T>
376 static inline bool hasMaskNeighbor9Center(
const T* mask,
const unsigned int width,
const unsigned int height,
const unsigned int maskPaddingElements,
const PixelPosition& position,
const T testValue);
391 static inline bool computeChessboardDistanceTransform8Bit(
const uint8_t* source,
const uint32_t width,
const uint32_t height, uint32_t* target, uint32_t* buffer =
nullptr,
const uint8_t referenceValue = 0u,
const uint32_t sourcePaddingElements = 0u,
const uint32_t targetPaddingElements = 0u);
406 static inline bool computeL1DistanceTransform8Bit(
const uint8_t* source,
const uint32_t width,
const uint32_t height, uint32_t* target, uint32_t* buffer =
nullptr,
const uint8_t referenceValue = 0u,
const uint32_t sourcePaddingElements = 0u,
const uint32_t targetPaddingElements = 0u);
421 static inline bool computeL2DistanceTransform8Bit(
const uint8_t* source,
const uint32_t width,
const uint32_t height,
float* target, uint32_t* buffer =
nullptr,
const uint8_t referenceValue = 0u,
const uint32_t sourcePaddingElements = 0u,
const uint32_t targetPaddingElements = 0u);
436 static void analyzeMaskSeparation8Bit(
const uint8_t* mask,
const unsigned int width,
const unsigned int height,
const unsigned int maskPaddingElements, uint32_t* separation,
const unsigned int separationPaddingElements,
MaskBlocks& blocks);
451 static void analyzeNonMaskSeparation8Bit(
const uint8_t* mask,
const unsigned int width,
const unsigned int height,
const unsigned int maskPaddingElements, uint32_t* separation,
const unsigned int separationPaddingElements,
MaskBlocks& blocks);
559 static inline bool isOuterContour(
const uint8_t* mask,
const unsigned int width,
const PixelContour& contour,
const uint8_t maskValue = 0u);
604 static PixelBoundingBoxes detectBoundingBoxes(
const uint8_t*
const mask,
const unsigned int width,
const unsigned int height,
const unsigned int maskPaddingElements,
const uint8_t maskValue,
const bool useNeighborhood4 =
true);
622 static void findBorderPixels4Subset(
const uint8_t* mask,
const unsigned int width,
const unsigned int height,
const unsigned int maskPaddingElements,
PixelPositions* borderPixelsArray,
const uint8_t nonMaskValue,
const unsigned int firstColumn,
const unsigned int numberColumns,
const unsigned int firstRow,
const unsigned int numberRows,
const unsigned int threadId);
638 static void findBorderPixels8Subset(
const uint8_t* mask,
const unsigned int width,
const unsigned int height,
const unsigned int maskPaddingElements,
PixelPositions* borderPixelsArray,
const uint8_t nonMaskValue,
const unsigned int firstColumn,
const unsigned int numberColumns,
const unsigned int firstRow,
const unsigned int numberRows,
const unsigned int threadId);
658 static void determineDistancesToBorder8BitSubset(uint8_t* mask,
const unsigned int width,
const unsigned int height,
const unsigned int maskPaddingElements,
const uint8_t searchValue,
const uint8_t resultValue,
const unsigned int firstColumn,
const unsigned int numberColumns,
const unsigned int firstRow,
const unsigned int numberRows);
675 template <
typename TDistanceType>
676 static bool computeDistanceTransform8Bit(
const uint8_t* source,
const uint32_t width,
const uint32_t height, TDistanceType* target, uint32_t* buffer,
const TDistanceType distanceVerticalHorizontal,
const TDistanceType distanceDiagonal,
const uint8_t referenceValue = 0u,
const uint32_t sourcePaddingElements = 0u,
const uint32_t targetPaddingElements = 0u);
685 template <
typename T,
bool tMaskValueIsEqual>
686 static inline bool compareValues(
const T valueA,
const T valueB);
698 blockPosition(position),
708 return blockPosition;
733 ocean_assert(start < end);
735 previousRowSegments_.reserve(8);
736 currentRowSegments_.reserve(8);
738 addSegment(currentRow, start, end);
743 boundingBox_ = boundingBox_ || sweepMaskIsland.
boundingBox_;
751 ocean_assert(start < end);
753 for (
const RowSegment& previousRowSegment : previousRowSegments_)
755 const unsigned int& previousSegmentStart = previousRowSegment.first;
756 const unsigned int& previousSegmentEnd = previousRowSegment.second;
758 if ((useNeighborhood4 && start < previousSegmentEnd && end > previousSegmentStart) || (!useNeighborhood4 && start <= previousSegmentEnd && end >= previousSegmentStart))
769 ocean_assert(start < end);
771 currentRowSegments_.emplace_back(start, end);
779 std::swap(currentRowSegments_, previousRowSegments_);
781 currentRowSegments_.clear();
789 template <
bool tMaskValueIsEqual,
typename T>
792 ocean_assert(mask !=
nullptr && width != 0u && height != 0u);
793 ocean_assert(position.
x() < width && position.
y() < height);
795 ocean_assert((position.
x() - 1u < width - 2u && position.
y() - 1u < height - 2u) == (position.
x() >= 1u && position.
x() + 1u < width && position.
y() >= 1u && position.
y() + 1u < height));
796 if (position.
x() - 1u < width - 2u && position.
y() - 1u < height - 2u)
798 return hasMaskNeighbor4Center<tMaskValueIsEqual, T>(mask, width, height, maskPaddingElements, position, testValue);
801 const unsigned int maskStrideElements = width + maskPaddingElements;
803 mask += position.
y() * maskStrideElements + position.
x();
805 if (position.
x() != 0u && compareValues<T, tMaskValueIsEqual>(*(mask - 1), testValue))
810 if (position.
x() + 1u < width && compareValues<T, tMaskValueIsEqual>(*(mask + 1), testValue))
815 if (position.
y() != 0u && compareValues<T, tMaskValueIsEqual>(*(mask - maskStrideElements), testValue))
820 if (position.
y() + 1u < height && compareValues<T, tMaskValueIsEqual>(*(mask + maskStrideElements), testValue))
828 template <
bool tMaskValueIsEqual,
typename T>
831 ocean_assert(mask !=
nullptr && height >= 3u && height >= 3u);
832 ocean_assert(position.
x() >= 1u && position.
x() + 1u < width);
833 ocean_assert_and_suppress_unused(position.
y() >= 1u && position.
y() + 1u < height, height);
835 const unsigned int maskStrideElements = width + maskPaddingElements;
837 mask += position.
y() * maskStrideElements + position.
x();
839 return compareValues<T, tMaskValueIsEqual>(*(mask - 1), testValue)
840 || compareValues<T, tMaskValueIsEqual>(*(mask + 1), testValue)
841 || compareValues<T, tMaskValueIsEqual>(*(mask - maskStrideElements), testValue)
842 || compareValues<T, tMaskValueIsEqual>(*(mask + maskStrideElements), testValue);
845 template <
bool tMaskValueIsEqual,
typename T>
848 ocean_assert(mask !=
nullptr && width != 0u && height != 0u);
849 ocean_assert(position.
x() < width && position.
y() < height);
851 ocean_assert((position.
x() - 1u < width - 2u && position.
y() - 1u < height - 2u) == (position.
x() >= 1u && position.
x() + 1u < width && position.
y() >= 1u && position.
y() + 1u < height));
852 if (position.
x() - 1u < width - 2u && position.
y() - 1u < height - 2u)
854 return hasMaskNeighbor5Center<tMaskValueIsEqual, T>(mask, width, height, maskPaddingElements, position, testValue);
857 const unsigned int maskStrideElements = width + maskPaddingElements;
859 mask += position.
y() * maskStrideElements + position.
x();
861 if (compareValues<T, tMaskValueIsEqual>(*mask, testValue))
866 if (position.
x() != 0u && compareValues<T, tMaskValueIsEqual>(*(mask - 1), testValue))
871 if (position.
x() + 1u < width && compareValues<T, tMaskValueIsEqual>(*(mask + 1), testValue))
876 if (position.
y() != 0u && compareValues<T, tMaskValueIsEqual>(*(mask - maskStrideElements), testValue))
881 if (position.
y() + 1u < height && compareValues<T, tMaskValueIsEqual>(*(mask + maskStrideElements), testValue))
889 template <
bool tMaskValueIsEqual,
typename T>
892 ocean_assert(mask !=
nullptr && height >= 3u && height >= 3u);
893 ocean_assert(position.
x() >= 1u && position.
x() + 1u < width);
894 ocean_assert_and_suppress_unused(position.
y() >= 1u && position.
y() + 1u < height, height);
896 const unsigned int maskStrideElements = width + maskPaddingElements;
898 mask += position.
y() * maskStrideElements + position.
x();
900 return compareValues<T, tMaskValueIsEqual>(*mask, testValue)
901 || compareValues<T, tMaskValueIsEqual>(*(mask - 1), testValue)
902 || compareValues<T, tMaskValueIsEqual>(*(mask + 1), testValue)
903 || compareValues<T, tMaskValueIsEqual>(*(mask - maskStrideElements), testValue)
904 || compareValues<T, tMaskValueIsEqual>(*(mask + maskStrideElements), testValue);
907 template <
bool tMaskValueIsEqual,
typename T>
910 ocean_assert(mask !=
nullptr && width != 0u && height != 0u);
911 ocean_assert(position.
x() < width && position.
y() < height);
913 ocean_assert((position.
x() - 1u < width - 2u && position.
y() - 1u < height - 2u) == (position.
x() >= 1u && position.
x() + 1u < width && position.
y() >= 1u && position.
y() + 1u < height));
914 if (position.
x() - 1u < width - 2u && position.
y() - 1u < height - 2u)
916 return hasMaskNeighbor8Center<tMaskValueIsEqual, T>(mask, width, height, maskPaddingElements, position, testValue);
919 const unsigned int maskStrideElements = width + maskPaddingElements;
921 mask += position.
y() * maskStrideElements + position.
x();
923 if (position.
x() != 0u)
925 if (compareValues<T, tMaskValueIsEqual>(*(mask - 1), testValue))
930 if (position.
y() != 0u && compareValues<T, tMaskValueIsEqual>(*(mask - maskStrideElements - 1u), testValue))
935 if (position.
y() + 1u < height && compareValues<T, tMaskValueIsEqual>(*(mask + maskStrideElements - 1u), testValue))
941 if (position.
x() + 1u < width)
943 if (compareValues<T, tMaskValueIsEqual>(*(mask + 1), testValue))
948 if (position.
y() != 0u && compareValues<T, tMaskValueIsEqual>(*(mask - maskStrideElements + 1u), testValue))
953 if (position.
y() + 1u < height && compareValues<T, tMaskValueIsEqual>(*(mask + maskStrideElements + 1u), testValue))
959 if (position.
y() != 0u && compareValues<T, tMaskValueIsEqual>(*(mask - maskStrideElements), testValue))
964 if (position.
y() + 1u < height && compareValues<T, tMaskValueIsEqual>(*(mask + maskStrideElements), testValue))
972 template <
bool tMaskValueIsEqual,
typename T>
975 ocean_assert(mask !=
nullptr && height >= 3u && height >= 3u);
976 ocean_assert(position.
x() >= 1u && position.
x() + 1u < width);
977 ocean_assert_and_suppress_unused(position.
y() >= 1u && position.
y() + 1u < height, height);
979 const unsigned int maskStrideElements = width + maskPaddingElements;
981 mask += position.
y() * maskStrideElements + position.
x();
983 return compareValues<T, tMaskValueIsEqual>(*(mask - 1), testValue) || compareValues<T, tMaskValueIsEqual>(*(mask + 1), testValue)
984 || compareValues<T, tMaskValueIsEqual>(*(mask - maskStrideElements - 1), testValue) || compareValues<T, tMaskValueIsEqual>(*(mask - maskStrideElements), testValue) || compareValues<T, tMaskValueIsEqual>(*(mask - maskStrideElements + 1), testValue)
985 || compareValues<T, tMaskValueIsEqual>(*(mask + maskStrideElements - 1), testValue) || compareValues<T, tMaskValueIsEqual>(*(mask + maskStrideElements), testValue) || compareValues<T, tMaskValueIsEqual>(*(mask + maskStrideElements + 1), testValue);
988 template <
bool tMaskValueIsEqual,
typename T>
991 ocean_assert(mask !=
nullptr && width != 0u && height != 0u);
992 ocean_assert(position.
x() < width && position.
y() < height);
994 ocean_assert((position.
x() - 1u < width - 2u && position.
y() - 1u < height - 2u) == (position.
x() >= 1u && position.
x() + 1u < width && position.
y() >= 1u && position.
y() + 1u < height));
995 if (position.
x() - 1u < width - 2u && position.
y() - 1u < height - 2u)
997 return hasMaskNeighbor9Center<tMaskValueIsEqual, T>(mask, width, height, maskPaddingElements, position, testValue);
1000 const unsigned int maskStrideElements = width + maskPaddingElements;
1002 mask += position.
y() * maskStrideElements + position.
x();
1004 if (compareValues<T, tMaskValueIsEqual>(*mask, testValue))
1009 if (position.
x() != 0u)
1011 if (compareValues<T, tMaskValueIsEqual>(*(mask - 1), testValue))
1016 if (position.
y() != 0u && compareValues<T, tMaskValueIsEqual>(*(mask - maskStrideElements - 1u), testValue))
1021 if (position.
y() + 1u < height && compareValues<T, tMaskValueIsEqual>(*(mask + maskStrideElements - 1u), testValue))
1027 if (position.
x() + 1u < width)
1029 if (compareValues<T, tMaskValueIsEqual>(*(mask + 1), testValue))
1034 if (position.
y() != 0u && compareValues<T, tMaskValueIsEqual>(*(mask - maskStrideElements + 1u), testValue))
1039 if (position.
y() + 1u < height && compareValues<T, tMaskValueIsEqual>(*(mask + maskStrideElements + 1u), testValue))
1045 if (position.
y() != 0u && compareValues<T, tMaskValueIsEqual>(*(mask - maskStrideElements), testValue))
1050 if (position.
y() + 1u < height && compareValues<T, tMaskValueIsEqual>(*(mask + maskStrideElements), testValue))
1058 template <
bool tMaskValueIsEqual,
typename T>
1061 ocean_assert(mask !=
nullptr && height >= 3u && height >= 3u);
1062 ocean_assert(position.
x() >= 1u && position.
x() + 1u < width);
1063 ocean_assert_and_suppress_unused(position.
y() >= 1u && position.
y() + 1u < height, height);
1065 const unsigned int maskStrideElements = width + maskPaddingElements;
1067 mask += position.
y() * maskStrideElements + position.
x();
1069 return compareValues<T, tMaskValueIsEqual>(*(mask - 1), testValue) || compareValues<T, tMaskValueIsEqual>(*mask, testValue) || compareValues<T, tMaskValueIsEqual>(*(mask + 1), testValue)
1070 || compareValues<T, tMaskValueIsEqual>(*(mask - maskStrideElements - 1), testValue) || compareValues<T, tMaskValueIsEqual>(*(mask - maskStrideElements), testValue) || compareValues<T, tMaskValueIsEqual>(*(mask - maskStrideElements + 1), testValue)
1071 || compareValues<T, tMaskValueIsEqual>(*(mask + maskStrideElements - 1), testValue) || compareValues<T, tMaskValueIsEqual>(*(mask + maskStrideElements), testValue) || compareValues<T, tMaskValueIsEqual>(*(mask + maskStrideElements + 1), testValue);
1076 ocean_assert(mask && width >= 1u);
1077 ocean_assert(contour);
1081 ocean_assert(mostLeftPixel.
x() < width);
1082 return mostLeftPixel.
x() == 0u || (mask[mostLeftPixel.
y() * width + mostLeftPixel.
x() - 1u] != maskValue);
1090 inline bool MaskAnalyzer::computeL1DistanceTransform8Bit(
const uint8_t* source,
const uint32_t width,
const uint32_t height, uint32_t* target, uint32_t* buffer,
const uint8_t referenceValue,
const uint32_t sourcePaddingElements,
const uint32_t targetPaddingElements)
1095 inline bool MaskAnalyzer::computeL2DistanceTransform8Bit(
const uint8_t* source,
const uint32_t width,
const uint32_t height,
float* target, uint32_t* buffer,
const uint8_t referenceValue,
const uint32_t sourcePaddingElements,
const uint32_t targetPaddingElements)
1100 template <
typename TDistanceType>
1101 bool MaskAnalyzer::computeDistanceTransform8Bit(
const uint8_t* source,
const uint32_t width,
const uint32_t height, TDistanceType* target, uint32_t* buffer,
const TDistanceType distanceVerticalHorizontal,
const TDistanceType distanceDiagonal,
const uint8_t referenceValue,
const uint32_t sourcePaddingElements,
const uint32_t targetPaddingElements)
1103 if (source ==
nullptr || width == 0u || height == 0u || target ==
nullptr)
1105 ocean_assert(
false &&
"Invalid input data");
1109 const uint32_t bufferWidth = width + 2u;
1110 const uint32_t bufferHeight = height + 2u;
1114 if (buffer ==
nullptr)
1116 memory =
Memory(bufferWidth * bufferHeight *
sizeof(uint32_t));
1117 buffer = memory.
data<uint32_t>();
1120 if (distanceVerticalHorizontal <= 0 || distanceDiagonal <= 0)
1122 ocean_assert(
false &&
"Distance values must be positive");
1126 const uint32_t sourceStrideElements = width + sourcePaddingElements;
1127 const uint32_t targetStrideElements = width + targetPaddingElements;
1129 const uint8_t*
const sourceEnd = source + (height - 1u) * sourceStrideElements + width;
1130 const TDistanceType*
const targetEnd = target + (height - 1u) * targetStrideElements + width;
1132 const uint32_t* bufferEnd = buffer + bufferHeight * bufferWidth;
1135 constexpr TDistanceType fixedPointScalingFactor = TDistanceType(std::is_floating_point<TDistanceType>::value ? 1u << 16u : 1u);
1136 constexpr TDistanceType fixedPointUnscalingFactor = TDistanceType(1) / fixedPointScalingFactor;
1137 static_assert(fixedPointScalingFactor > 0 && fixedPointUnscalingFactor > 0,
"Fixed-point scaling factors must be positive");
1138 static_assert(std::is_floating_point<TDistanceType>::value || (fixedPointScalingFactor == TDistanceType(1u) && fixedPointUnscalingFactor == TDistanceType(1)),
"For integral types scaling factor must be 1");
1140 const uint32_t distanceVerticalHorizontal_q = uint32_t((distanceVerticalHorizontal * fixedPointScalingFactor));
1141 const uint32_t distanceDiagonal_q = uint32_t(distanceDiagonal * fixedPointScalingFactor);
1148 uint32_t* bufferTopRow = buffer;
1149 uint32_t* bufferBottomRow = buffer + (bufferHeight - 1u) * bufferWidth;
1150 for (uint32_t x = 0u; x < bufferWidth; ++x)
1152 *bufferTopRow = boundaryValue;
1153 *bufferBottomRow = boundaryValue;
1181 bool foundReferenceValue =
false;
1184 for (uint32_t y = 0u; y < height; ++y)
1186 const uint8_t* sourceRow = source + y * sourceStrideElements;
1187 uint32_t* bufferRow = buffer + (y + 1u) * bufferWidth;
1190 *bufferRow = boundaryValue;
1191 *(bufferRow + bufferWidth - 1u) = boundaryValue;
1195 for (uint32_t x = 0u; x < width; ++x)
1197 ocean_assert_and_suppress_unused(sourceRow >= source && sourceRow < sourceEnd, sourceEnd);
1198 ocean_assert_and_suppress_unused(bufferRow - bufferWidth - 1u >= buffer && bufferRow < bufferEnd, bufferEnd);
1200 if (*sourceRow == referenceValue)
1203 foundReferenceValue =
true;
1207 const uint32_t neighbor0_q = *(bufferRow - bufferWidth - 1u) + distanceDiagonal_q;
1208 const uint32_t neighbor1_q = *(bufferRow - bufferWidth) + distanceVerticalHorizontal_q;
1209 const uint32_t neighbor2_q = *(bufferRow - bufferWidth + 1u) + distanceDiagonal_q;
1210 const uint32_t neighbor3_q = *(bufferRow - 1u) + distanceVerticalHorizontal_q;
1212 *bufferRow = std::min(std::min(neighbor0_q, neighbor1_q), std::min(neighbor2_q, neighbor3_q));
1220 if (foundReferenceValue ==
false)
1226 for (uint32_t y = height - 1u; y < height; --y)
1228 TDistanceType* targetRow = target + y * targetStrideElements + width - 1u;
1229 uint32_t* bufferRow = buffer + (y + 1u) * bufferWidth + width;
1231 for (uint32_t x = width - 1u; x < width; --x)
1233 ocean_assert_and_suppress_unused(targetRow >= target && targetRow < targetEnd, targetEnd);
1234 ocean_assert(bufferRow >= buffer && bufferRow + bufferWidth + 1u < bufferEnd);
1236 const uint32_t neighbor4_q = *(bufferRow + 1u) + distanceVerticalHorizontal_q;
1237 const uint32_t neighbor5_q = *(bufferRow + bufferWidth - 1u) + distanceDiagonal_q;
1238 const uint32_t neighbor6_q = *(bufferRow + bufferWidth) + distanceVerticalHorizontal_q;
1239 const uint32_t neighbor7_q = *(bufferRow + bufferWidth + 1u) + distanceDiagonal_q;
1241 *bufferRow = std::min(*bufferRow, std::min(std::min(neighbor4_q, neighbor5_q), std::min(neighbor6_q, neighbor7_q)));
1243 if (std::is_floating_point<TDistanceType>::value)
1245 *targetRow = TDistanceType(*bufferRow) * fixedPointUnscalingFactor;
1249 *targetRow = TDistanceType(*bufferRow);
1261 template <
typename T,
bool tMaskValueIsEqual>
1264 if constexpr (tMaskValueIsEqual)
1266 return valueA == valueB;
1270 return valueA != valueB;
T y() const
Returns the vertical coordinate position of this object.
Definition: PixelPosition.h:470
T x() const
Returns the horizontal coordinate position of this object.
Definition: PixelPosition.h:458
This class implements a simple information for a block/area of mask pixels.
Definition: segmentation/MaskAnalyzer.h:46
unsigned int id() const
Returns the id of this block.
Definition: segmentation/MaskAnalyzer.h:711
PixelPosition blockPosition
One position inside the mask block.
Definition: segmentation/MaskAnalyzer.h:97
unsigned int size() const
Returns the size of this block in pixel.
Definition: segmentation/MaskAnalyzer.h:716
MaskBlock()
Creates an invalid block object.
Definition: segmentation/MaskAnalyzer.h:689
unsigned int blockSize
Size of the mask block in pixel.
Definition: segmentation/MaskAnalyzer.h:103
bool operator<(const MaskBlock &block) const
Returns whether the size of this block is smaller than the size of the given block.
Definition: segmentation/MaskAnalyzer.h:726
bool border() const
Returns whether this block intersects with the image border.
Definition: segmentation/MaskAnalyzer.h:721
bool blockBorder
True, if image border block.
Definition: segmentation/MaskAnalyzer.h:106
unsigned int blockId
Id of the mask block.
Definition: segmentation/MaskAnalyzer.h:100
const PixelPosition & position() const
Returns the one position of this block.
Definition: segmentation/MaskAnalyzer.h:706
This class implements a mask island used in a sweep algorithm.
Definition: segmentation/MaskAnalyzer.h:168
void join(const SweepMaskIsland &sweepMask)
Joins a given sweep mask island with this island.
Definition: segmentation/MaskAnalyzer.h:741
RowSegments previousRowSegments_
The segments located in the previous row.
Definition: segmentation/MaskAnalyzer.h:234
RowSegments currentRowSegments_
The segments located in the current row.
Definition: segmentation/MaskAnalyzer.h:237
SweepMaskIsland()=default
Default constructor.
const PixelBoundingBox & boundingBox() const
Returns the bounding box of this mask island.
Definition: segmentation/MaskAnalyzer.h:784
PixelBoundingBox boundingBox_
The bounding box of this mask.
Definition: segmentation/MaskAnalyzer.h:240
void addSegment(const unsigned int currentRow, const unsigned int start, const unsigned int end)
Adds a new row segment to this island.
Definition: segmentation/MaskAnalyzer.h:767
bool hasIntersection(const unsigned int start, const unsigned int end, const bool useNeighborhood4) const
Checks whether this island intersects with a given row segment.
Definition: segmentation/MaskAnalyzer.h:749
void nextRow()
Ends segment handling for the current row and prepares the mask for the next row.
Definition: segmentation/MaskAnalyzer.h:777
std::pair< unsigned int, unsigned int > RowSegment
Definition of a pair combining the horizontal start position (inclusive), and the horizontal end posi...
Definition: segmentation/MaskAnalyzer.h:174
std::vector< RowSegment > RowSegments
Definition of a vector holding row segments.
Definition: segmentation/MaskAnalyzer.h:179
This class implements functions analyzing masks, determining specific pixels in relation to masks and...
Definition: segmentation/MaskAnalyzer.h:39
static void analyzeNonMaskSeparation8Bit(const uint8_t *mask, const unsigned int width, const unsigned int height, const unsigned int maskPaddingElements, uint32_t *separation, const unsigned int separationPaddingElements, MaskBlocks &blocks)
Analyzes an 8 bit binary mask frame and separates the pixels into individual blocks of not joined sub...
static constexpr uint32_t distanceDiagonalC
Chessboard distance for diagonal steps.
Definition: segmentation/MaskAnalyzer.h:152
static constexpr float distanceDiagonalL2
L2 distance for diagonal steps.
Definition: segmentation/MaskAnalyzer.h:162
static bool computeL1DistanceTransform8Bit(const uint8_t *source, const uint32_t width, const uint32_t height, uint32_t *target, uint32_t *buffer=nullptr, const uint8_t referenceValue=0u, const uint32_t sourcePaddingElements=0u, const uint32_t targetPaddingElements=0u)
Computes a per pixel L1 distance to the nearest pixel with a reference value using 3-by-3 neighborhoo...
Definition: segmentation/MaskAnalyzer.h:1090
static unsigned int countMaskPixels(const uint8_t *mask, const unsigned int width, const unsigned int height, const unsigned int maskPaddingElements, const PixelBoundingBox &boundingBox=PixelBoundingBox(), const uint8_t nonMaskValue=0xFFu)
Counts the number of mask pixels.
static void findOutline4(const uint8_t *mask, const unsigned int width, const unsigned int height, const unsigned int maskPaddingElements, PixelPositions &outlinePixels4, const PixelBoundingBox &boundingBox=PixelBoundingBox(), const uint8_t nonMaskValue=0xFF)
Determines all outline-4 pixels in an 8 bit mask frame.
static bool hasMaskNeighbor8(const T *mask, const unsigned int width, const unsigned int height, const unsigned int maskPaddingElements, const PixelPosition &position, const T testValue)
Determines whether at least one neighbor pixel in the 8-neighborhood is or is not a mask pixel.
Definition: segmentation/MaskAnalyzer.h:908
static bool hasMaskNeighbor4Center(const T *mask, const unsigned int width, const unsigned int height, const unsigned int maskPaddingElements, const PixelPosition &position, const T testValue)
Determines whether at least one neighbor pixel in the 4-neighborhood is a mask pixel with the given t...
Definition: segmentation/MaskAnalyzer.h:829
static void analyzeMaskSeparation8Bit(const uint8_t *mask, const unsigned int width, const unsigned int height, const unsigned int maskPaddingElements, uint32_t *separation, const unsigned int separationPaddingElements, MaskBlocks &blocks)
Analyzes an 8 bit binary mask frame and separates the pixels into individual blocks of joined sub-mas...
static bool hasMaskNeighbor8Center(const T *mask, const unsigned int width, const unsigned int height, const unsigned int maskPaddingElements, const PixelPosition &position, const T testValue)
Determines whether at least one neighbor pixel in the 8-neighborhood is a mask pixel with the given t...
Definition: segmentation/MaskAnalyzer.h:973
static bool pixels2contour(const PixelPositions &pixels, const unsigned int width, const unsigned int height, PixelPositions &contour, PixelPositions *remainingPixels=nullptr)
Converts an unordered set of pixel positions (providing all pixels at the mask border either inside t...
static void findBorderPixels8(const uint8_t *mask, const unsigned int width, const unsigned int height, const unsigned int maskPaddingElements, PixelPositions &borderPixels, const PixelBoundingBox &boundingBox=PixelBoundingBox(), Worker *worker=nullptr, const uint8_t nonMaskValue=255u)
Determines all border pixels in an 8 bit mask frame for a 8-neighborhood.
static bool compareValues(const T valueA, const T valueB)
Returns whether two values are identical or not identical.
Definition: segmentation/MaskAnalyzer.h:1262
static bool isOuterContour(const uint8_t *mask, const unsigned int width, const PixelContour &contour, const uint8_t maskValue=0u)
This functions checks whether a given contour is an outer contour or an inner contour.
Definition: segmentation/MaskAnalyzer.h:1074
static void findBorderPixels8Subset(const uint8_t *mask, const unsigned int width, const unsigned int height, const unsigned int maskPaddingElements, PixelPositions *borderPixelsArray, const uint8_t nonMaskValue, const unsigned int firstColumn, const unsigned int numberColumns, const unsigned int firstRow, const unsigned int numberRows, const unsigned int threadId)
Determines the border pixels in a subset of a 8 bit mask frame for a 8-neighborhood.
static bool pixels2contours(const uint8_t *mask, const unsigned int width, const unsigned int height, const PixelPositions &pixels, PixelContours &outerContours, PixelContours &innerContours, const uint8_t maskValue=0u)
Converts an unordered set of pixel positions (providing all pixels at the mask border either inside t...
static void findBorderPixels4Subset(const uint8_t *mask, const unsigned int width, const unsigned int height, const unsigned int maskPaddingElements, PixelPositions *borderPixelsArray, const uint8_t nonMaskValue, const unsigned int firstColumn, const unsigned int numberColumns, const unsigned int firstRow, const unsigned int numberRows, const unsigned int threadId)
Determines the border pixels in a subset of a 8 bit mask frame for a 4-neighborhood.
static bool computeL2DistanceTransform8Bit(const uint8_t *source, const uint32_t width, const uint32_t height, float *target, uint32_t *buffer=nullptr, const uint8_t referenceValue=0u, const uint32_t sourcePaddingElements=0u, const uint32_t targetPaddingElements=0u)
Computes a per pixel (approximated) L2 distance to the nearest pixel with a reference value using 3-b...
Definition: segmentation/MaskAnalyzer.h:1095
std::vector< MaskBlock > MaskBlocks
Definition of a vector holding mask block objects.
Definition: segmentation/MaskAnalyzer.h:112
static void findNonUniquePixels4(const uint8_t *mask, const unsigned int width, const unsigned int height, const unsigned int maskPaddingElements, PixelPositions &nonUniquePixels, const PixelBoundingBox &boundingBox=PixelBoundingBox())
Determines the pixels in an 8 bit mask frame not having the identical pixel values in an 4-neighborho...
std::vector< SweepMaskIsland > SweepMaskIslands
Definition of a vector holding SweepMaskIsland objects.
Definition: segmentation/MaskAnalyzer.h:246
static void findNonUniquePixels8(const uint8_t *mask, const unsigned int width, const unsigned int height, const unsigned int maskPaddingElements, PixelPositions &nonUniquePixels, const PixelBoundingBox &boundingBox=PixelBoundingBox())
Determines the pixels in an 8 bit mask frame not having the identical pixel values in an 8-neighborho...
static bool hasMaskNeighbor5(const T *mask, const unsigned int width, const unsigned int height, const unsigned int maskPaddingElements, const PixelPosition &position, const T testValue)
Determines whether at least one neighbor pixel in the 4-neighborhood (+ center pixel) is or is not a ...
Definition: segmentation/MaskAnalyzer.h:846
static constexpr uint32_t distanceVerticalHorizontalC
Chessboard distance for vertical and horizontal steps.
Definition: segmentation/MaskAnalyzer.h:150
static constexpr uint32_t distanceVerticalHorizontalL1
L1 distance for vertical and horizontal steps.
Definition: segmentation/MaskAnalyzer.h:155
static bool computeChessboardDistanceTransform8Bit(const uint8_t *source, const uint32_t width, const uint32_t height, uint32_t *target, uint32_t *buffer=nullptr, const uint8_t referenceValue=0u, const uint32_t sourcePaddingElements=0u, const uint32_t targetPaddingElements=0u)
Computes a per pixel Chessboard distance to the nearest pixel with a reference value using 3-by-3 nei...
Definition: segmentation/MaskAnalyzer.h:1085
static void determineDistancesToBorder8Bit(uint8_t *mask, const unsigned int width, const unsigned int height, unsigned int maskPaddingElements, const unsigned int iterations, const bool assignFinal, const PixelBoundingBox &boundingBox, Worker *worker=nullptr)
Determines the distance to the mask border in an 8 bit mask frame.
static bool computeDistanceTransform8Bit(const uint8_t *source, const uint32_t width, const uint32_t height, TDistanceType *target, uint32_t *buffer, const TDistanceType distanceVerticalHorizontal, const TDistanceType distanceDiagonal, const uint8_t referenceValue=0u, const uint32_t sourcePaddingElements=0u, const uint32_t targetPaddingElements=0u)
Computes a per pixel distance to the nearest pixel with a reference value using 3-by-3 neighborhood.
Definition: segmentation/MaskAnalyzer.h:1101
static bool hasMaskNeighbor9(const T *mask, const unsigned int width, const unsigned int height, const unsigned int maskPaddingElements, const PixelPosition &position, const T testValue)
Determines whether at least one neighbor pixel in the 8-neighborhood (+ center pixel) is or is not a ...
Definition: segmentation/MaskAnalyzer.h:989
static constexpr uint32_t distanceDiagonalL1
L1 distance for diagonal steps.
Definition: segmentation/MaskAnalyzer.h:157
std::vector< IndexPairs32 > IndexPairGroups
Definition of a vector holding index pair vectors.
Definition: segmentation/MaskAnalyzer.h:147
static void determineDistancesToBorder8BitSubset(uint8_t *mask, const unsigned int width, const unsigned int height, const unsigned int maskPaddingElements, const uint8_t searchValue, const uint8_t resultValue, const unsigned int firstColumn, const unsigned int numberColumns, const unsigned int firstRow, const unsigned int numberRows)
Determines the distance to the mask border in an 8 bit mask frame.
std::set< PixelPosition > PixelPositionSet
Definition of a set holding pixel positions.
Definition: segmentation/MaskAnalyzer.h:142
static bool hasMaskNeighbor5Center(const T *mask, const unsigned int width, const unsigned int height, const unsigned int maskPaddingElements, const PixelPosition &position, const T testValue)
Determines whether at least one neighbor pixel in the 4-neighborhood (+ center pixel) is a mask pixel...
Definition: segmentation/MaskAnalyzer.h:890
static bool hasMaskNeighbor9Center(const T *mask, const unsigned int width, const unsigned int height, const unsigned int maskPaddingElements, const PixelPosition &position, const T testValue)
Determines whether at least one neighbor pixel in the 8-neighborhood (+ center pixel) is a mask pixel...
Definition: segmentation/MaskAnalyzer.h:1059
static void findBorderPixels4(const uint8_t *mask, const unsigned int width, const unsigned int height, const unsigned int maskPaddingElements, PixelPositions &borderPixels, const PixelBoundingBox &boundingBox=PixelBoundingBox(), Worker *worker=nullptr, const uint8_t nonMaskValue=255u)
Determines all border pixels in an 8 bit mask frame for a 4-neighborhood.
ProcessDirection
Definition of process directions.
Definition: segmentation/MaskAnalyzer.h:120
@ PD_SE
South east.
Definition: segmentation/MaskAnalyzer.h:132
@ PD_NW
North west.
Definition: segmentation/MaskAnalyzer.h:124
@ PD_W
West.
Definition: segmentation/MaskAnalyzer.h:126
@ PD_N
North.
Definition: segmentation/MaskAnalyzer.h:122
@ PD_E
East.
Definition: segmentation/MaskAnalyzer.h:134
@ PD_S
South.
Definition: segmentation/MaskAnalyzer.h:130
@ PD_SW
South west.
Definition: segmentation/MaskAnalyzer.h:128
static bool hasMaskNeighbor4(const T *mask, const unsigned int width, const unsigned int height, const unsigned int maskPaddingElements, const PixelPosition &position, const T testValue)
Determines whether at least one neighbor pixel in the 4-neighborhood is or is not a mask pixel.
Definition: segmentation/MaskAnalyzer.h:790
static PixelBoundingBoxes detectBoundingBoxes(const uint8_t *const mask, const unsigned int width, const unsigned int height, const unsigned int maskPaddingElements, const uint8_t maskValue, const bool useNeighborhood4=true)
Determines the axis-aligned bounding boxes of all isolated mask islands in a binary (but 8-bit) mask ...
static constexpr float distanceVerticalHorizontalL2
L2 distance for vertical and horizontal steps.
Definition: segmentation/MaskAnalyzer.h:160
size_t indexLeftPosition() const
Returns the index of a left most position of this contour with following pixel right to this position...
Definition: PixelContour.h:547
This class implements an object able to allocate memory.
Definition: base/Memory.h:22
void * data()
Returns the pointer to the writable memory which is allocated by this object.
Definition: base/Memory.h:303
static constexpr T maxValue()
Returns the max scalar value.
Definition: Numeric.h:3244
This class implements a worker able to distribute function calls over different threads.
Definition: Worker.h:33
std::vector< PixelPosition > PixelPositions
Definition of a vector holding pixel positions (with positive coordinate values).
Definition: PixelPosition.h:48
PixelPositionT< unsigned int > PixelPosition
Definition of the default PixelPosition object with a data type allowing only positive coordinate val...
Definition: PixelPosition.h:27
std::vector< PixelBoundingBox > PixelBoundingBoxes
Definition of a vector holding bounding box objects with only positive coordinate values.
Definition: PixelBoundingBox.h:42
PixelBoundingBoxT< unsigned int > PixelBoundingBox
Definition of the default PixelBoundingBox object with data type allowing only positive coordinate va...
Definition: PixelBoundingBox.h:21
std::vector< PixelContour > PixelContours
Definition of a vector holding pixel contours (with positive coordinate values).
Definition: PixelContour.h:50
The namespace covering the entire Ocean framework.
Definition: Accessor.h:15