Ocean
SubRegion.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_SUB_REGION_H
9 #define META_OCEAN_CV_SUB_REGION_H
10 
11 #include "ocean/cv/CV.h"
12 
13 #include "ocean/base/Frame.h"
14 
16 
17 #include "ocean/math/Box2.h"
19 #include "ocean/math/Triangle2.h"
20 
21 namespace Ocean
22 {
23 
24 namespace CV
25 {
26 
27 /**
28  * This class implement a sub-region either defined by 2D triangles or defined by a binary mask.
29  * @ingroup cv
30  */
31 class OCEAN_CV_EXPORT SubRegion
32 {
33  public:
34 
35  /**
36  * Creates an empty sub-region.
37  */
38  inline SubRegion();
39 
40  /**
41  * Copy constructor.
42  * @param subRegion Sub-region object to be copied
43  */
44  inline SubRegion(const SubRegion& subRegion);
45 
46  /**
47  * Move constructor.
48  * @param subRegion Sub-region object to be moved
49  */
50  inline SubRegion(SubRegion&& subRegion) noexcept;
51 
52  /**
53  * Creates a sub-region object by a given set of 2D triangles.
54  * @param triangles 2D triangles defining the sub-region
55  */
56  explicit SubRegion(const Triangles2& triangles);
57 
58  /**
59  * Creates a sub-region object by a given set of 2D triangles.
60  * @param triangles 2D triangles defining the sub-region
61  * @param size The number of given triangles
62  */
63  SubRegion(const Triangle2* triangles, const size_t size);
64 
65  /**
66  * Creates a sub-region object by a given binary mask.
67  * @param mask Binary mask defining the sub-region
68  * @param boundingBox Optional bounding box of the sub-region
69  * @param maskValue The mask value defining the valid pixels
70  */
71  explicit SubRegion(const Frame& mask, const PixelBoundingBox& boundingBox = PixelBoundingBox(), const uint8_t maskValue = 0x00);
72 
73  /**
74  * Creates a sub-region object by a given binary mask.
75  * @param mask Binary mask defining the sub-region
76  * @param boundingBox Optional bounding box of the sub-region
77  * @param maskValue The mask value defining the valid pixels
78  */
79  explicit SubRegion(Frame&& mask, const PixelBoundingBox& boundingBox = PixelBoundingBox(), const uint8_t maskValue = 0x00);
80 
81  /**
82  * Creates a rectangular sub-region which will be handled (internally) as two triangles with one common edge.
83  * @param left Horizontal start position of the sub-region
84  * @param top Vertical start position of the sub-region
85  * @param width The width of the sub-region, with range (0, infinity)
86  * @param height The height of the sub-region, with range (0, infinity)
87  */
88  SubRegion(const Scalar left, const Scalar top, const Scalar width, const Scalar height);
89 
90  /**
91  * Creates a rectangular sub-region which will be handled (internally) as two triangles with one common edge.
92  * If the given bounding box is invalid the new sub-region will be a default sub-region.<br>
93  * @param boundingBox Bounding box that will defined the sub-region
94  */
95  explicit SubRegion(const Box2& boundingBox);
96 
97  /**
98  * Creates a rectangular sub-region which will be handled (internally) as two triangles with one common edge.
99  * If the given bounding box is invalid the new sub-region will be a default sub-region.<br>
100  * @param boundingBox Bounding box that will defined the sub-region
101  */
102  explicit SubRegion(const PixelBoundingBox& boundingBox);
103 
104  /**
105  * Returns the bounding box of this sub-region.
106  * @return Sub-region bounding box
107  */
108  inline const Box2& boundingBox() const;
109 
110  /**
111  * Returns whether a given point lies inside this sub-region.
112  * @param point Point to be checked
113  * @return True, if so
114  */
115  bool isInside(const Vector2& point) const;
116 
117  /**
118  * Filters a set of given image points so that all resulting points ly inside this sub-region.
119  * @param points The set of image points which will be filtered
120  * @return Image points lying inside this sub-region
121  */
122  Vectors2 filterPoints(const Vectors2& points) const;
123 
124  /**
125  * Returns whether this sub-region is empty.
126  * @return True, if so
127  */
128  inline bool isEmpty() const;
129 
130  /**
131  * Returns the binary sub-region mask, if any
132  * @return Sub-region mask, otherwise an invalid frame is returned
133  */
134  inline const Frame& mask() const;
135 
136  /**
137  * Returns the 2D triangles of this sub-region, if any
138  * @return 2D sub-region triangles
139  */
140  inline const Triangles2& triangles() const;
141 
142  /**
143  * Returns the number of pixels that are covered by this region.
144  * @return The number of pixels, with range [0, infinity)
145  */
146  Scalar size() const;
147 
148  /**
149  * Assign operator.
150  * @param subRegion Sub-region object to be assigned
151  * @return Reference to this object
152  */
153  inline SubRegion& operator=(const SubRegion& subRegion);
154 
155  /**
156  * Move operator.
157  * @param subRegion Sub-region object to be moved
158  * @return Reference to this object
159  */
160  inline SubRegion& operator=(SubRegion&& subRegion) noexcept;
161 
162  /**
163  * Increases or decreases a sub-region by application of a scalar multiplication factor.
164  * @param factor The multiplication factor defining the new sub-region, with range [0, infinity)
165  * @return Resulting sub-region with new location and size due to the provided factor
166  */
167  SubRegion operator*(const Scalar factor) const;
168 
169  /**
170  * Transforms a sub-region by application of a homography that transforms points in the initial frame to points in the new frame.
171  * @param homography The homography transforming points defined in the coordinate system of the initial frame into points defined in the coordinate system of the new frame.
172  * @return Resulting transformed subregion
173  */
174  SubRegion operator*(const SquareMatrix3& homography) const;
175 
176  /**
177  * Returns whether this sub-region is not empty.
178  * @return True, if so
179  */
180  explicit inline operator bool() const;
181 
182  private:
183 
184  /// A set of 2D triangles defining the sub-region.
186 
187  /// A binary frame defining the sub-region per pixel.
189 
190  /// The pixel value defining the sub-region.
191  uint8_t maskValue_ = 0x00u;
192 
193  /// Bounding box of this sub-region.
195 };
196 
198  maskValue_(0x00)
199 {
200  // nothing to do here
201 }
202 
203 inline SubRegion::SubRegion(const SubRegion& subRegion) :
204  triangles_(subRegion.triangles_),
205  mask_(subRegion.mask_),
206  maskValue_(subRegion.maskValue_),
207  boundingBox_(subRegion.boundingBox_)
208 {
209  // nothing to do here
210 }
211 
212 inline SubRegion::SubRegion(SubRegion&& subRegion) noexcept :
213  SubRegion()
214 {
215  *this = std::move(subRegion);
216 }
217 
218 inline const Box2& SubRegion::boundingBox() const
219 {
220  return boundingBox_;
221 }
222 
223 inline bool SubRegion::isEmpty() const
224 {
225  return triangles_.empty() && !mask_.isValid();
226 }
227 
228 inline const Frame& SubRegion::mask() const
229 {
230  return mask_;
231 }
232 
233 inline const Triangles2& SubRegion::triangles() const
234 {
235  return triangles_;
236 }
237 
238 inline SubRegion& SubRegion::operator=(const SubRegion& subRegion)
239 {
240  triangles_ = subRegion.triangles_;
241  mask_ = subRegion.mask_;
242  maskValue_ = subRegion.maskValue_;
243  boundingBox_ = subRegion.boundingBox_;
244 
245  return *this;
246 }
247 
248 inline SubRegion& SubRegion::operator=(SubRegion&& subRegion) noexcept
249 {
250  if (this != &subRegion)
251  {
252  triangles_ = std::move(subRegion.triangles_);
253  mask_ = std::move(subRegion.mask_);
254  maskValue_ = subRegion.maskValue_;
255  boundingBox_ = subRegion.boundingBox_;
256 
257  subRegion.boundingBox_ = Box2();
258  }
259 
260  return *this;
261 }
262 
263 inline SubRegion::operator bool() const
264 {
265  return !isEmpty();
266 }
267 
268 }
269 
270 }
271 
272 #endif // META_OCEAN_CV_ADVANCED_SUB_REGION_H
This class implement a sub-region either defined by 2D triangles or defined by a binary mask.
Definition: SubRegion.h:32
SubRegion operator*(const Scalar factor) const
Increases or decreases a sub-region by application of a scalar multiplication factor.
SubRegion(const Scalar left, const Scalar top, const Scalar width, const Scalar height)
Creates a rectangular sub-region which will be handled (internally) as two triangles with one common ...
const Box2 & boundingBox() const
Returns the bounding box of this sub-region.
Definition: SubRegion.h:218
Frame mask_
A binary frame defining the sub-region per pixel.
Definition: SubRegion.h:188
Vectors2 filterPoints(const Vectors2 &points) const
Filters a set of given image points so that all resulting points ly inside this sub-region.
const Frame & mask() const
Returns the binary sub-region mask, if any.
Definition: SubRegion.h:228
SubRegion(Frame &&mask, const PixelBoundingBox &boundingBox=PixelBoundingBox(), const uint8_t maskValue=0x00)
Creates a sub-region object by a given binary mask.
const Triangles2 & triangles() const
Returns the 2D triangles of this sub-region, if any.
Definition: SubRegion.h:233
SubRegion(const PixelBoundingBox &boundingBox)
Creates a rectangular sub-region which will be handled (internally) as two triangles with one common ...
Box2 boundingBox_
Bounding box of this sub-region.
Definition: SubRegion.h:194
SubRegion(const Frame &mask, const PixelBoundingBox &boundingBox=PixelBoundingBox(), const uint8_t maskValue=0x00)
Creates a sub-region object by a given binary mask.
SubRegion(const Triangle2 *triangles, const size_t size)
Creates a sub-region object by a given set of 2D triangles.
Triangles2 triangles_
A set of 2D triangles defining the sub-region.
Definition: SubRegion.h:185
SubRegion(const Box2 &boundingBox)
Creates a rectangular sub-region which will be handled (internally) as two triangles with one common ...
bool isEmpty() const
Returns whether this sub-region is empty.
Definition: SubRegion.h:223
SubRegion & operator=(const SubRegion &subRegion)
Assign operator.
Definition: SubRegion.h:238
SubRegion(const Triangles2 &triangles)
Creates a sub-region object by a given set of 2D triangles.
SubRegion()
Creates an empty sub-region.
Definition: SubRegion.h:197
SubRegion operator*(const SquareMatrix3 &homography) const
Transforms a sub-region by application of a homography that transforms points in the initial frame to...
bool isInside(const Vector2 &point) const
Returns whether a given point lies inside this sub-region.
Scalar size() const
Returns the number of pixels that are covered by this region.
uint8_t maskValue_
The pixel value defining the sub-region.
Definition: SubRegion.h:191
This class implements Ocean's image class.
Definition: Frame.h:1760
bool isValid() const
Returns whether this frame is valid.
Definition: Frame.h:4416
This class implements a 2D triangle with Cartesian coordinates.
Definition: Triangle2.h:81
PixelBoundingBoxT< unsigned int > PixelBoundingBox
Definition of the default PixelBoundingBox object with data type allowing only positive coordinate va...
Definition: PixelBoundingBox.h:21
float Scalar
Definition of a scalar type.
Definition: Math.h:128
std::vector< Triangle2 > Triangles2
Definition of a vector holding 2D triangles.
Definition: Triangle2.h:57
std::vector< Vector2 > Vectors2
Definition of a vector holding Vector2 objects.
Definition: Vector2.h:64
BoxT2< Scalar > Box2
Definition of the Box2 object, depending on the OCEAN_MATH_USE_SINGLE_PRECISION either with single or...
Definition: Box2.h:22
The namespace covering the entire Ocean framework.
Definition: Accessor.h:15