MRPT  2.0.4
TPixelLabelInfo.h
Go to the documentation of this file.
1 /* +------------------------------------------------------------------------+
2  | Mobile Robot Programming Toolkit (MRPT) |
3  | https://www.mrpt.org/ |
4  | |
5  | Copyright (c) 2005-2020, Individual contributors, see AUTHORS file |
6  | See: https://www.mrpt.org/Authors - All rights reserved. |
7  | Released under BSD License. See: https://www.mrpt.org/License |
8  +------------------------------------------------------------------------+ */
9 #pragma once
10 
13 #include <map>
14 #include <memory>
15 #include <string>
16 
17 namespace mrpt::obs
18 {
19 /** Virtual interface to all pixel-label semantic information structs.
20  *
21  * See CObservation3DRangeScan::pixelLabels
22  * \ingroup mrpt_obs_grp
23  */
25 {
26  TPixelLabelInfoBase(unsigned int BITFIELD_BYTES_)
27  : BITFIELD_BYTES(BITFIELD_BYTES_)
28  {
29  }
31 
32  /** Used in CObservation3DRangeScan::pixelLabels */
33  using Ptr = std::shared_ptr<TPixelLabelInfoBase>;
34  using TMapLabelID2Name = std::map<uint32_t, std::string>;
35 
36  /** The 'semantic' or human-friendly name of the i'th bit in
37  * pixelLabels(r,c) can be found in pixelLabelNames[i] as a std::string
38  */
40 
41  const std::string& getLabelName(unsigned int label_idx) const
42  {
43  auto it = pixelLabelNames.find(label_idx);
44  if (it == pixelLabelNames.end())
45  throw std::runtime_error("Error: label index has no defined name");
46  return it->second;
47  }
48  void setLabelName(unsigned int label_idx, const std::string& name)
49  {
50  pixelLabelNames[label_idx] = name;
51  }
52  /** Check the existence of a label by returning its associated index.
53  * -1 if it does not exist. */
54  int checkLabelNameExistence(const std::string& name) const
55  {
56  std::map<uint32_t, std::string>::const_iterator it;
57  for (it = pixelLabelNames.begin(); it != pixelLabelNames.end(); it++)
58  if (it->second == name) return it->first;
59  return -1;
60  }
61 
62  /** Resizes the matrix pixelLabels to the given size, setting all
63  * bitfields to zero (that is, all pixels are assigned NONE category).
64  */
65  virtual void setSize(const int NROWS, const int NCOLS) = 0;
66  /** Mark the pixel(row,col) as classified in the category \a label_idx,
67  * which may be in the range 0 to MAX_NUM_LABELS-1
68  * Note that 0 is a valid label index, it does not mean "no label" \sa
69  * unsetLabel, unsetAll */
70  virtual void setLabel(const int row, const int col, uint8_t label_idx) = 0;
71  virtual void getLabels(const int row, const int col, uint8_t& labels) = 0;
72  /** For the pixel(row,col), removes its classification into the category
73  * \a label_idx, which may be in the range 0 to 7
74  * Note that 0 is a valid label index, it does not mean "no label" \sa
75  * setLabel, unsetAll */
76  virtual void unsetLabel(
77  const int row, const int col, uint8_t label_idx) = 0;
78  /** Removes all categories for pixel(row,col) \sa setLabel, unsetLabel
79  */
80  virtual void unsetAll(const int row, const int col, uint8_t label_idx) = 0;
81  /** Checks whether pixel(row,col) has been clasified into category \a
82  * label_idx, which may be in the range 0 to 7
83  * \sa unsetLabel, unsetAll */
84  virtual bool checkLabel(
85  const int row, const int col, uint8_t label_idx) const = 0;
86 
90 
91  /// std stream interface
92  friend std::ostream& operator<<(
93  std::ostream& out, const TPixelLabelInfoBase& obj)
94  {
95  obj.Print(out);
96  return out;
97  }
98 
99  /** Minimum number of bytes required to hold MAX_NUM_DIFFERENT_LABELS
100  * bits. */
101  const uint8_t BITFIELD_BYTES;
102 
103  protected:
107  virtual void Print(std::ostream&) const = 0;
108 };
109 
110 /** Pixel-wise semantic label struct.
111  *
112  * See CObservation3DRangeScan::pixelLabels
113  * \ingroup mrpt_obs_grp
114  */
115 template <unsigned int BYTES_REQUIRED_>
117 {
118  using Ptr = std::shared_ptr<TPixelLabelInfo>;
119  constexpr static unsigned int BYTES_REQUIRED = BYTES_REQUIRED_;
120 
121  /** Automatically-determined integer type of the proper size such that
122  * all labels fit as one bit (max: 64) */
123  using bitmask_t =
125 
126  /** Each pixel may be assigned between 0 and MAX_NUM_LABELS-1 'labels'
127  * by
128  * setting to 1 the corresponding i'th bit [0,MAX_NUM_LABELS-1] in the
129  * byte in pixelLabels(r,c).
130  * That is, each pixel is assigned an 8*BITFIELD_BYTES bit-wide
131  * bitfield of possible categories.
132  * \sa hasPixelLabels
133  */
136 
137  void setSize(const int NROWS, const int NCOLS) override
138  {
139  pixelLabels = TPixelLabelMatrix::Zero(NROWS, NCOLS);
140  }
141  void setLabel(const int row, const int col, uint8_t label_idx) override
142  {
143  pixelLabels(row, col) |= static_cast<bitmask_t>(1) << label_idx;
144  }
145  void getLabels(const int row, const int col, uint8_t& labels) override
146  {
147  labels = pixelLabels(row, col);
148  }
149 
150  void unsetLabel(const int row, const int col, uint8_t label_idx) override
151  {
152  pixelLabels(row, col) &= ~(static_cast<bitmask_t>(1) << label_idx);
153  }
154  void unsetAll(
155  const int row, const int col,
156  [[maybe_unused]] uint8_t label_idx) override
157  {
158  pixelLabels(row, col) = 0;
159  }
161  const int row, const int col, uint8_t label_idx) const override
162  {
163  return (pixelLabels(row, col) &
164  (static_cast<bitmask_t>(1) << label_idx)) != 0;
165  }
166 
167  // Ctor: pass identification to parent for deserialization
168  TPixelLabelInfo() : TPixelLabelInfoBase(BYTES_REQUIRED_) {}
169 
170  protected:
173  mrpt::serialization::CArchive& out) const override;
174  void Print(std::ostream& out) const override;
175 }; // end TPixelLabelInfo
176 
177 } // namespace mrpt::obs
mrpt::obs::TPixelLabelInfoBase::setLabelName
void setLabelName(unsigned int label_idx, const std::string &name)
Definition: TPixelLabelInfo.h:48
mrpt::obs::TPixelLabelInfoBase::getLabels
virtual void getLabels(const int row, const int col, uint8_t &labels)=0
mrpt::obs::TPixelLabelInfo::TPixelLabelInfo
TPixelLabelInfo()
Definition: TPixelLabelInfo.h:168
mrpt::obs::TPixelLabelInfoBase::Ptr
std::shared_ptr< TPixelLabelInfoBase > Ptr
Used in CObservation3DRangeScan::pixelLabels.
Definition: TPixelLabelInfo.h:33
integer_select.h
mrpt::obs::TPixelLabelInfoBase::operator<<
friend std::ostream & operator<<(std::ostream &out, const TPixelLabelInfoBase &obj)
std stream interface
Definition: TPixelLabelInfo.h:92
mrpt::obs::TPixelLabelInfoBase::setLabel
virtual void setLabel(const int row, const int col, uint8_t label_idx)=0
Mark the pixel(row,col) as classified in the category label_idx, which may be in the range 0 to MAX_N...
mrpt::obs::TPixelLabelInfo::setSize
void setSize(const int NROWS, const int NCOLS) override
Resizes the matrix pixelLabels to the given size, setting all bitfields to zero (that is,...
Definition: TPixelLabelInfo.h:137
mrpt::obs::TPixelLabelInfoBase::unsetLabel
virtual void unsetLabel(const int row, const int col, uint8_t label_idx)=0
For the pixel(row,col), removes its classification into the category label_idx, which may be in the r...
mrpt::obs::TPixelLabelInfo::internal_writeToStream
void internal_writeToStream(mrpt::serialization::CArchive &out) const override
Definition: TPixelLabelInfo.cpp:47
mrpt::obs::TPixelLabelInfo
Pixel-wise semantic label struct.
Definition: TPixelLabelInfo.h:117
mrpt::obs::TPixelLabelInfoBase::internal_writeToStream
virtual void internal_writeToStream(mrpt::serialization::CArchive &out) const =0
mrpt::obs::TPixelLabelInfo::Print
void Print(std::ostream &out) const override
Definition: TPixelLabelInfo.cpp:115
out
mrpt::vision::TStereoCalibResults out
Definition: chessboard_stereo_camera_calib_unittest.cpp:25
mrpt::obs
This namespace contains representation of robot actions and observations.
Definition: CParticleFilter.h:18
mrpt::serialization::CArchive
Virtual base class for "archives": classes abstracting I/O streams.
Definition: CArchive.h:55
mrpt::obs::TPixelLabelInfo::bitmask_t
typename mrpt::uint_select_by_bytecount< BYTES_REQUIRED >::type bitmask_t
Automatically-determined integer type of the proper size such that all labels fit as one bit (max: 64...
Definition: TPixelLabelInfo.h:124
mrpt::obs::TPixelLabelInfoBase::checkLabel
virtual bool checkLabel(const int row, const int col, uint8_t label_idx) const =0
Checks whether pixel(row,col) has been clasified into category label_idx, which may be in the range 0...
mrpt::obs::TPixelLabelInfo::checkLabel
bool checkLabel(const int row, const int col, uint8_t label_idx) const override
Checks whether pixel(row,col) has been clasified into category label_idx, which may be in the range 0...
Definition: TPixelLabelInfo.h:160
mrpt::obs::TPixelLabelInfoBase::unsetAll
virtual void unsetAll(const int row, const int col, uint8_t label_idx)=0
Removes all categories for pixel(row,col)
mrpt::obs::TPixelLabelInfoBase::writeToStream
void writeToStream(mrpt::serialization::CArchive &out) const
Definition: TPixelLabelInfo.cpp:22
mrpt::obs::TPixelLabelInfo::pixelLabels
TPixelLabelMatrix pixelLabels
Definition: TPixelLabelInfo.h:135
mrpt::obs::TPixelLabelInfo::unsetLabel
void unsetLabel(const int row, const int col, uint8_t label_idx) override
For the pixel(row,col), removes its classification into the category label_idx, which may be in the r...
Definition: TPixelLabelInfo.h:150
mrpt::obs::TPixelLabelInfo::unsetAll
void unsetAll(const int row, const int col, [[maybe_unused]] uint8_t label_idx) override
Definition: TPixelLabelInfo.h:154
mrpt::obs::TPixelLabelInfoBase::~TPixelLabelInfoBase
virtual ~TPixelLabelInfoBase()
mrpt::obs::TPixelLabelInfo::BYTES_REQUIRED
constexpr static unsigned int BYTES_REQUIRED
Definition: TPixelLabelInfo.h:119
mrpt::obs::TPixelLabelInfo::internal_readFromStream
void internal_readFromStream(mrpt::serialization::CArchive &in) override
Definition: TPixelLabelInfo.cpp:34
mrpt::obs::TPixelLabelInfoBase::Print
virtual void Print(std::ostream &) const =0
mrpt::obs::TPixelLabelInfoBase::TPixelLabelInfoBase
TPixelLabelInfoBase(unsigned int BITFIELD_BYTES_)
Definition: TPixelLabelInfo.h:26
mrpt::obs::TPixelLabelInfoBase::TMapLabelID2Name
std::map< uint32_t, std::string > TMapLabelID2Name
Definition: TPixelLabelInfo.h:34
mrpt::obs::TPixelLabelInfoBase::setSize
virtual void setSize(const int NROWS, const int NCOLS)=0
Resizes the matrix pixelLabels to the given size, setting all bitfields to zero (that is,...
mrpt::math::MatrixVectorBase< bitmask_t, CMatrixDynamic< bitmask_t > >::Zero
static CMatrixDynamic< bitmask_t > Zero()
Definition: MatrixVectorBase.h:125
mrpt::obs::TPixelLabelInfo::getLabels
void getLabels(const int row, const int col, uint8_t &labels) override
Definition: TPixelLabelInfo.h:145
mrpt::obs::TPixelLabelInfoBase::internal_readFromStream
virtual void internal_readFromStream(mrpt::serialization::CArchive &in)=0
mrpt::obs::TPixelLabelInfoBase::checkLabelNameExistence
int checkLabelNameExistence(const std::string &name) const
Check the existence of a label by returning its associated index.
Definition: TPixelLabelInfo.h:54
mrpt::obs::TPixelLabelInfoBase::getLabelName
const std::string & getLabelName(unsigned int label_idx) const
Definition: TPixelLabelInfo.h:41
mrpt::uint_select_by_bytecount
Usage: uint_select_by_bytecount<N>::type var; allows defining var as a unsigned integer with,...
Definition: integer_select.h:54
mrpt::obs::TPixelLabelInfoBase::readAndBuildFromStream
static TPixelLabelInfoBase * readAndBuildFromStream(mrpt::serialization::CArchive &in)
Definition: TPixelLabelInfo.cpp:61
mrpt::obs::TPixelLabelInfoBase
Virtual interface to all pixel-label semantic information structs.
Definition: TPixelLabelInfo.h:25
CSerializable.h
mrpt::obs::TPixelLabelInfo::setLabel
void setLabel(const int row, const int col, uint8_t label_idx) override
Mark the pixel(row,col) as classified in the category label_idx, which may be in the range 0 to MAX_N...
Definition: TPixelLabelInfo.h:141
mrpt::obs::TPixelLabelInfoBase::BITFIELD_BYTES
const uint8_t BITFIELD_BYTES
Minimum number of bytes required to hold MAX_NUM_DIFFERENT_LABELS bits.
Definition: TPixelLabelInfo.h:101
mrpt::math::CMatrixDynamic< bitmask_t >
mrpt::obs::TPixelLabelInfoBase::pixelLabelNames
TMapLabelID2Name pixelLabelNames
The 'semantic' or human-friendly name of the i'th bit in pixelLabels(r,c) can be found in pixelLabelN...
Definition: TPixelLabelInfo.h:39



Page generated by Doxygen 1.8.18 for MRPT 2.0.4 at Thu Sep 24 07:14:18 UTC 2020