Ocean
testsegmentation/TestMaskAnalyzer.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) Meta Platforms, Inc. and affiliates.
3  *
4  * This source code is licensed under the MIT license found in the
5  * LICENSE file in the root directory of this source tree.
6  */
7 
8 #ifndef META_OCEAN_TEST_TESTCV_TESTSEGMENTATION_TEST_MASK_ANALYZER_H
9 #define META_OCEAN_TEST_TESTCV_TESTSEGMENTATION_TEST_MASK_ANALYZER_H
10 
12 
14 
15 #include "ocean/cv/PixelPosition.h"
16 
18 
19 namespace Ocean
20 {
21 
22 namespace Test
23 {
24 
25 namespace TestCV
26 {
27 
28 namespace TestSegmentation
29 {
30 
31 /**
32  * This class implements a mask analyzer test.
33  * @ingroup testcvsegmentation
34  */
35 class OCEAN_TEST_CV_SEGMENTATION_EXPORT TestMaskAnalyzer : public CV::Segmentation::MaskAnalyzer
36 {
37  protected:
38 
39  /// Function pointer for distance transform the functions `CV::Segmentation::MaskAnalyzer::compute{C,L1,L2}DistanceTransform8Bit()`
40  template <typename TDistanceType>
41  using ComputeDistanceTransformFunctionPointer = bool (*) (const uint8_t*, const uint32_t, const uint32_t, TDistanceType*, uint32_t*, const uint8_t, const uint32_t, const uint32_t);
42 
43  /**
44  * Definition of an unordered set holding bounding boxes.
45  */
46  typedef std::unordered_set<CV::PixelBoundingBox, CV::PixelBoundingBox> BoundingBoxSet;
47 
48  /**
49  * Definition of an unordered set holding pixel positions boxes.
50  */
51  typedef std::unordered_set<CV::PixelPosition, CV::PixelPosition> PixelPositionSet;
52 
53  public:
54 
55  /**
56  * Tests all mask analyzer functions.
57  * @param width The width of the test frame in pixel, with range [32, infinity)
58  * @param height The height of the test frame in pixel, with range [32, infinity)
59  * @param testDuration Number of seconds for each test, with range (0, infinity)
60  * @param worker The worker object to distribute the computational load
61  * @return True, if succeeded
62  */
63  static bool test(const unsigned int width, const unsigned int height, const double testDuration, Worker& worker);
64 
65  /**
66  * Tests the has mask neighbor-4 function.
67  * @param testDuration Number of seconds for each test, with range (0, infinity)
68  * @return True, if succeeded
69  */
70  static bool testHasMaskNeighbor4(const double testDuration);
71 
72  /**
73  * Tests the has mask neighbor-4 center function.
74  * @param testDuration Number of seconds for each test, with range (0, infinity)
75  * @return True, if succeeded
76  */
77  static bool testHasMaskNeighbor4Center(const double testDuration);
78 
79  /**
80  * Tests the has mask neighbor-5 function.
81  * @param testDuration Number of seconds for each test, with range (0, infinity)
82  * @return True, if succeeded
83  */
84  static bool testHasMaskNeighbor5(const double testDuration);
85 
86  /**
87  * Tests the has mask neighbor-5 center function.
88  * @param testDuration Number of seconds for each test, with range (0, infinity)
89  * @return True, if succeeded
90  */
91  static bool testHasMaskNeighbor5Center(const double testDuration);
92 
93  /**
94  * Tests the has mask neighbor-8 function.
95  * @param testDuration Number of seconds for each test, with range (0, infinity)
96  * @return True, if succeeded
97  */
98  static bool testHasMaskNeighbor8(const double testDuration);
99 
100  /**
101  * Tests the has mask neighbor-8 center function.
102  * @param testDuration Number of seconds for each test, with range (0, infinity)
103  * @return True, if succeeded
104  */
105  static bool testHasMaskNeighbor8Center(const double testDuration);
106 
107  /**
108  * Tests the has mask neighbor-9 function.
109  * @param testDuration Number of seconds for each test, with range (0, infinity)
110  * @return True, if succeeded
111  */
112  static bool testHasMaskNeighbor9(const double testDuration);
113 
114  /**
115  * Tests the has mask neighbor-9 center function.
116  * @param testDuration Number of seconds for each test, with range (0, infinity)
117  * @return True, if succeeded
118  */
119  static bool testHasMaskNeighbor9Center(const double testDuration);
120 
121  /**
122  * Tests the distance to border function.
123  * @param testDuration Number of seconds for each test, with range (0, infinity)
124  * @param worker The worker object
125  * @return True, if succeeded
126  */
127  static bool testDetermineDistancesToBorder(const double testDuration, Worker& worker);
128 
129  /**
130  * Tests the distance to border function.
131  * @param width The width of the test frame in pixel, with range [32, infinity)
132  * @param height The height of the test frame in pixel, with range [32, infinity)
133  * @param testDuration Number of seconds for each test, with range (0, infinity)
134  * @param maximalDistance The maximal distance to be used, with range [1, infinity)
135  * @param worker The worker object
136  * @return True, if succeeded
137  */
138  static bool testDetermineDistancesToBorder(const unsigned int width, const unsigned int height, const double testDuration, const unsigned int maximalDistance, Worker& worker);
139 
140  /**
141  * Tests the find border pixels in a 4-neighborhood function.
142  * @param width The width of the test frame in pixel, with range [32, infinity)
143  * @param height The height of the test frame in pixel, with range [32, infinity)
144  * @param testDuration Number of seconds for each test, with range (0, infinity)
145  * @param worker The worker object
146  * @return True, if succeeded
147  */
148  static bool testFindBorderPixels4(const unsigned int width, const unsigned int height, const double testDuration, Worker& worker);
149 
150  /**
151  * Tests the find border pixels in an 8-neighborhood function.
152  * @param width The width of the test frame in pixel, with range [32, infinity)
153  * @param height The height of the test frame in pixel, with range [32, infinity)
154  * @param testDuration Number of seconds for each test, with range (0, infinity)
155  * @param worker The worker object
156  * @return True, if succeeded
157  */
158  static bool testFindBorderPixels8(const unsigned int width, const unsigned int height, const double testDuration, Worker& worker);
159 
160  /**
161  * Tests the non-unique pixels in a 4-neighborhood function.
162  * @param width The width of the test frame in pixel, with range [32, infinity)
163  * @param height The height of the test frame in pixel, with range [32, infinity)
164  * @param testDuration Number of seconds for each test, with range (0, infinity)
165  * @param worker The worker object
166  * @return True, if succeeded
167  */
168  static bool testFindNonUniquePixels4(const unsigned int width, const unsigned int height, const double testDuration, Worker& worker);
169 
170  /**
171  * Tests the non-unique pixels in an 8-neighborhood function.
172  * @param width The width of the test frame in pixel, with range [32, infinity)
173  * @param height The height of the test frame in pixel, with range [32, infinity)
174  * @param testDuration Number of seconds for each test, with range (0, infinity)
175  * @param worker The worker object
176  * @return True, if succeeded
177  */
178  static bool testFindNonUniquePixels8(const unsigned int width, const unsigned int height, const double testDuration, Worker& worker);
179 
180  /**
181  * Tests the outline pixels in a 4-neighborhood function.
182  * @param width The width of the test frame in pixel, with range [32, infinity)
183  * @param height The height of the test frame in pixel, with range [32, infinity)
184  * @param testDuration Number of seconds for each test, with range (0, infinity)
185  * @return True, if succeeded
186  */
187  static bool testFindOutlinePixels4(const unsigned int width, const unsigned int height, const double testDuration);
188 
189  /**
190  * Test the distance transformations that uses the chessboard distance
191  * @param width The width of the source frame in pixel, with range [1, infinity)
192  * @param height The height of the source frame in pixel, with range [1, infinity)
193  * @param testDuration Number of seconds for each test, with range (0, infinity)
194  * @return True, if succeeded
195  */
196  static bool testComputeChessboardDistanceTransform8Bit(const uint32_t width, const uint32_t height, const double testDuration);
197 
198  /**
199  * Test the distance transformations that uses the L1 distance
200  * @param width The width of the source frame in pixel, with range [1, infinity)
201  * @param height The height of the source frame in pixel, with range [1, infinity)
202  * @param testDuration Number of seconds for each test, with range (0, infinity)
203  * @return True, if succeeded
204  */
205  static bool testComputeL1DistanceTransform8Bit(const uint32_t width, const uint32_t height, const double testDuration);
206 
207  /**
208  * Test the distance transformations that uses the approximated L2 distance
209  * @param width The width of the source frame in pixel, with range [1, infinity)
210  * @param height The height of the source frame in pixel, with range [1, infinity)
211  * @param testDuration Number of seconds for each test, with range (0, infinity)
212  * @return True, if succeeded
213  */
214  static bool testComputeL2DistanceTransform8Bit(const uint32_t width, const uint32_t height, const double testDuration);
215 
216  /**
217  * Test the determine bounding boxes function.
218  * @param testDuration Number of seconds for each test, with range (0, infinity)
219  * @return True, if succeeded
220  */
221  static bool testDetectBoundingBoxes(const double testDuration);
222 
223  /**
224  * Tests the count mask pixels functions.
225  * @param testDuration Number of seconds for each test, with range (0, infinity)
226  * @return True, if succeeded
227  */
228  static bool testCountMaskPixels(const double testDuration);
229 
230  /**
231  * Generates test data for the mask analyzer tests
232  * This will generate a white mask frame with a black cross in the center, with 20 pixel width
233  * @param randomGenerator The random generator to be used
234  * @param width The width of the frame that will be generated, range: [1, infinity)
235  * @param height The height of the frame that will be generated, range: [1, infinity)
236  * @param maskValue The value of mask pixels, with range [0, 255]
237  * @param nonMaskValue The value of non-mask pixels, with range [0, 255] without 'maskValue'
238  * @return The generated test frame
239  */
240  static Frame generateTestMask(RandomGenerator& randomGenerator, const unsigned int width, const unsigned int height, const uint8_t maskValue = 0x00u, const uint8_t nonMaskValue = 0xFFu);
241 
242  protected:
243 
244  /**
245  * Test for distance transform functions
246  * @note The function that can be tested with this function must have an interface as defined by `TestDistanceTransform::ComputeDistanceTransformFunctionPointer`
247  * @param width The width of the images that are used for the performance tests, range: [1, infinity)
248  * @param height The height of the image that are used for the performance tests, range: [1, infinity)
249  * @param computeDistanceTransformPtr A pointer to distance transformation function in `CV::DistanceTransform` that will be tested, must be valid
250  * @param distanceVerticalHorizontal The cost of a vertical/horizontal step; must match the cost of the function that is tested, range: (0, infinity)
251  * @param distanceDiagonal The cost of diagonal step; must match the cost of the function that is tested, range: (0, infinity)
252  * @param testDuration Number of seconds for each test, with range (0, infinity)
253  * @return True if the validation was successful, otherwise false
254  * @tparam TDistanceType The data type of the resulting distance transform, must be integral or floating-point
255  */
256  template <typename TDistanceType>
257  static bool testComputeDistanceTransform8Bit(const uint32_t width, const uint32_t height, ComputeDistanceTransformFunctionPointer<TDistanceType> computeDistanceTransformPtr, const TDistanceType distanceVerticalHorizontal, const TDistanceType distanceDiagonal, const double testDuration);
258 
259  /**
260  * Validates the distance to border function.
261  * @param mask The original mask frame, must be valid
262  * @param distance The corresponding distance frame to validate, must be valid
263  * @param maximalDistance Maximal distance to be determined
264  * @param assignFinal True, to assign each mask pixel a minimal distance after the final iteration
265  * @return True, if succeeded
266  */
267  static bool validateDetermineDistancesToBorder(const Frame& mask, const Frame& distance, const unsigned int maximalDistance, const bool assignFinal);
268 
269  /**
270  * Validates the find border pixels in a 4-neighborhood function.
271  * @param mask The mask to verify, must be valid
272  * @param boundingBox The bounding box which has been used, invalid to use the entire mask
273  * @param borderPixels The border pixels to verify
274  * @return True, if succeeded
275  */
276  static bool validateFindBorderPixels4(const Frame& mask, const CV::PixelBoundingBox& boundingBox, const CV::PixelPositions& borderPixels);
277 
278  /**
279  * Validates the find border pixels in an 8-neighborhood function.
280  * @param mask The mask to verify, must be valid
281  * @param boundingBox The bounding box which has been used, invalid to use the entire mask
282  * @param borderPixels The border pixels to verify
283  * @return True, if succeeded
284  */
285  static bool validateFindBorderPixels8(const Frame& mask, const CV::PixelBoundingBox& boundingBox, const CV::PixelPositions& borderPixels);
286 
287  /**
288  * Validates the find non-unique pixels in a 4-neighborhood function.
289  * @param mask The mask to verify, must be valid
290  * @param boundingBox The bounding box which has been used, invalid to use the entire mask
291  * @param nonUniquePixels The on unique pixels to verify
292  * @return True, if succeeded
293  */
294  static bool validateFindNonUniquePixels4(const Frame& mask, const CV::PixelBoundingBox& boundingBox, const CV::PixelPositions& nonUniquePixels);
295 
296  /**
297  * Validates the find non-unique pixels in an 8-neighborhood function.
298  * @param mask The mask to verify, must be valid
299  * @param boundingBox The bounding box which has been used, invalid to use the entire mask
300  * @param nonUniquePixels The on unique pixels to verify
301  * @return True, if succeeded
302  */
303  static bool validateFindNonUniquePixels8(const Frame& mask, const CV::PixelBoundingBox& boundingBox, const CV::PixelPositions& nonUniquePixels);
304 
305  /**
306  * Validates the find outline pixels in a 4-neighborhood function.
307  * @param mask The mask to verify, must be valid
308  * @param boundingBox The bounding box which has been used, invalid to use the entire mask
309  * @param outlinePixels The outline pixels to verify
310  * @param nonMaskValue The value of non-mask pixels, with range [0, 255] without 'maskValue'
311  * @return True, if succeeded
312  */
313  static bool validateFindOutlinePixels4(const Frame& mask, const CV::PixelBoundingBox& boundingBox, const CV::PixelPositions& outlinePixels, const uint8_t nonMaskValue);
314 
315  /**
316  * Validation function for the distance transform function
317  * @param source A pointer to the source frame data, must be valid
318  * @param width The width of the source frame, range: [1, infinity)
319  * @param height The height of the source frame, range: [1, infinity)
320  * @param validationTarget A pointer to the result of a distance transform that will be validated, must be valid and have the same size as the source frame
321  * @param validationTargetStatus The return value of the distance transform function that will be validated
322  * @param distanceVerticalHorizontal e cost of a vertical/horizontal step; must match the cost of the function that is tested, range: (0, infinity)
323  * @param distanceDiagonal The cost of diagonal step; must match the cost of the function that is tested, range: (0, infinity)
324  * @param referenceValue Distance values pixels will be computed to the closest pixel with this reference value, range: [0, 255] and must match that of the distance transform that is validated
325  * @param sourcePaddingElements Optional number of padding elements of the source frame, range: [0, infinity)
326  * @param validationTargetPaddingElements Optional number padding elements of the target frame, range: [0, infinity)
327  * @return True if the validation was successful, otherwise false
328  * @tparam TDistanceType The data type of the distance transform that will be validated, this must be an integral or floating-point type
329  */
330  template <typename TDistanceType>
331  static bool validateComputeDistanceTransform8Bit(const uint8_t* source, const uint32_t width, const uint32_t height, const TDistanceType* validationTarget, const bool validationTargetStatus, const TDistanceType distanceVerticalHorizontal, const TDistanceType distanceDiagonal, const uint8_t referenceValue, const uint32_t sourcePaddingElements = 0u, const uint32_t validationTargetPaddingElements = 0u);
332 };
333 
334 } // namespace TestSegmentation
335 
336 } // namespace TestCV
337 
338 } // namespace Test
339 
340 } // namespace Ocean
341 
342 #endif // META_OCEAN_TEST_TESTCV_TESTSEGMENTATION_TEST_MASK_ANALYZER_H
This class implements functions analyzing masks, determining specific pixels in relation to masks and...
Definition: segmentation/MaskAnalyzer.h:39
This class implements Ocean's image class.
Definition: Frame.h:1760
This class implements a generator for random numbers.
Definition: RandomGenerator.h:42
This class implements a mask analyzer test.
Definition: testsegmentation/TestMaskAnalyzer.h:36
static bool validateFindBorderPixels4(const Frame &mask, const CV::PixelBoundingBox &boundingBox, const CV::PixelPositions &borderPixels)
Validates the find border pixels in a 4-neighborhood function.
std::unordered_set< CV::PixelBoundingBox, CV::PixelBoundingBox > BoundingBoxSet
Definition of an unordered set holding bounding boxes.
Definition: testsegmentation/TestMaskAnalyzer.h:46
static bool testFindNonUniquePixels8(const unsigned int width, const unsigned int height, const double testDuration, Worker &worker)
Tests the non-unique pixels in an 8-neighborhood function.
static bool testHasMaskNeighbor9Center(const double testDuration)
Tests the has mask neighbor-9 center function.
static bool testHasMaskNeighbor5Center(const double testDuration)
Tests the has mask neighbor-5 center function.
static bool testFindBorderPixels8(const unsigned int width, const unsigned int height, const double testDuration, Worker &worker)
Tests the find border pixels in an 8-neighborhood function.
static bool validateFindNonUniquePixels4(const Frame &mask, const CV::PixelBoundingBox &boundingBox, const CV::PixelPositions &nonUniquePixels)
Validates the find non-unique pixels in a 4-neighborhood function.
static bool testComputeDistanceTransform8Bit(const uint32_t width, const uint32_t height, ComputeDistanceTransformFunctionPointer< TDistanceType > computeDistanceTransformPtr, const TDistanceType distanceVerticalHorizontal, const TDistanceType distanceDiagonal, const double testDuration)
Test for distance transform functions.
static bool testHasMaskNeighbor8(const double testDuration)
Tests the has mask neighbor-8 function.
static bool testCountMaskPixels(const double testDuration)
Tests the count mask pixels functions.
static bool testFindOutlinePixels4(const unsigned int width, const unsigned int height, const double testDuration)
Tests the outline pixels in a 4-neighborhood function.
static bool validateFindNonUniquePixels8(const Frame &mask, const CV::PixelBoundingBox &boundingBox, const CV::PixelPositions &nonUniquePixels)
Validates the find non-unique pixels in an 8-neighborhood function.
static bool validateComputeDistanceTransform8Bit(const uint8_t *source, const uint32_t width, const uint32_t height, const TDistanceType *validationTarget, const bool validationTargetStatus, const TDistanceType distanceVerticalHorizontal, const TDistanceType distanceDiagonal, const uint8_t referenceValue, const uint32_t sourcePaddingElements=0u, const uint32_t validationTargetPaddingElements=0u)
Validation function for the distance transform function.
static bool testComputeL2DistanceTransform8Bit(const uint32_t width, const uint32_t height, const double testDuration)
Test the distance transformations that uses the approximated L2 distance.
static bool testDetermineDistancesToBorder(const double testDuration, Worker &worker)
Tests the distance to border function.
static bool validateFindBorderPixels8(const Frame &mask, const CV::PixelBoundingBox &boundingBox, const CV::PixelPositions &borderPixels)
Validates the find border pixels in an 8-neighborhood function.
bool(*)(const uint8_t *, const uint32_t, const uint32_t, TDistanceType *, uint32_t *, const uint8_t, const uint32_t, const uint32_t) ComputeDistanceTransformFunctionPointer
Function pointer for distance transform the functions CV::Segmentation::MaskAnalyzer::compute{C,...
Definition: testsegmentation/TestMaskAnalyzer.h:41
static bool testComputeChessboardDistanceTransform8Bit(const uint32_t width, const uint32_t height, const double testDuration)
Test the distance transformations that uses the chessboard distance.
static bool testHasMaskNeighbor4(const double testDuration)
Tests the has mask neighbor-4 function.
static bool testHasMaskNeighbor4Center(const double testDuration)
Tests the has mask neighbor-4 center function.
static bool validateDetermineDistancesToBorder(const Frame &mask, const Frame &distance, const unsigned int maximalDistance, const bool assignFinal)
Validates the distance to border function.
static bool testHasMaskNeighbor9(const double testDuration)
Tests the has mask neighbor-9 function.
static bool testFindNonUniquePixels4(const unsigned int width, const unsigned int height, const double testDuration, Worker &worker)
Tests the non-unique pixels in a 4-neighborhood function.
static bool testFindBorderPixels4(const unsigned int width, const unsigned int height, const double testDuration, Worker &worker)
Tests the find border pixels in a 4-neighborhood function.
static bool testHasMaskNeighbor5(const double testDuration)
Tests the has mask neighbor-5 function.
static Frame generateTestMask(RandomGenerator &randomGenerator, const unsigned int width, const unsigned int height, const uint8_t maskValue=0x00u, const uint8_t nonMaskValue=0xFFu)
Generates test data for the mask analyzer tests This will generate a white mask frame with a black cr...
static bool testDetectBoundingBoxes(const double testDuration)
Test the determine bounding boxes function.
static bool test(const unsigned int width, const unsigned int height, const double testDuration, Worker &worker)
Tests all mask analyzer functions.
static bool testHasMaskNeighbor8Center(const double testDuration)
Tests the has mask neighbor-8 center function.
std::unordered_set< CV::PixelPosition, CV::PixelPosition > PixelPositionSet
Definition of an unordered set holding pixel positions boxes.
Definition: testsegmentation/TestMaskAnalyzer.h:51
static bool testComputeL1DistanceTransform8Bit(const uint32_t width, const uint32_t height, const double testDuration)
Test the distance transformations that uses the L1 distance.
static bool testDetermineDistancesToBorder(const unsigned int width, const unsigned int height, const double testDuration, const unsigned int maximalDistance, Worker &worker)
Tests the distance to border function.
static bool validateFindOutlinePixels4(const Frame &mask, const CV::PixelBoundingBox &boundingBox, const CV::PixelPositions &outlinePixels, const uint8_t nonMaskValue)
Validates the find outline pixels in a 4-neighborhood function.
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
The namespace covering the entire Ocean framework.
Definition: Accessor.h:15