camera_utilities.h
Go to the documentation of this file.
1 
34 #ifndef MULTISENSE_ROS_CAMERA_UTILITIES_H
35 #define MULTISENSE_ROS_CAMERA_UTILITIES_H
36 
37 #include <tuple>
38 #include <mutex>
39 
40 #include <Eigen/Core>
41 #include <Eigen/Geometry>
42 
43 #include <opencv2/opencv.hpp>
44 
45 #include <ros/ros.h>
46 #include <sensor_msgs/CameraInfo.h>
47 
48 #include <multisense_lib/MultiSenseChannel.hh>
49 #include <multisense_lib/MultiSenseTypes.hh>
50 
51 namespace multisense_ros {
52 
53 static constexpr size_t S30_AUX_CAM_WIDTH = 1920;
54 static constexpr size_t S30_AUX_CAM_HEIGHT = 1188;
55 
57 {
58  size_t width = 0;
59  size_t height = 0;
60 };
61 
63 
65 {
66  cv::Mat map1;
67  cv::Mat map2;
68 };
69 
70 template <typename T>
72 {
73 public:
75  const T &data):
76  driver_(driver),
77  callback_buffer_(driver->reserveCallbackBuffer()),
78  data_(data)
79  {
80  }
81 
83  {
85  }
86 
87  const T &data() const noexcept
88  {
89  return data_;
90  }
91 
92 private:
93 
94  BufferWrapper(const BufferWrapper&) = delete;
95  BufferWrapper operator=(const BufferWrapper&) = delete;
96 
99  const T data_;
100 
101 };
102 
103 template <typename T>
104 constexpr Eigen::Matrix<T, 3, 1> ycbcrToBgr(const crl::multisense::image::Header &luma,
105  const crl::multisense::image::Header &chroma,
106  size_t u,
107  size_t v)
108 {
109  const uint8_t *lumaP = reinterpret_cast<const uint8_t*>(luma.imageDataP);
110  const uint8_t *chromaP = reinterpret_cast<const uint8_t*>(chroma.imageDataP);
111 
112  const size_t luma_offset = (v * luma.width) + u;
113  const size_t chroma_offset = 2 * (((v/2) * (luma.width/2)) + (u/2));
114 
115  const float px_y = static_cast<float>(lumaP[luma_offset]);
116  const float px_cb = static_cast<float>(chromaP[chroma_offset+0]) - 128.0f;
117  const float px_cr = static_cast<float>(chromaP[chroma_offset+1]) - 128.0f;
118 
119  float px_r = px_y + 1.402f * px_cr;
120  float px_g = px_y - 0.34414f * px_cb - 0.71414f * px_cr;
121  float px_b = px_y + 1.772f * px_cb;
122 
123  if (px_r < 0.0f) px_r = 0.0f;
124  else if (px_r > 255.0f) px_r = 255.0f;
125  if (px_g < 0.0f) px_g = 0.0f;
126  else if (px_g > 255.0f) px_g = 255.0f;
127  if (px_b < 0.0f) px_b = 0.0f;
128  else if (px_b > 255.0f) px_b = 255.0f;
129 
130  return Eigen::Matrix<T, 3, 1>{static_cast<T>(px_b), static_cast<T>(px_g), static_cast<T>(px_r)};
131 }
132 
134  const crl::multisense::image::Header &chroma,
135  uint8_t *output);
136 
137 Eigen::Matrix4d makeQ(const crl::multisense::image::Config& config,
138  const crl::multisense::image::Calibration& calibration,
139  const crl::multisense::system::DeviceInfo& device_info);
140 
141 sensor_msgs::CameraInfo makeCameraInfo(const crl::multisense::image::Config& config,
143  const crl::multisense::system::DeviceInfo& device_info,
144  bool scale_calibration);
145 
146 RectificationRemapT makeRectificationRemap(const crl::multisense::image::Config& config,
148  const crl::multisense::system::DeviceInfo& device_info);
149 
151 {
152 public:
153  EIGEN_MAKE_ALIGNED_OPERATOR_NEW
154 
156  const crl::multisense::image::Calibration& calibration,
157  const crl::multisense::system::DeviceInfo& device_info);
158 
160 
162 
167  Eigen::Matrix4d Q() const;
168 
173  double T() const;
174 
179  Eigen::Vector3d aux_T() const;
180 
184  Eigen::Vector3f reproject(size_t u, size_t v, double d) const;
185 
189  static Eigen::Vector3f reproject(size_t u,
190  size_t v,
191  double d,
192  const sensor_msgs::CameraInfo &left_camera_info,
193  const sensor_msgs::CameraInfo &right_camera_info);
194 
198  static bool isValidReprojectedPoint(const Eigen::Vector3f& pt, const float squared_max_range);
199 
204  Eigen::Vector2f rectifiedAuxProject(const Eigen::Vector3f &left_rectified_point) const;
205 
210  static Eigen::Vector2f rectifiedAuxProject(const Eigen::Vector3f &left_rectified_point,
211  const sensor_msgs::CameraInfo &aux_camera_info);
212 
218 
224 
228  bool validRight() const;
229 
233  bool validAux() const;
234 
235  sensor_msgs::CameraInfo leftCameraInfo(const std::string& frame_id, const ros::Time& stamp) const;
236  sensor_msgs::CameraInfo rightCameraInfo(const std::string& frame_id, const ros::Time& stamp) const;
237  sensor_msgs::CameraInfo auxCameraInfo(const std::string& frame_id,
238  const ros::Time& stamp,
239  const OperatingResolutionT& resolution) const;
240  sensor_msgs::CameraInfo auxCameraInfo(const std::string& frame_id,
241  const ros::Time& stamp,
242  size_t width,
243  size_t height) const;
244 
245  std::shared_ptr<RectificationRemapT> leftRemap() const;
246  std::shared_ptr<RectificationRemapT> rightRemap() const;
247 
248 private:
249 
253 
254  //
255  // Protect our cache during calibration updates
256 
257  mutable std::mutex mutex_;
258 
259  //
260  // Cache for quick queries
261 
262  Eigen::Matrix4d q_matrix_;
263 
264  sensor_msgs::CameraInfo left_camera_info_;
265  sensor_msgs::CameraInfo right_camera_info_;
266  sensor_msgs::CameraInfo aux_camera_info_;
267 
268  std::shared_ptr<RectificationRemapT> left_remap_;
269  std::shared_ptr<RectificationRemapT> right_remap_;
270 };
271 
275 bool clipPoint(const BorderClip& borderClipType,
276  double borderClipValue,
277  size_t height,
278  size_t width,
279  size_t u,
280  size_t v);
281 
284 cv::Vec3b interpolateColor(const Eigen::Vector2f &pixel, const cv::Mat &image);
285 
286 }// namespace
287 
288 #endif
multisense_ros::OperatingResolutionT
Definition: camera_utilities.h:56
multisense_ros::StereoCalibrationManager::aux_camera_info_
sensor_msgs::CameraInfo aux_camera_info_
Definition: camera_utilities.h:266
multisense_ros::StereoCalibrationManager::q_matrix_
Eigen::Matrix4d q_matrix_
Definition: camera_utilities.h:262
multisense_ros::interpolateColor
cv::Vec3b interpolateColor(const Eigen::Vector2f &pixel, const cv::Mat &image)
Given a floating point pixel location, determine its average color using all neighboring pixels.
Definition: camera_utilities.cpp:540
multisense_ros::StereoCalibrationManager::reproject
Eigen::Vector3f reproject(size_t u, size_t v, double d) const
Reproject disparity values into 3D.
Definition: camera_utilities.cpp:341
multisense_ros::BorderClip::RECTANGULAR
@ RECTANGULAR
multisense_ros::StereoCalibrationManager
Definition: camera_utilities.h:150
multisense_ros::StereoCalibrationManager::config_
crl::multisense::image::Config config_
Definition: camera_utilities.h:250
crl::multisense::image::Calibration::Data
multisense_ros::BufferWrapper::BufferWrapper
BufferWrapper(crl::multisense::Channel *driver, const T &data)
Definition: camera_utilities.h:74
multisense_ros::StereoCalibrationManager::config
crl::multisense::image::Config config() const
Definition: camera_utilities.cpp:293
multisense_ros::S30_AUX_CAM_WIDTH
static constexpr size_t S30_AUX_CAM_WIDTH
Definition: camera_utilities.h:53
ros.h
multisense_ros::RectificationRemapT::map1
cv::Mat map1
Definition: camera_utilities.h:66
crl::multisense::Channel::releaseCallbackBuffer
virtual Status releaseCallbackBuffer(void *referenceP)=0
crl::multisense::image::Header::width
uint32_t width
multisense_ros::StereoCalibrationManager::calibration_
const crl::multisense::image::Calibration calibration_
Definition: camera_utilities.h:251
multisense_ros::StereoCalibrationManager::validAux
bool validAux() const
Determine if the Aux calibration is valid.
Definition: camera_utilities.cpp:437
multisense_ros::RectificationRemapT
Definition: camera_utilities.h:64
multisense_ros::StereoCalibrationManager::T
double T() const
Translation which transforms points from the rectified left camera frame into the recified right came...
Definition: camera_utilities.cpp:307
multisense_ros::BufferWrapper::data
const T & data() const noexcept
Definition: camera_utilities.h:87
f
f
multisense_ros::BufferWrapper::~BufferWrapper
~BufferWrapper()
Definition: camera_utilities.h:82
multisense_ros::BorderClip::CIRCULAR
@ CIRCULAR
multisense_ros::StereoCalibrationManager::right_camera_info_
sensor_msgs::CameraInfo right_camera_info_
Definition: camera_utilities.h:265
multisense_ros::clipPoint
bool clipPoint(const BorderClip &borderClipType, double borderClipValue, size_t height, size_t width, size_t u, size_t v)
Determine if a pixel should be clipped given the provided parameters.
Definition: camera_utilities.cpp:503
multisense_ros::StereoCalibrationManager::device_info_
const crl::multisense::system::DeviceInfo & device_info_
Definition: camera_utilities.h:252
crl::multisense::image::Config
multisense_ros::StereoCalibrationManager::validRight
bool validRight() const
Determine if the Right calibration is valid.
Definition: camera_utilities.cpp:432
multisense_ros
Definition: camera.h:58
crl::multisense::system::DeviceInfo
multisense_ros::StereoCalibrationManager::Q
Eigen::Matrix4d Q() const
Q stereo reprojection matrix see: https://docs.opencv.org/4.3.0/d9/d0c/group__calib3d....
Definition: camera_utilities.cpp:300
multisense_ros::BufferWrapper::driver_
crl::multisense::Channel * driver_
Definition: camera_utilities.h:97
multisense_ros::StereoCalibrationManager::rightCameraInfo
sensor_msgs::CameraInfo rightCameraInfo(const std::string &frame_id, const ros::Time &stamp) const
Definition: camera_utilities.cpp:455
crl::multisense::image::Calibration
multisense_ros::BufferWrapper::callback_buffer_
void * callback_buffer_
Definition: camera_utilities.h:98
d
d
multisense_ros::S30_AUX_CAM_HEIGHT
static constexpr size_t S30_AUX_CAM_HEIGHT
Definition: camera_utilities.h:54
multisense_ros::BorderClip::NONE
@ NONE
multisense_ros::StereoCalibrationManager::StereoCalibrationManager
EIGEN_MAKE_ALIGNED_OPERATOR_NEW StereoCalibrationManager(const crl::multisense::image::Config &config, const crl::multisense::image::Calibration &calibration, const crl::multisense::system::DeviceInfo &device_info)
Definition: camera_utilities.cpp:243
multisense_ros::BorderClip
BorderClip
Definition: camera_utilities.h:62
multisense_ros::StereoCalibrationManager::leftRemap
std::shared_ptr< RectificationRemapT > leftRemap() const
Definition: camera_utilities.cpp:489
multisense_ros::OperatingResolutionT::width
size_t width
Definition: camera_utilities.h:58
multisense_ros::RectificationRemapT::map2
cv::Mat map2
Definition: camera_utilities.h:67
multisense_ros::StereoCalibrationManager::operatingStereoResolution
OperatingResolutionT operatingStereoResolution() const
Get the current main stereo pair operating resolution. This resolution applies for both the mono and ...
Definition: camera_utilities.cpp:410
multisense_ros::ycbcrToBgr
constexpr Eigen::Matrix< T, 3, 1 > ycbcrToBgr(const crl::multisense::image::Header &luma, const crl::multisense::image::Header &chroma, size_t u, size_t v)
Definition: camera_utilities.h:104
multisense_ros::StereoCalibrationManager::left_camera_info_
sensor_msgs::CameraInfo left_camera_info_
Definition: camera_utilities.h:264
multisense_ros::StereoCalibrationManager::updateConfig
void updateConfig(const crl::multisense::image::Config &config)
Definition: camera_utilities.cpp:259
multisense_ros::StereoCalibrationManager::rightRemap
std::shared_ptr< RectificationRemapT > rightRemap() const
Definition: camera_utilities.cpp:496
multisense_ros::StereoCalibrationManager::operatingAuxResolution
OperatingResolutionT operatingAuxResolution() const
Get the current aux camera operating resolution. This resolution applies for just the mono topics....
Definition: camera_utilities.cpp:417
multisense_ros::StereoCalibrationManager::auxCameraInfo
sensor_msgs::CameraInfo auxCameraInfo(const std::string &frame_id, const ros::Time &stamp, const OperatingResolutionT &resolution) const
Definition: camera_utilities.cpp:466
multisense_ros::StereoCalibrationManager::leftCameraInfo
sensor_msgs::CameraInfo leftCameraInfo(const std::string &frame_id, const ros::Time &stamp) const
Definition: camera_utilities.cpp:443
multisense_ros::BufferWrapper::operator=
BufferWrapper operator=(const BufferWrapper &)=delete
multisense_ros::OperatingResolutionT::height
size_t height
Definition: camera_utilities.h:59
crl::multisense::Channel
ros::Time
multisense_ros::makeCameraInfo
sensor_msgs::CameraInfo makeCameraInfo(const crl::multisense::image::Config &config, const crl::multisense::image::Calibration::Data &calibration, const crl::multisense::system::DeviceInfo &device_info, bool scale_calibration)
multisense_ros::BufferWrapper::data_
const T data_
Definition: camera_utilities.h:99
multisense_ros::makeRectificationRemap
RectificationRemapT makeRectificationRemap(const crl::multisense::image::Config &config, const crl::multisense::image::Calibration::Data &calibration, const crl::multisense::system::DeviceInfo &device_info)
Definition: camera_utilities.cpp:189
multisense_ros::StereoCalibrationManager::left_remap_
std::shared_ptr< RectificationRemapT > left_remap_
Definition: camera_utilities.h:268
multisense_ros::StereoCalibrationManager::right_remap_
std::shared_ptr< RectificationRemapT > right_remap_
Definition: camera_utilities.h:269
multisense_ros::StereoCalibrationManager::aux_T
Eigen::Vector3d aux_T() const
Translation which transforms points from the rectified left camera frame into the rectified aux camer...
Definition: camera_utilities.cpp:323
multisense_ros::StereoCalibrationManager::rectifiedAuxProject
Eigen::Vector2f rectifiedAuxProject(const Eigen::Vector3f &left_rectified_point) const
Project points corresponding to disparity measurements in the left rectified image frame into the aux...
Definition: camera_utilities.cpp:382
crl::multisense::image::Header::imageDataP
const void * imageDataP
multisense_ros::StereoCalibrationManager::mutex_
std::mutex mutex_
Definition: camera_utilities.h:257
multisense_ros::makeQ
Eigen::Matrix4d makeQ(const crl::multisense::image::Config &config, const crl::multisense::image::Calibration &calibration, const crl::multisense::system::DeviceInfo &device_info)
Definition: camera_utilities.cpp:84
multisense_ros::StereoCalibrationManager::isValidReprojectedPoint
static bool isValidReprojectedPoint(const Eigen::Vector3f &pt, const float squared_max_range)
Check if a reprojected point falls within a valid range.
Definition: camera_utilities.cpp:377
crl::multisense::image::Header
multisense_ros::BufferWrapper
Definition: camera_utilities.h:71


multisense_ros
Author(s):
autogenerated on Thu Feb 20 2025 03:51:03