Ocean
ContourAnalyzer.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_CV_SEGMENTATION_CONTOUR_ANALYZER_H
9 #define META_OCEAN_CV_SEGMENTATION_CONTOUR_ANALYZER_H
10 
13 
14 #include "ocean/cv/PixelPosition.h"
15 
16 #include "ocean/math/Math.h"
17 
18 #include <vector>
19 
20 namespace Ocean
21 {
22 
23 namespace CV
24 {
25 
26 namespace Segmentation
27 {
28 
29 /**
30  * This class implements functions analyzing contours.
31  * @ingroup cvsegmentation
32  */
33 class OCEAN_CV_SEGMENTATION_EXPORT ContourAnalyzer
34 {
35  public:
36 
37  /**
38  * Definition of a vector holding indices.
39  */
40  typedef std::vector<unsigned int> Indices;
41 
42  protected:
43 
44  /**
45  * This class implements a simple storage for a point index with corresponding dense parameter.
46  * The class is mainly a helper class allowing to sort several dense objects according to their value.
47  */
49  {
50  public:
51 
52  /**
53  * Creates an default dense object.
54  */
55  DenseObject() = default;
56 
57  /**
58  * Creates a new dense object.
59  * @param dense The dense value of the object
60  * @param index The index of the object
61  */
62  inline DenseObject(const Scalar dense, const unsigned int index);
63 
64  /**
65  * Returns the dense value of this object.
66  * @return Dense value
67  */
68  inline Scalar dense() const;
69 
70  /**
71  * Returns the index of this object.
72  * @return Object index
73  */
74  inline unsigned int index() const;
75 
76  /**
77  * Returns whether the dense value of this object is smaller than that of a given second object.
78  * @param object Second object to compare the dense with
79  * @return True, if so
80  */
81  inline bool operator<(const DenseObject& object) const;
82 
83  private:
84 
85  /// Object dense value.
86  Scalar dense_ = Scalar(-1);
87 
88  /// Object index value.
89  unsigned int index_ = (unsigned int)(-1);
90  };
91 
92  /// Definition of a vector holding dense objects.
93  typedef std::vector<DenseObject> DenseObjects;
94 
95  public:
96 
97  /**
98  * Determines the curvature for each contour pixel.
99  * The resulting values are the cosine value of the angles between two vectors starting at the interest point pointing several pixels forward and backward in the contour.
100  * @param contour The contour for which the curvature parameters will be determined
101  * @param offset Pixel offset inside the contour between the pixel of interest and the pixels to measure the curvature, with range [1, contour.size())
102  * @return Cosine values giving the curvature of the contour, one value for each pixel in the contour (a value of 1 determines an angle of 90 deg, a value of 0 determines a value of +/- 180 deg)
103  */
104  static Scalars curvature(const PixelPositions& contour, const unsigned int offset);
105 
106  /**
107  * Creates a dense and distinct contour from any kind of given contour also ensuring that the resulting contour does not contain complex properties like loops.
108  * In contrast to e.g., PixelContour::makeDense(), this function needs significantly more computational time while is able to handle more complex input contours.
109  * @param points The points of any kind of contour which will be converted to a dense and distinct contour
110  * @return The resulting dense and distinct contour
111  * @see PixelContour::makeDense().
112  */
114 
115  /**
116  * Creates a dense and distinct contour from any kind of given contour also ensuring that the resulting contour does not contain complex properties like loops.
117  * In contrast to e.g., PixelContour::makeDense(), this function needs significantly more computational time while is able to handle more complex input contours.
118  * @param points The points of any kind of contour which will be converted to a dense and distinct contour with sub-pixel accuracy
119  * @return The resulting dense and distinct contour
120  */
121  static inline PixelContour createDenseContour(const Vectors2& points);
122 
123  /**
124  * Equalized a sparse contour according to the density of the locations of contour points.
125  * @param contour The contour to be equalized
126  * @return The resulting equalized contour
127  */
128  static Vectors2 equalizeContourDensity(const Vectors2& contour);
129 
130  private:
131 
132  /**
133  * Calculates a hash value for a given pixel position.
134  * @param position Pixel position to determine the hash function for
135  * @return Resulting hash function
136  */
137  static inline size_t pixelPositionHashValueFunction(const PixelPosition& position);
138 
139  /**
140  * Returns the pixel direction of two successive pixels in a dense contour.
141  * @param pixel0 First pixel
142  * @param pixel1 Following pixel
143  * @return Resulting pixel direction
144  */
145  static inline PixelDirection determinePixelDirection(const PixelPosition& pixel0, const PixelPosition& pixel1);
146 };
147 
148 inline ContourAnalyzer::DenseObject::DenseObject(const Scalar dense, const unsigned int index) :
149  dense_(dense),
150  index_(index)
151 {
152  // nothing to do here
153 }
154 
156 {
157  return dense_;
158 }
159 
160 inline unsigned int ContourAnalyzer::DenseObject::index() const
161 {
162  return index_;
163 }
164 
165 inline bool ContourAnalyzer::DenseObject::operator<(const DenseObject& object) const
166 {
167  return dense_ < object.dense_;
168 }
169 
171 {
173 }
174 
176 {
177  // we simply shift the x-coordinate to the upper bits and take the y-coordinate for the lower bits
178 
179 #ifdef OCEAN_DEBUG
180  const size_t size_tBits_2 = sizeof(size_t) * 4u;
181  const size_t maximalValue = size_t(1) << size_tBits_2;
182  ocean_assert(size_t(position.x()) < maximalValue && size_t(position.y()) < maximalValue);
183 #endif
184 
185  return (size_t(position.x()) << sizeof(size_t) * 4) | size_t(position.y());
186 }
187 
189 {
190  ocean_assert(pixel0 && pixel1);
191  ocean_assert(pixel0.isNeighbor8(pixel1));
192 
193  const unsigned int parameter = (0x0000FFFF & (pixel1.x() - pixel0.x())) | ((pixel1.y() - pixel0.y()) << 16);
194 
195  // the low 16 bit may have value 0x0000 (same), 0x0001 (east) or 0xFFFF (west)
196  // the high 16 bit may have value 0x0000 (same), 0x0001 (south) or 0xFFFF (north)
197 
198  switch (parameter)
199  {
200  // north
201  case 0xFFFF0000u:
202  ocean_assert(pixel0.north() == pixel1);
203  return PD_NORTH;
204 
205  // north west
206  case 0xFFFFFFFFu:
207  ocean_assert(pixel0.northWest() == pixel1);
208  return PD_NORTH_WEST;
209 
210  // west
211  case 0x0000FFFFu:
212  ocean_assert(pixel0.west() == pixel1);
213  return PD_WEST;
214 
215  // south west
216  case 0x0001FFFFu:
217  ocean_assert(pixel0.southWest() == pixel1);
218  return PD_SOUTH_WEST;
219 
220  // south
221  case 0x00010000u:
222  ocean_assert(pixel0.south() == pixel1);
223  return PD_SOUTH;
224 
225  // south east
226  case 0x00010001u:
227  ocean_assert(pixel0.southEast() == pixel1);
228  return PD_SOUTH_EAST;
229 
230  // east
231  case 0x00000001u:
232  ocean_assert(pixel0.east() == pixel1);
233  return PD_EAST;
234 
235  // north east
236  case 0xFFFF0001u:
237  ocean_assert(pixel0.northEast() == pixel1);
238  return PD_NORTH_EAST;
239  }
240 
241  ocean_assert(false && "Invalid direction");
242  return PD_INVALID;
243 }
244 
245 }
246 
247 }
248 
249 }
250 
251 #endif // META_OCEAN_CV_SEGMENTATION_CONTOUR_ANALYZER_H
PixelPositionT< T > west() const
Returns the pixel position west to this position.
Definition: PixelPosition.h:561
PixelPositionT< T > southWest() const
Returns the pixel position south west to this position.
Definition: PixelPosition.h:567
bool isNeighbor8(const PixelPositionT< T > &position) const
Returns whether this pixel position is the direct neighbor to a second pixel position in an 8-neighbo...
Definition: PixelPosition.h:663
PixelPositionT< T > north() const
Returns the pixel position north to this position.
Definition: PixelPosition.h:549
PixelPositionT< T > east() const
Returns the pixel position east to this position.
Definition: PixelPosition.h:585
T y() const
Returns the vertical coordinate position of this object.
Definition: PixelPosition.h:470
PixelPositionT< T > southEast() const
Returns the pixel position south east to this position.
Definition: PixelPosition.h:579
T x() const
Returns the horizontal coordinate position of this object.
Definition: PixelPosition.h:458
PixelPositionT< T > northWest() const
Returns the pixel position north west to this position.
Definition: PixelPosition.h:555
static std::vector< PixelPositionT< unsigned int > > vectors2pixelPositions(const Vectors2 &values)
Converts several 2D vectors into pixel positions.
Definition: PixelPosition.h:947
PixelPositionT< T > south() const
Returns the pixel position south to this position.
Definition: PixelPosition.h:573
PixelPositionT< T > northEast() const
Returns the pixel position north east to this position.
Definition: PixelPosition.h:591
This class implements a simple storage for a point index with corresponding dense parameter.
Definition: ContourAnalyzer.h:49
DenseObject()=default
Creates an default dense object.
bool operator<(const DenseObject &object) const
Returns whether the dense value of this object is smaller than that of a given second object.
Definition: ContourAnalyzer.h:165
Scalar dense() const
Returns the dense value of this object.
Definition: ContourAnalyzer.h:155
unsigned int index() const
Returns the index of this object.
Definition: ContourAnalyzer.h:160
This class implements functions analyzing contours.
Definition: ContourAnalyzer.h:34
static Vectors2 equalizeContourDensity(const Vectors2 &contour)
Equalized a sparse contour according to the density of the locations of contour points.
std::vector< DenseObject > DenseObjects
Definition of a vector holding dense objects.
Definition: ContourAnalyzer.h:93
static PixelDirection determinePixelDirection(const PixelPosition &pixel0, const PixelPosition &pixel1)
Returns the pixel direction of two successive pixels in a dense contour.
Definition: ContourAnalyzer.h:188
std::vector< unsigned int > Indices
Definition of a vector holding indices.
Definition: ContourAnalyzer.h:40
static size_t pixelPositionHashValueFunction(const PixelPosition &position)
Calculates a hash value for a given pixel position.
Definition: ContourAnalyzer.h:175
static Scalars curvature(const PixelPositions &contour, const unsigned int offset)
Determines the curvature for each contour pixel.
static PixelContour createDenseContour(const PixelPositions &points)
Creates a dense and distinct contour from any kind of given contour also ensuring that the resulting ...
std::vector< PixelPosition > PixelPositions
Definition of a vector holding pixel positions (with positive coordinate values).
Definition: PixelPosition.h:48
PixelDirection
Definition of individual directions with pixel accuracy.
Definition: CV.h:85
@ PD_WEST
Definition: CV.h:93
@ PD_NORTH_EAST
Definition: CV.h:103
@ PD_NORTH_WEST
Definition: CV.h:91
@ PD_EAST
Definition: CV.h:101
@ PD_SOUTH_WEST
Definition: CV.h:95
@ PD_SOUTH
Definition: CV.h:97
@ PD_SOUTH_EAST
Definition: CV.h:99
@ PD_INVALID
Definition: CV.h:87
@ PD_NORTH
Definition: CV.h:89
float Scalar
Definition of a scalar type.
Definition: Math.h:128
std::vector< Scalar > Scalars
Definition of a vector holding Scalar objects.
Definition: Math.h:144
std::vector< Vector2 > Vectors2
Definition of a vector holding Vector2 objects.
Definition: Vector2.h:64
The namespace covering the entire Ocean framework.
Definition: Accessor.h:15