Ocean
Loading...
Searching...
No Matches
MappingI.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_SYNTHESIS_MAPPING_I_H
9#define META_OCEAN_CV_SYNTHESIS_MAPPING_I_H
10
13
15
16#include <algorithm>
17#include <memory>
18
19namespace Ocean
20{
21
22namespace CV
23{
24
25namespace Synthesis
26{
27
28/**
29 * This class implements a mapping with integer accuracy.
30 * @ingroup cvsynthesis
31 */
32class OCEAN_CV_SYNTHESIS_EXPORT MappingI : public Mapping
33{
34 friend class MappingF1;
35
36 public:
37
38 /**
39 * Destructs a mapping object.
40 */
41 inline ~MappingI() override;
42
43 /**
44 * Returns the mapping for a given position.
45 * @param x Horizontal position to return the mapping for, with range [0, width - 1]
46 * @param y Vertical position to return the mapping for, with range [0, height - 1]
47 * @return Current mapping for the specified position
48 */
49 inline const PixelPosition& position(const unsigned int x, const unsigned int y) const;
50
51 /**
52 * Returns the mapping for a given position.
53 * @param x Horizontal position to return the mapping for, with range [0, width - 1]
54 * @param y Vertical position to return the mapping for, with range [0, height - 1]
55 * @return Current mapping for the specified position
56 */
57 inline PixelPosition& position(const unsigned int x, const unsigned int y);
58
59 /**
60 * Returns the mapping for a given position.
61 * @param location The location for which the mapping will be returned, with range [0, width-1]x[0, height-1]
62 * @return Current mapping for the specified position
63 */
64 inline const PixelPosition& position(const CV::PixelPosition& location) const;
65
66 /**
67 * Returns the mapping for a given position.
68 * @param location The location for which the mapping will be returned, with range [0, width-1]x[0, height-1]
69 * @return Current mapping for the specified position
70 */
71 inline PixelPosition& position(const CV::PixelPosition& location);
72
73 /**
74 * Sets a new mapping for a specified position.
75 * @param x Horizontal position to set the mapping for, with range [0, width - 1]
76 * @param y Vertical position to set the mapping for, with range [0, height - 1]
77 * @param pixelPosition New mapping to be set
78 */
79 inline void setPosition(const unsigned int x, const unsigned int y, const PixelPosition& pixelPosition);
80
81 /**
82 * Returns the pointer to a mapping row.
83 * @param y The index of the row to return, with range [0, height - 1]
84 * @return The mapping row
85 */
86 inline const PixelPosition* row(const unsigned int y) const;
87
88 /**
89 * Returns the pointer to a mapping row.
90 * @param y The index of the row to return, with range [0, height - 1]
91 * @return The mapping row
92 */
93 inline PixelPosition* row(const unsigned int y);
94
95 /**
96 * Resets the stored mapping.
97 */
98 inline void reset();
99
100 /**
101 * Returns the mappings of this object.
102 * @return All mapping
103 */
104 inline const PixelPosition* operator()() const;
105
106 /**
107 * Returns the mappings of this object.
108 * @return All mapping
109 */
110 inline PixelPosition* operator()();
111
112 protected:
113
114 /**
115 * Creates an empty mapping object.
116 */
117 MappingI() = default;
118
119 /**
120 * Copies a mapping from a given mapping object.
121 * @param pixelMapping Pixel mapping to be copied
122 */
123 inline MappingI(const MappingI& pixelMapping);
124
125 /**
126 * Moves a mapping from a given mapping object.
127 * @param pixelMapping Pixel mapping to be moved
128 */
129 inline MappingI(MappingI&& pixelMapping) noexcept;
130
131 /**
132 * Creates a new mapping object with defined dimension.
133 * Beware: An initial mapping is not provided.<br>
134 * @param width The width of the mapping object in pixel, with range [1, infinity)
135 * @param height The height of the mapping object in pixel, with range [1, infinity)
136 */
137 inline MappingI(const unsigned int width, const unsigned int height);
138
139 /**
140 * Calculates the sum of square differences between two 5x5 frame regions in two frames with explicit weighted mask pixels.<br>
141 * Compared to the standard ssd calculation this extended version weights the square difference of mask pixels (value not equal to 0xFF) with a given factor, further the center pixel is not considered.
142 * @param frame0 Pointer to the top left position in the 5x5 region in the first frame
143 * @param frame1 Pointer to the top left position in the 5x5 region in the second frame
144 * @param mask0 Pointer to the top left position in the 5x5 region in the mask frame, with 0xFF defining a non-mask pixel
145 * @param width0 Width of the first frame in pixel, with range [5, infinity)
146 * @param width1 Width of the second frame in pixel, with range [5, infinity)
147 * @param frame0PaddingElements The number of padding elements at the end of each first frame row, in elements, with range [0, infinity)
148 * @param frame1PaddingElements The number of padding elements at the end of each second frame row, in elements, with range [0, infinity)
149 * @param mask0PaddingElements The number of padding elements at the end of each mask row, in elements, with range [0, infinity)
150 * @return Resulting sum of squared differences
151 * @tparam tChannels Number of frame channels
152 * @tparam tBorderFactor Multiplication factor for squared differences of border pixels, with range [1, infinity)
153 */
154 template <unsigned int tChannels, unsigned int tBorderFactor>
155 static inline unsigned int ssd5x5MaskNoCenter(const uint8_t* frame0, const uint8_t* frame1, const uint8_t* mask0, const unsigned int width0, const unsigned int width1, const unsigned int frame0PaddingElements, const unsigned int frame1PaddingElements, const unsigned int mask0PaddingElements);
156
157 /**
158 * Assign operator.
159 * @param pixelMapping Mapping object to be copied
160 * @return Reference to this object
161 */
162 inline MappingI& operator=(const MappingI& pixelMapping);
163
164 /**
165 * Move operator.
166 * @param pixelMapping Mapping object to be moved
167 * @return Reference to this object
168 */
169 inline MappingI& operator=(MappingI&& pixelMapping) noexcept;
170
171 protected:
172
173 /// Pixel mappings for each pixel.
174 PixelPosition* mappingI_ = nullptr;
175};
176
177inline MappingI::MappingI(const MappingI& pixelMapping) :
178 Mapping(pixelMapping.width_, pixelMapping.height_),
179 mappingI_(nullptr)
180{
181 const unsigned int size = width_ * height_;
182
183 if (size != 0u)
184 {
185 mappingI_ = (PixelPosition*)malloc(size * sizeof(PixelPosition));
186 ocean_assert(mappingI_ != nullptr);
187
188 std::uninitialized_copy(pixelMapping.mappingI_, pixelMapping.mappingI_ + size, mappingI_);
189 }
190}
191
192inline MappingI::MappingI(MappingI&& pixelMapping) noexcept :
193 Mapping(std::move(pixelMapping)),
194 mappingI_(nullptr)
195{
196 mappingI_ = pixelMapping.mappingI_;
197 pixelMapping.mappingI_ = nullptr;
198}
199
200inline MappingI::MappingI(const unsigned int width, const unsigned int height) :
201 Mapping(width, height),
202 mappingI_(nullptr)
203{
204 const unsigned int size = width * height;
205
206 if (size != 0)
207 {
208 mappingI_ = (PixelPosition*)malloc(size * sizeof(PixelPosition));
209 ocean_assert(mappingI_ != nullptr);
210 }
211}
212
214{
215 free(mappingI_);
216}
217
218inline const PixelPosition& MappingI::position(const unsigned int x, const unsigned int y) const
219{
220 ocean_assert(x < width_ && y < height_);
221 return mappingI_[y * width_ + x];
222}
223
224inline PixelPosition& MappingI::position(const unsigned int x, const unsigned int y)
225{
226 ocean_assert(x < width_ && y < height_);
227 return mappingI_[y * width_ + x];
228}
229
230inline const PixelPosition& MappingI::position(const CV::PixelPosition& location) const
231{
232 return position(location.x(), location.y());
233}
234
236{
237 return position(location.x(), location.y());
238}
239
240inline void MappingI::setPosition(const unsigned int x, const unsigned int y, const PixelPosition& pixelPosition)
241{
242 ocean_assert(x < width_ && y < height_);
243 mappingI_[y * width_ + x] = pixelPosition;
244}
245
246inline const PixelPosition* MappingI::row(const unsigned int y) const
247{
248 ocean_assert(y < height_);
249
250 return mappingI_ + y * width_;
251}
252
253inline PixelPosition* MappingI::row(const unsigned int y)
254{
255 ocean_assert(y < height_);
256
257 return mappingI_ + y * width_;
258}
259
260inline void MappingI::reset()
261{
262 ocean_assert(mappingI_);
264}
265
267{
268 return mappingI_;
269}
270
272{
273 return mappingI_;
274}
275
276template <unsigned int tChannels, unsigned int tBorderFactor>
277inline unsigned int MappingI::ssd5x5MaskNoCenter(const uint8_t* frame0, const uint8_t* frame1, const uint8_t* mask0, const unsigned int width0, const unsigned int width1, const unsigned int frame0PaddingElements, const unsigned int frame1PaddingElements, const unsigned int mask0PaddingElements)
278{
279 static_assert(tBorderFactor >= 1u, "Invalid border factor!");
280
281 ocean_assert(frame0 != nullptr && frame1 != nullptr && mask0 != nullptr);
282 ocean_assert(width0 >= 5u && width1 >= 5u);
283
284 const unsigned int frame0StrideElements = width0 * tChannels + frame0PaddingElements;
285 const unsigned int frame1StrideElements = width1 * tChannels + frame1PaddingElements;
286 const unsigned int mask0StrideElements = width0 + mask0PaddingElements;
287
288 unsigned int ssd = 0u;
289
290 for (unsigned int y = 0u; y < 5u; ++y)
291 {
292 for (unsigned int x = 0u; x < 5u; ++x)
293 {
294 if (x == 2u && y == 2u)
295 {
296 // skip the center pixel
297 continue;
298 }
299
300 unsigned int local = 0u;
301
302 for (unsigned int n = 0u; n < tChannels; ++n)
303 {
304 const int value = int(frame0[x * tChannels + n] - frame1[x * tChannels + n]);
305 local += value * value;
306 }
307
308 if (mask0[x] == 0xFFu)
309 {
310 local *= tBorderFactor;
311 }
312
313 ssd += local;
314 }
315
316 frame0 += frame0StrideElements;
317 frame1 += frame1StrideElements;
318 mask0 += mask0StrideElements;
319 }
320
321 return ssd;
322}
323
324inline MappingI& MappingI::operator=(const MappingI& pixelMapping)
325{
326 if (this != &pixelMapping)
327 {
328 if (mappingI_)
329 {
330 free(mappingI_);
331 mappingI_ = nullptr;
332 }
333
334 Mapping::operator=(pixelMapping);
335
336 const unsigned int size = width_ * height_;
337
338 if (size != 0u)
339 {
340 mappingI_ = (PixelPosition*)malloc(size * sizeof(PixelPosition));
341 ocean_assert(mappingI_ != nullptr);
342
343 std::uninitialized_copy(pixelMapping.mappingI_, pixelMapping.mappingI_ + size, mappingI_);
344 }
345 }
346
347 return *this;
348}
349
350inline MappingI& MappingI::operator=(MappingI&& pixelMapping) noexcept
351{
352 if (this != &pixelMapping)
353 {
354 Mapping::operator=(std::move(pixelMapping));
355
356 mappingI_ = pixelMapping.mappingI_;
357 pixelMapping.mappingI_ = nullptr;
358 }
359
360 return *this;
361}
362
363}
364
365}
366
367}
368
369#endif // META_OCEAN_CV_SYNTHESIS_MAPPING_I_1_H
T y() const
Returns the vertical coordinate position of this object.
Definition PixelPosition.h:468
T x() const
Returns the horizontal coordinate position of this object.
Definition PixelPosition.h:456
Cost function:
Definition MappingF1.h:52
This class is the base class for all mappings.
Definition Mapping.h:35
unsigned int width_
Width of this pixel mapping object in pixel.
Definition Mapping.h:147
unsigned int width() const
Returns the width of this mapping object.
Definition Mapping.h:282
Mapping & operator=(const Mapping &mapping)
Assign operator.
Definition Mapping.h:411
unsigned int height_
Height of this pixel mapping object in pixel.
Definition Mapping.h:150
unsigned int height() const
Returns the height of this mapping object.
Definition Mapping.h:287
This class implements a mapping with integer accuracy.
Definition MappingI.h:33
MappingI & operator=(const MappingI &pixelMapping)
Assign operator.
Definition MappingI.h:324
static unsigned int ssd5x5MaskNoCenter(const uint8_t *frame0, const uint8_t *frame1, const uint8_t *mask0, const unsigned int width0, const unsigned int width1, const unsigned int frame0PaddingElements, const unsigned int frame1PaddingElements, const unsigned int mask0PaddingElements)
Calculates the sum of square differences between two 5x5 frame regions in two frames with explicit we...
Definition MappingI.h:277
const PixelPosition * row(const unsigned int y) const
Returns the pointer to a mapping row.
Definition MappingI.h:246
const PixelPosition * operator()() const
Returns the mappings of this object.
Definition MappingI.h:266
MappingI()=default
Creates an empty mapping object.
const PixelPosition & position(const unsigned int x, const unsigned int y) const
Returns the mapping for a given position.
Definition MappingI.h:218
void reset()
Resets the stored mapping.
Definition MappingI.h:260
PixelPosition * mappingI_
Pixel mappings for each pixel.
Definition MappingI.h:174
void setPosition(const unsigned int x, const unsigned int y, const PixelPosition &pixelPosition)
Sets a new mapping for a specified position.
Definition MappingI.h:240
~MappingI() override
Destructs a mapping object.
Definition MappingI.h:213
PixelPositionT< unsigned int > PixelPosition
Definition of the default PixelPosition object with a data type allowing only positive coordinate val...
Definition PixelPosition.h:32
The namespace covering the entire Ocean framework.
Definition Accessor.h:15