MRPT  2.0.4
COpenNI2Generic_CDevice.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 
10 #pragma once
11 
12 #include <mrpt/config.h> //MRPT_HAS_OPENNI2
13 
14 /** @file Include this file only from your user code if you have OPENNI2 */
15 
16 namespace mrpt::hwdrivers
17 {
19 {
20  public:
22  enum
23  {
27  STREAM_TYPE_SIZE // this last value is to know the number of possible
28  // channels, leave it always at the end!
29  };
30 #if MRPT_HAS_OPENNI2
31  private:
32  class CStream
33  {
34  public:
36 
37  private:
38  std::ostream& m_log;
39  openni::Device& m_device;
40  std::string m_strName;
41  openni::SensorType m_type;
42  openni::VideoStream m_stream;
43  openni::PixelFormat m_format;
44  bool m_verbose;
45 
46  public:
47  CStream(
48  openni::Device& device, openni::SensorType type,
49  openni::PixelFormat format, std::ostream& log, bool verbose);
50  virtual ~CStream();
51  const std::string& getName() const { return m_strName; }
52  bool isValid() const;
53  bool isMirrorSupported() const;
54  bool setMirror(bool flag);
55  void setCloseRange(int& value);
56  virtual bool open(int w, int h, int fps);
57  virtual bool start();
58  virtual void destroy();
59  virtual bool getFrame(
60  openni::VideoFrameRef& frame, mrpt::system::TTimeStamp& timestamp,
61  bool& there_is_obs, bool& hardware_error);
62 
63  int getFrameWidth() const
64  {
65  return m_stream.getVideoMode().getResolutionX();
66  }
67  int getFrameHeight() const
68  {
69  return m_stream.getVideoMode().getResolutionY();
70  }
71  double getHFov() const { return m_stream.getHorizontalFieldOfView(); }
72  double getFx() const
73  {
74  return getFrameWidth() / (2.0 * tan(getHFov() / 2.0));
75  }
76  double getVFov() const { return m_stream.getVerticalFieldOfView(); }
77  double getFy() const
78  {
79  return getFrameHeight() / (2.0 * tan(getVFov() / 2.0));
80  }
81  double getCx() const { return (getFrameWidth() - 1) * 0.5; }
82  double getCy() const { return (getFrameHeight() - 1) * 0.5; }
83  void disableAutoExposure()
84  {
85  m_stream.getCameraSettings()->setAutoExposureEnabled(false);
86  }
87  void enableAutoExposure()
88  {
89  m_stream.getCameraSettings()->setAutoExposureEnabled(true);
90  }
91 
92  void getCameraParam(mrpt::img::TCamera& param) const
93  {
94  param.ncols = getFrameWidth();
95  param.nrows = getFrameHeight();
96  param.fx(getFx());
97  param.fy(getFy());
98  param.cx(getCx());
99  param.cy(getCy());
100  }
101 
102  static Ptr create(
103  openni::Device& device, openni::SensorType type,
104  openni::PixelFormat format, std::ostream& log, bool verbose);
105  };
106  openni::DeviceInfo m_info;
107  openni::Device m_device;
108  CStream::Ptr m_streams[STREAM_TYPE_SIZE];
109  bool m_mirror;
110  std::stringstream m_log;
111  bool m_verbose;
112 
113  bool synchMirrorMode();
114  bool startStreams();
115 
116  inline void resize(mrpt::img::CImage& rgb, int w, int h)
117  {
118  rgb.resize(w, h, mrpt::img::CH_RGB);
119  }
120  inline void resize(mrpt::math::CMatrix_u16& depth, int w, int h)
121  {
122  depth.resize(h, w);
123  }
124  inline void resize(mrpt::obs::CObservation3DRangeScan& obs, int w, int h)
125  {
126  resize(obs.intensityImage, w, h);
127  obs.rangeImage_setSize(h, w);
128  }
129 
130  inline void setPixel(
131  const openni::RGB888Pixel& src, mrpt::img::CImage& rgb, int x, int y)
132  {
133  rgb.setPixel(x, y, (src.r << 16) + (src.g << 8) + src.b);
134  }
135  inline void setPixel(
136  const openni::DepthPixel& src, mrpt::math::CMatrix_u16& depth_mm, int x,
137  int y)
138  {
139  depth_mm(y, x) = src;
140  }
141 
142  template <class NI_PIXEL, class MRPT_DATA>
143  void copyRow(const char* src, MRPT_DATA& rgb, int w, const int y)
144  {
145  const auto* s = (const NI_PIXEL*)src;
146  for (int xc = 0; xc < w; ++xc, ++s)
147  {
148  int x = xc;
149  if (isMirrorMode())
150  {
151  x = w - xc - 1;
152  }
153  setPixel(*s, rgb, x, y);
154  }
155  }
156 
157  template <class NI_PIXEL, class MRPT_DATA>
158  void copyFrame(openni::VideoFrameRef& frame, MRPT_DATA& dst)
159  {
160  const char* data = (const char*)frame.getData();
161  const int stride = frame.getStrideInBytes();
162  const int width = frame.getWidth();
163  const int height = frame.getHeight();
164  resize(dst, width, height);
165  for (int y = 0; y < height; ++y, data += stride)
166  {
167  copyRow<NI_PIXEL, MRPT_DATA>(data, dst, width, y);
168  }
169  }
170 
171  public:
172  CDevice(
173  const openni::DeviceInfo& info, openni::PixelFormat rgb,
174  openni::PixelFormat depth, bool m_verbose);
175  virtual ~CDevice();
176 
177  const openni::DeviceInfo& getInfo() const { return m_info; }
178  std::string getLog() const { return m_log.str(); }
179  void clearLog()
180  {
181  m_log.str("");
182  m_log.clear();
183  }
184  bool isMirrorMode() const { return m_mirror; }
185  void setMirrorMode(bool mode) { m_mirror = mode; }
186  bool hasColor() const
187  {
188  if (!m_streams[COLOR_STREAM])
189  return false;
190  else
191  return m_streams[COLOR_STREAM]->isValid();
192  }
193  bool hasDepth() const
194  {
195  if (!m_streams[DEPTH_STREAM])
196  return false;
197  else
198  return m_streams[DEPTH_STREAM]->isValid();
199  }
200 
201  bool isOpen() const;
202  void close();
203  bool open(int w, int h, int fps);
204 
205  bool getNextFrameRGB(
207  bool& there_is_obs, bool& hardware_error);
208  bool getNextFrameD(
210  bool& there_is_obs, bool& hardware_error);
211  bool getNextFrameRGBD(
212  mrpt::obs::CObservation3DRangeScan& obs, bool& there_is_obs,
213  bool& hardware_error);
214 
215  bool getCameraParam(int streamType, mrpt::img::TCamera& param) const
216  {
217  if (streamType < 0 || streamType >= STREAM_TYPE_SIZE)
218  {
219  return false;
220  }
221  if (!m_streams[streamType] || m_streams[streamType]->isValid() == false)
222  {
223  return false;
224  }
225  m_streams[streamType]->getCameraParam(param);
226  return true;
227  }
228 
229  bool getSerialNumber(unsigned int& sn);
230 
231  static Ptr create(
232  const openni::DeviceInfo& info, openni::PixelFormat rgb,
233  openni::PixelFormat depth, bool verbose);
234 
235  openni::Device& getDevicePtr() { return m_device; }
236 
237  private:
238  bool getSerialNumber(std::string& sn);
239 
240 #endif // MRPT_HAS_OPENNI2
241 };
242 } // namespace mrpt::hwdrivers
uint32_t nrows
Definition: TCamera.h:40
void resize(size_t row, size_t col)
double fx() const
Get the value of the focal length x-value (in pixels).
Definition: TCamera.h:175
std::string std::string format(std::string_view fmt, ARGS &&... args)
Definition: format.h:26
void getNextFrameRGB(mrpt::img::CImage &rgb_img, mrpt::system::TTimeStamp &timestamp, bool &there_is_obs, bool &hardware_error, unsigned sensor_id=0)
The main data retrieving function, to be called after calling loadConfig() and initialize().
double fy() const
Get the value of the focal length y-value (in pixels).
Definition: TCamera.h:177
A range or depth 3D scan measurement, as from a time-of-flight range camera or a structured-light dep...
void getNextFrameRGBD(mrpt::obs::CObservation3DRangeScan &out_obs, bool &there_is_obs, bool &hardware_error, unsigned sensor_id=0)
The main data retrieving function, to be called after calling loadConfig() and initialize().
Contains classes for various device interfaces.
mrpt::io::CStream CStream
Definition: utils/CStream.h:5
mrpt::Clock::time_point TTimeStamp
A system independent time type, it holds the the number of 100-nanosecond intervals since January 1...
Definition: datetime.h:40
bool start()
Open all sensor streams (normally called automatically at constructor, no need to call it manually)...
double cy() const
Get the value of the principal point y-coordinate (in pixels).
Definition: TCamera.h:173
void getNextFrameD(mrpt::math::CMatrix_u16 &depth_img_mm, mrpt::system::TTimeStamp &timestamp, bool &there_is_obs, bool &hardware_error, unsigned sensor_id=0)
The main data retrieving function, to be called after calling loadConfig() and initialize().
void resize(std::size_t width, std::size_t height, TImageChannels nChannels, PixelDepth depth=PixelDepth::D8U)
Changes the size of the image, erasing previous contents (does NOT scale its current content...
Definition: CImage.cpp:250
bool isOpen(const unsigned sensor_id) const
Whether there is a working connection to the sensor.
mrpt::img::CImage intensityImage
If hasIntensityImage=true, a color or gray-level intensity image of the same size than "rangeImage"...
void close(unsigned sensor_id=0)
Close the connection to the sensor (no need to call it manually unless desired for some reason...
Parameters for the Brown-Conrady camera lens distortion model.
Definition: TCamera.h:26
double cx() const
Get the value of the principal point x-coordinate (in pixels).
Definition: TCamera.h:171
void setPixel(int x, int y, size_t color) override
Changes the value of the pixel (x,y).
Definition: CImage.cpp:1082
void rangeImage_setSize(const int HEIGHT, const int WIDTH)
Similar to calling "rangeImage.setSize(H,W)" but this method provides memory pooling to speed-up the ...
void open(unsigned sensor_id=0)
Try to open the camera (all the parameters [resolution,fps,...] must be set before calling this) - us...
images resize(NUM_IMGS)
uint32_t ncols
Camera resolution.
Definition: TCamera.h:40
A class for storing images as grayscale or RGB bitmaps.
Definition: img/CImage.h:148
static struct FontData data
Definition: gltext.cpp:144



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