Ocean
CreatorInformationSpatialCostI1.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_CREATOR_INFORMATION_SPATIAL_COST_I_1_H
9 #define META_OCEAN_CV_SYNTHESIS_CREATOR_INFORMATION_SPATIAL_COST_I_1_H
10 
16 
17 namespace Ocean
18 {
19 
20 namespace CV
21 {
22 
23 namespace Synthesis
24 {
25 
26 /**
27  * This class implements a creator object that creates a visual representation of the spatial mapping cost.
28  * The information output is a frame visualizing target pixels with zero and non-zero spatial mapping cost.<br>
29  * The template parameter defines the size of the neighborhood that is used for the output.
30  * @tparam tNeighborhood Number of neighbors that must have perfect mapping, with range [1, 4]
31  * @tparam tOnlyCenterPixels True, if only center pixels will be considered
32  * @ingroup cvsynthesis
33  */
34 template <unsigned int tNeighborhood, bool tOnlyCenterPixels>
36  virtual public CreatorFrame,
37  virtual public CreatorI,
38  virtual public CreatorSubset,
39  virtual public Creator1
40 {
41  public:
42 
43  /**
44  * Creates a new creator object.
45  * @param layer The layer that is used to create the information
46  * @param target The target frame that will receive the creator output
47  */
48  inline CreatorInformationSpatialCostI1(const LayerI1& layer, Frame& target);
49 
50  protected:
51 
52  /**
53  * Creates a subset of the information.
54  * @see CreatorSubset::createSubset().
55  */
56  void createSubset(const unsigned int firstColumn, const unsigned int numberColumns, const unsigned int firstRow, const unsigned int numberRows) const override;
57 
58  /**
59  * Specialization of the default function that creates a subset of the information.
60  * The template parameter specifies the number of channels of the target frame.<br>
61  * @param firstColumn First column to be handled
62  * @param numberColumns Number of columns to be handled
63  * @param firstRow First row to be handled
64  * @param numberRows Number of rows to be handled
65  * @tparam tChannels Number of data channels of the frame
66  */
67  template <unsigned int tChannels>
68  void createSubsetChannels(const unsigned int firstColumn, const unsigned int numberColumns, const unsigned int firstRow, const unsigned int numberRows) const;
69 };
70 
71 template <unsigned int tNeighborhood, bool tOnlyCenterPixels>
73  Creator(layer),
74  CreatorFrame(layer, target),
75  CreatorI(layer),
76  CreatorSubset(layer),
77  Creator1(layer)
78 {
79  // nothing to do here
80 }
81 
82 template <unsigned int tNeighborhood, bool tOnlyCenterPixels>
83 void CreatorInformationSpatialCostI1<tNeighborhood, tOnlyCenterPixels>::createSubset(const unsigned int firstColumn, const unsigned int numberColumns, const unsigned int firstRow, const unsigned int numberRows) const
84 {
85  ocean_assert(target_.numberPlanes() == 1u);
86 
87  switch (target_.channels())
88  {
89  case 1u:
90  createSubsetChannels<1u>(firstColumn, numberColumns, firstRow, numberRows);
91  break;
92 
93  default:
94  ocean_assert(false && "Invalid frame type.");
95  }
96 }
97 
98 template <unsigned int tNeighborhood, bool tOnlyCenterPixels>
99 template <unsigned int tChannels>
100 void CreatorInformationSpatialCostI1<tNeighborhood, tOnlyCenterPixels>::createSubsetChannels(const unsigned int firstColumn, const unsigned int numberColumns, const unsigned int firstRow, const unsigned int numberRows) const
101 {
102  static_assert(tNeighborhood >= 1u && tNeighborhood <= 4u, "Invalid number of neighbors!");
103  static_assert(tChannels == 1u, "Invalid channel number!");
104 
105  ocean_assert(target_.isValid());
106 
107  const unsigned int layerWidth = layerI_.width();
108  const unsigned int layerHeight = layerI_.height();
109 
110  const MappingI& layerMapping = layerI_.mapping();
111 
112  const Frame& layerMask = layerI_.mask();
113 
114  const unsigned int maskStrideElements = layerMask.strideElements();
115 
116  for (unsigned int y = firstRow; y < firstRow + numberRows; ++y)
117  {
118  uint8_t* targetRow = target_.row<uint8_t>(y) + firstColumn;
119  const uint8_t* maskPixel = layerI_.mask().template constrow<uint8_t>(y) + firstColumn;
120 
121  for (unsigned int x = firstColumn; x < firstColumn + numberColumns; ++x)
122  {
123  if (*maskPixel != 0xFFu)
124  {
125  unsigned int counter = 0u;
126 
127  const PixelPosition& position = layerMapping.position(x, y);
128  ocean_assert(position);
129 
130  // top
131  if (y > 0u && *(maskPixel - maskStrideElements) != 0xFFu)
132  {
133  if (layerMapping.position(x, y - 1u) == position.north())
134  {
135  ++counter;
136  }
137  }
138  else if (!tOnlyCenterPixels)
139  {
140  ++counter;
141  }
142 
143  // left
144  if (x > 0u && *(maskPixel - 1) != 0xFFu)
145  {
146  if (layerMapping.position(x - 1u, y) == position.west())
147  {
148  ++counter;
149  }
150  }
151  else if (!tOnlyCenterPixels)
152  {
153  ++counter;
154  }
155 
156  // bottom
157  if (y < layerHeight - 1u && *(maskPixel + maskStrideElements) != 0xFFu)
158  {
159  if (layerMapping.position(x, y + 1u) == position.south())
160  {
161  ++counter;
162  }
163  }
164  else if (!tOnlyCenterPixels)
165  {
166  ++counter;
167  }
168 
169  // right
170  if (x < layerWidth - 1u && *(maskPixel + 1) != 0xFFu)
171  {
172  if (layerMapping.position(x + 1u, y) == position.east())
173  {
174  ++counter;
175  }
176  }
177  else if (!tOnlyCenterPixels)
178  {
179  ++counter;
180  }
181 
182  if (counter >= tNeighborhood)
183  {
184  *targetRow = 0x80u;
185  }
186  else
187  {
188  *targetRow = 0x00u;
189  }
190  }
191 
192  ++targetRow;
193  ++maskPixel;
194  }
195  }
196 }
197 
198 }
199 
200 }
201 
202 }
203 
204 #endif // META_OCEAN_CV_SYNTHESIS_CREATOR_INFORMATION_SPATIAL_COST_I_1_H
PixelPositionT< T > west() const
Returns the pixel position west to this position.
Definition: PixelPosition.h:561
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
PixelPositionT< T > south() const
Returns the pixel position south to this position.
Definition: PixelPosition.h:573
This class implements the base class for all creators that support mappings for one frame.
Definition: Creator1.h:28
This class implements the base class for all creator objects that create a resulting frame as output.
Definition: CreatorFrame.h:28
This class implements the base class for all creators.
Definition: Creator.h:29
This class is the base class for all creators that support mappings with integer accuracy.
Definition: CreatorI.h:29
This class implements a creator object that creates a visual representation of the spatial mapping co...
Definition: CreatorInformationSpatialCostI1.h:40
CreatorInformationSpatialCostI1(const LayerI1 &layer, Frame &target)
Creates a new creator object.
Definition: CreatorInformationSpatialCostI1.h:72
void createSubsetChannels(const unsigned int firstColumn, const unsigned int numberColumns, const unsigned int firstRow, const unsigned int numberRows) const
Specialization of the default function that creates a subset of the information.
Definition: CreatorInformationSpatialCostI1.h:100
void createSubset(const unsigned int firstColumn, const unsigned int numberColumns, const unsigned int firstRow, const unsigned int numberRows) const override
Creates a subset of the information.
Definition: CreatorInformationSpatialCostI1.h:83
This class implements a creator that can be distributed to subsets of the synthesis layer.
Definition: CreatorSubset.h:28
This class implements a single layer for pixel synthesis within one frame and pixel accuracy.
Definition: LayerI1.h:41
This class implements a mapping with integer accuracy.
Definition: MappingI.h:30
const PixelPosition & position(const unsigned int x, const unsigned int y) const
Returns the mapping for a given position.
Definition: MappingI.h:215
This class implements Ocean's image class.
Definition: Frame.h:1760
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:4026
The namespace covering the entire Ocean framework.
Definition: Accessor.h:15