Ocean
Loading...
Searching...
No Matches
InitializerAppearanceMappingF1.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_INITIALIZER_APPEARANCE_MAPPING_F_1_H
9#define META_OCEAN_CV_SYNTHESIS_INITIALIZER_APPEARANCE_MAPPING_F_1_H
10
18
20
21#include "ocean/math/Random.h"
22
23namespace Ocean
24{
25
26namespace CV
27{
28
29namespace Synthesis
30{
31
32/**
33 * This class implements an initializer that initializes the mapping by appearance constraints for mappings with float accuracy.
34 * For each mask pixel the best matching target pixel is assigned as initial mapping.<br>
35 * The matching is provided by a randomized test of best matching positions.
36 * @tparam tPatchSize Defines the patch size for the matching, must be 1
37 * @tparam tIterations Defines the number of random seek iterations for each pixel inside the synthesis mask, with range [1, infinity)
38 * @see LayerF1, MappingF.
39 * @ingroup cvsynthesis
40 */
41template <unsigned int tPatchSize, unsigned int tIterations>
43 virtual public InitializerAppearanceMapping,
44 virtual public InitializerF,
45 virtual public InitializerRandomized,
46 virtual public InitializerSubset,
47 virtual public Initializer1
48{
49 public:
50
51 /**
52 * Creates a new initializer object.
53 * @param layer The layer for that the initial mapping has to be provided
54 * @param randomGenerator Random number generator
55 */
56 inline InitializerAppearanceMappingF1(LayerF1& layer, RandomGenerator& randomGenerator);
57
58 private:
59
60 /**
61 * Initializes a subset of the entire mapping area.
62 * @see InitializerSubset::initializeSubset().
63 * @see initializeSubsetChannels().
64 */
65 void initializeSubset(const unsigned int firstColumn, const unsigned int numberColumns, const unsigned int firstRow, const unsigned int numberRows) const override;
66
67 /**
68 * This function is the specialization of the default initializeSubset() function.
69 * @param firstColumn First column of the mapping area to be initialized
70 * @param numberColumns Number of columns of the mapping area to be handled
71 * @param firstRow First row of the mapping area to be initialized
72 * @param numberRows Number of rows of the mapping area to be handled
73 * @tparam tChannels Number of channels the frame has
74 * @see initializeSubset().
75 */
76 template <unsigned int tChannels>
77 void initializeSubsetChannels(const unsigned int firstColumn, const unsigned int numberColumns, const unsigned int firstRow, const unsigned int numberRows) const;
78};
79
80template <unsigned int tPatchSize, unsigned int tIterations>
82 Initializer(layer),
84 InitializerF(layer),
85 InitializerRandomized(layer, randomGenerator),
86 InitializerSubset(layer),
87 Initializer1(layer)
88{
89 // nothing to do here
90}
91
92template <unsigned int tPatchSize, unsigned int tIterations>
93void InitializerAppearanceMappingF1<tPatchSize, tIterations>::initializeSubset(const unsigned int firstColumn, const unsigned int numberColumns, const unsigned int firstRow, const unsigned int numberRows) const
94{
95 static_assert(tPatchSize == 1u, "Invalid patch size!");
96
97 ocean_assert(layerF_.frame().numberPlanes() == 1u);
98 ocean_assert(layer_.frame().dataType() == FrameType::DT_UNSIGNED_INTEGER_8);
99
100 switch (layerF_.frame().channels())
101 {
102 case 1u:
103 initializeSubsetChannels<1u>(firstColumn, numberColumns, firstRow, numberRows);
104 break;
105
106 case 2u:
107 initializeSubsetChannels<2u>(firstColumn, numberColumns, firstRow, numberRows);
108 break;
109
110 case 3u:
111 initializeSubsetChannels<3u>(firstColumn, numberColumns, firstRow, numberRows);
112 break;
113
114 case 4u:
115 initializeSubsetChannels<4u>(firstColumn, numberColumns, firstRow, numberRows);
116 break;
117
118 default:
119 ocean_assert(false && "Invalid frame type.");
120 }
121}
122
123template <unsigned int tPatchSize, unsigned int tIterations>
124template <unsigned int tChannels>
125void InitializerAppearanceMappingF1<tPatchSize, tIterations>::initializeSubsetChannels(const unsigned int firstColumn, const unsigned int numberColumns, const unsigned int firstRow, const unsigned int numberRows) const
126{
127 static_assert(tPatchSize == 1, "Missing Implementation: Currently tPatchSize must be 1");
128 static_assert(tChannels >= 1u, "Invalid channel number!");
129
130 const unsigned int tPatchSize_2 = tPatchSize / 2u;
131
132 const unsigned int width = layerF_.width();
133 const unsigned int height = layerF_.height();
134
135 MappingF& layerMapping = layerF_.mapping();
136
137 const Frame& frame = layerF_.frame();
138 const Frame& mask = layerF_.mask();
139
140 ocean_assert(frame.isValid() && mask.isValid());
141
142 ocean_assert(frame.numberPlanes() == 1u && frame.dataType() == FrameType::DT_UNSIGNED_INTEGER_8);
143 ocean_assert(frame.width() == width);
144 ocean_assert(frame.height() == height);
145
146 ocean_assert(frame.isFrameTypeCompatible(FrameType(mask, frame.pixelFormat()), false));
147
148 RandomGenerator randomGenerator(randomGenerator_);
149
150 const uint8_t* const frameData = frame.constdata<uint8_t>();
151 const uint8_t* const maskData = mask.constdata<uint8_t>();
152
153 const unsigned int framePaddingElements = frame.paddingElements();
154 const unsigned int maskStrideElements = mask.strideElements();
155
156#ifdef OCEAN_DEBUG
157 const PixelBoundingBox& debugLayerBoundingBox = layerF_.boundingBox();
158 ocean_assert(!debugLayerBoundingBox || firstColumn >= debugLayerBoundingBox.left());
159 ocean_assert(!debugLayerBoundingBox || firstColumn + numberColumns <= debugLayerBoundingBox.rightEnd());
160 ocean_assert(!debugLayerBoundingBox || firstRow >= debugLayerBoundingBox.top());
161 ocean_assert(!debugLayerBoundingBox || firstRow + numberRows <= debugLayerBoundingBox.bottomEnd());
162#endif // OCEAN_DEBUG
163
164 ocean_assert(firstColumn + numberColumns <= width);
165 ocean_assert(firstRow + numberRows <= height);
166
167 const Scalar xLocationMax = Scalar(width - tPatchSize_2 - 1u) - Numeric::weakEps();
168 const Scalar yLocationMax = Scalar(height - tPatchSize_2 - 1u) - Numeric::weakEps();
169
170 for (unsigned int y = firstRow; y < std::min(firstRow + numberRows, height - 1u); ++y)
171 {
172 const uint8_t* maskRow = maskData + y * maskStrideElements + firstColumn;
173 Vector2* position = layerMapping.row(y) + firstColumn;
174
175 for (unsigned int x = firstColumn; x < std::min(firstColumn + numberColumns, width - 1u); ++x)
176 {
177 if (*maskRow++ != 0xFFu)
178 {
179 Scalar bestX, bestY;
180
181 do
182 {
183 bestX = Random::scalar(randomGenerator, Scalar(tPatchSize_2), xLocationMax);
184 bestY = Random::scalar(randomGenerator, Scalar(tPatchSize_2), yLocationMax);
185 }
186 while (maskData[Numeric::round32(bestY) * int(maskStrideElements) + Numeric::round32(bestX)] != 0xFF);
187
188 unsigned int bestSSD = Advanced::AdvancedSumSquareDifferences::patch8BitPerChannel<tChannels, tPatchSize>(frameData, frameData, width, width, Scalar(x), Scalar(y), bestX, bestY, framePaddingElements, framePaddingElements)
189 + (unsigned int)(sqrDistance(Scalar(x), Scalar(bestX)) + sqrDistance(Scalar(y), Scalar(bestY))) / 2u;
190
191 for (unsigned int n = 1u; n < tIterations; ++n)
192 {
193 const Scalar candidateX = Random::scalar(randomGenerator, Scalar(tPatchSize_2), xLocationMax);
194 const Scalar candidateY = Random::scalar(randomGenerator, Scalar(tPatchSize_2), yLocationMax);
195
196 if (maskData[Numeric::round32(candidateY) * int(maskStrideElements) + Numeric::round32(candidateX)] != 0xFF)
197 {
198 continue;
199 }
200
201 const unsigned int candidateSSD = Advanced::AdvancedSumSquareDifferences::patch8BitPerChannel<tChannels, tPatchSize>(frameData, frameData, width, width, Scalar(x), Scalar(y), candidateX, candidateY, framePaddingElements, framePaddingElements)
202 + (unsigned int)(sqrDistance(Scalar(x), Scalar(candidateX)) + sqrDistance(Scalar(y), Scalar(candidateY))) / 2u;
203
204 if (candidateSSD < bestSSD)
205 {
206 bestX = candidateX;
207 bestY = candidateY;
208 bestSSD = candidateSSD;
209 }
210 }
211
212 *position = Vector2(bestX, bestY);
213 }
214
215 ++position;
216 }
217 }
218}
219
220}
221
222}
223
224}
225
226#endif // META_OCEAN_CV_SYNTHESIS_INITIALIZER_APPEARANCE_MAPPING_F_1_H
static uint32_t patch8BitPerChannel(const uint8_t *image0, const uint8_t *image1, const unsigned int width0, const unsigned int width1, const Scalar centerX0, const Scalar centerY0, const Scalar centerX1, const Scalar centerY1, const unsigned int image0PaddingElements, const unsigned int image1PaddingElements)
Returns the sum of square differences for an image patch block determined for two sub-pixel positions...
T left() const
Returns the left (including) pixel position of this bounding box.
Definition PixelBoundingBox.h:416
T rightEnd() const
Returns the right (excluding) pixel position of this bounding box.
Definition PixelBoundingBox.h:437
T bottomEnd() const
Returns the bottom (excluding) pixel position of this bounding box.
Definition PixelBoundingBox.h:451
T top() const
Returns the top (including) pixel position of this bounding box.
Definition PixelBoundingBox.h:423
Initializer1(Layer &layer)
Creates a new initializer object.
Definition Initializer1.h:39
InitializerAppearanceMappingF1(LayerF1 &layer, RandomGenerator &randomGenerator)
Creates a new initializer object.
Definition InitializerAppearanceMappingF1.h:81
void initializeSubset(const unsigned int firstColumn, const unsigned int numberColumns, const unsigned int firstRow, const unsigned int numberRows) const override
Initializes a subset of the entire mapping area.
Definition InitializerAppearanceMappingF1.h:93
void initializeSubsetChannels(const unsigned int firstColumn, const unsigned int numberColumns, const unsigned int firstRow, const unsigned int numberRows) const
This function is the specialization of the default initializeSubset() function.
Definition InitializerAppearanceMappingF1.h:125
InitializerAppearanceMapping(Layer &layer)
Creates a new initializer object.
Definition InitializerAppearanceMapping.h:38
InitializerF(LayerF &layer)
Creates a new initializer object.
Definition InitializerF.h:45
LayerF & layerF_
Specialized layer reference.
Definition InitializerF.h:42
This class implements the base class for all synthesis initializers.
Definition Initializer.h:34
Layer & layer_
Synthesis layer that has to be initialized.
Definition Initializer.h:55
RandomGenerator & randomGenerator_
Random number generator.
Definition InitializerRandomized.h:43
InitializerRandomized(Layer &layer, RandomGenerator &randomGenerator)
Creates a new initializer object.
Definition InitializerRandomized.h:46
InitializerSubset(Layer &layer)
Creates a new initializer object.
Definition InitializerSubset.h:57
This class implements a single layer for pixel synthesis within one frame and sub-pixel accuracy.
Definition LayerF1.h:44
This class implements a mapping with float accuracy.
Definition MappingF.h:33
const Vector2 * row(const unsigned int y) const
Returns the pointer to a mapping row.
Definition MappingF.h:213
This class implements Ocean's image class.
Definition Frame.h:1969
unsigned int strideElements(const unsigned int planeIndex=0u) const
Returns the number of elements within one row, including optional padding at the end of a row for a s...
Definition Frame.h:4315
const T * constdata(const unsigned int planeIndex=0u) const
Returns a pointer to the read-only pixel data of a specific plane.
Definition Frame.h:4425
bool isValid() const
Returns whether this frame is valid.
Definition Frame.h:4705
unsigned int paddingElements(const unsigned int planeIndex=0u) const
Returns the optional number of padding elements at the end of each row for a specific plane.
Definition Frame.h:4299
Definition of a frame type composed by the frame dimension, pixel format and pixel origin.
Definition Frame.h:30
unsigned int width() const
Returns the width of the frame format in pixel.
Definition Frame.h:3334
uint32_t numberPlanes() const
Returns the number of planes of the pixel format of this frame.
Definition Frame.h:3374
PixelFormat pixelFormat() const
Returns the pixel format of the frame.
Definition Frame.h:3344
@ DT_UNSIGNED_INTEGER_8
Unsigned 8 bit integer data type (uint8_t).
Definition Frame.h:41
unsigned int height() const
Returns the height of the frame in pixel.
Definition Frame.h:3339
DataType dataType() const
Returns the data type of the pixel format of this frame.
Definition Frame.h:3354
bool isFrameTypeCompatible(const FrameType &frameType, const bool allowDifferentPixelOrigins) const
Returns whether this frame type is compatible with a given frame type.
Definition Frame.h:3401
static constexpr Scalar weakEps()
static constexpr int32_t round32(const Scalar value)
This class implements a generator for random numbers.
Definition RandomGenerator.h:42
static Scalar scalar(const Scalar lower, const Scalar upper)
unsigned int sqrDistance(const char first, const char second)
Returns the square distance between two values.
Definition base/Utilities.h:1159
PixelBoundingBoxT< unsigned int > PixelBoundingBox
Definition of the default PixelBoundingBox object with data type allowing only positive coordinate va...
Definition PixelBoundingBox.h:28
VectorT2< Scalar > Vector2
Definition of a 2D vector.
Definition Vector2.h:28
float Scalar
Definition of a scalar type.
Definition Math.h:129
Namespace of the CV Synthesis library.
Definition Constraint.h:25
Namespace of the CV library.
Definition Advanced.h:17
The namespace covering the entire Ocean framework.
Definition Accessor.h:15