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  }
30  virtual ~TPixelLabelInfoBase();
31 
32  /** Used in CObservation3DRangeScan::pixelLabels */
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:
105  virtual void internal_writeToStream(
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 {
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
Virtual interface to all pixel-label semantic information structs.
void writeToStream(mrpt::serialization::CArchive &out) const
void setSize(const int NROWS, const int NCOLS) override
Resizes the matrix pixelLabels to the given size, setting all bitfields to zero (that is...
TPixelLabelInfoBase(unsigned int BITFIELD_BYTES_)
std::map< uint32_t, std::string > TMapLabelID2Name
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...
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...
static constexpr unsigned int BYTES_REQUIRED
virtual void Print(std::ostream &) const =0
const std::string & getLabelName(unsigned int label_idx) const
Usage: uint_select_by_bytecount<N>::type var; allows defining var as a unsigned integer with...
void internal_writeToStream(mrpt::serialization::CArchive &out) const override
virtual void internal_readFromStream(mrpt::serialization::CArchive &in)=0
int checkLabelNameExistence(const std::string &name) const
Check the existence of a label by returning its associated index.
void Print(std::ostream &out) const override
TPixelLabelMatrix pixelLabels
static TPixelLabelInfoBase * readAndBuildFromStream(mrpt::serialization::CArchive &in)
const uint8_t BITFIELD_BYTES
Minimum number of bytes required to hold MAX_NUM_DIFFERENT_LABELS bits.
This namespace contains representation of robot actions and observations.
TMapLabelID2Name pixelLabelNames
The &#39;semantic&#39; or human-friendly name of the i&#39;th bit in pixelLabels(r,c) can be found in pixelLabelN...
friend std::ostream & operator<<(std::ostream &out, const TPixelLabelInfoBase &obj)
std stream interface
void setLabelName(unsigned int label_idx, const std::string &name)
virtual void getLabels(const int row, const int col, uint8_t &labels)=0
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...
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...
void unsetAll(const int row, const int col, [[maybe_unused]] uint8_t label_idx) override
void internal_readFromStream(mrpt::serialization::CArchive &in) override
Virtual base class for "archives": classes abstracting I/O streams.
Definition: CArchive.h:54
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...
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...
virtual void internal_writeToStream(mrpt::serialization::CArchive &out) const =0
mrpt::vision::TStereoCalibResults out
Pixel-wise semantic label struct.
void getLabels(const int row, const int col, uint8_t &labels) override
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...
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...
virtual void unsetAll(const int row, const int col, uint8_t label_idx)=0
Removes all categories for pixel(row,col)



Page generated by Doxygen 1.8.14 for MRPT 2.0.4 Git: 33de1d0ad Sat Jun 20 11:02:42 2020 +0200 at sáb jun 20 17:35:17 CEST 2020