36 #include <Image/ImageProcessor.h>
37 #include <librealsense2/h/rs_sensor.h>
38 #include <librealsense2/rs.hpp>
39 #include <librealsense2/rs_advanced_mode.hpp>
40 #include <librealsense2/rsutil.h>
46 IntelRealSenseProvider::onInitImageProvider()
48 Eigen::Vector2f fov = getProperty<Eigen::Vector2f>(
"FieldOfView");
49 depthUtils.setFieldOfView(fov(0), fov(1));
51 Eigen::Vector2i dimensions = getProperty<Eigen::Vector2i>(
"Resolution");
52 setImageFormat(visionx::ImageDimension(dimensions(0), dimensions(1)),
54 visionx::eBayerPatternGr);
56 pointcloud.reset(
new pcl::PointCloud<CloudPointType>());
58 alignFilter.reset(
new rs2::align(RS2_STREAM_COLOR));
62 IntelRealSenseProvider::onInitCapturingPointCloudProvider()
67 IntelRealSenseProvider::onExitCapturingPointCloudProvider()
72 IntelRealSenseProvider::onStartCapture(
float framesPerSecond)
79 Eigen::Vector2i dimensions = getProperty<Eigen::Vector2i>(
"Resolution").getValue();
82 RS2_STREAM_COLOR, dimensions(0), dimensions(1), RS2_FORMAT_RGB8, framesPerSecond);
84 RS2_STREAM_DEPTH, dimensions(0), dimensions(1), RS2_FORMAT_Z16, framesPerSecond);
85 rs2::pipeline_profile profile = pipe.start(cfg);
87 rs2::device dev = profile.get_device();
89 rs2::depth_sensor ds = dev.query_sensors().front().as<rs2::depth_sensor>();
91 if (getProperty<std::string>(
"ConfigFile").isSet())
95 auto devices = ctx.query_devices();
96 size_t device_count = devices.size();
99 throw armarx::LocalException(
"No device detected. Is it plugged in?\n");
103 auto dev = devices[0];
106 if (dev.is<rs400::advanced_mode>())
109 auto advanced_mode_dev = dev.as<rs400::advanced_mode>();
112 std::string path = getProperty<std::string>(
"ConfigFile");
116 ARMARX_INFO <<
"Loading realsense config from " << path;
117 std::ifstream t(path);
118 std::string
str((std::istreambuf_iterator<char>(t)), std::istreambuf_iterator<char>());
119 advanced_mode_dev.load_json(
str);
123 throw armarx::LocalException() <<
"Current device doesn't support advanced-mode!\n";
128 auto preset = getProperty<rs2_rs400_visual_preset>(
"Preset").getValue();
129 ARMARX_INFO <<
"Setting preset to: " << rs2_rs400_visual_preset_to_string(preset);
130 ds.set_option(RS2_OPTION_VISUAL_PRESET, preset);
133 depthScale = ds.get_depth_scale();
136 cloudFormat = getPointCloudFormat();
137 calibration = createStereoCalibration(profile);
142 std::to_string(calibration.calibrationRight.cameraParam.translation[1]) +
", " +
143 std::to_string(calibration.calibrationRight.cameraParam.translation[2])));
147 IntelRealSenseProvider::onStopCapture()
153 IntelRealSenseProvider::doCapture()
159 data = pipe.wait_for_frames(timeout);
168 auto rstimestamp =
data.get_timestamp();
170 if (
data.get_frame_timestamp_domain() == RS2_TIMESTAMP_DOMAIN_GLOBAL_TIME)
172 timestamp = IceUtil::Time::milliSeconds(rstimestamp);
178 <<
"Timestamp of realsense is not global - measuring time myself";
182 << (IceUtil::Time::now() - IceUtil::Time::milliSeconds(rstimestamp)).toMilliSeconds()
186 if (getProperty<bool>(
"ApplyAlignmentFilter").getValue())
193 auto depthFrame =
data.get_depth_frame();
195 const bool applyDisparityFilter = getProperty<bool>(
"ApplyDisparityFilter").getValue();
196 if (applyDisparityFilter)
199 depthFrame = depth_to_disparity_filter.process(depthFrame);
204 if (getProperty<bool>(
"ApplySpatialFilter").getValue())
207 depthFrame = spat_filter.process(depthFrame);
210 if (getProperty<bool>(
"ApplyTemporalFilter").getValue())
213 depthFrame = temporal_filter.process(depthFrame);
217 if (applyDisparityFilter)
220 depthFrame = disparity_to_depth_filter.process(depthFrame);
224 rs2::frame color =
data.get_color_frame();
229 color =
data.get_infrared_frame();
233 const int cw = color.as<rs2::video_frame>().get_width();
234 const int ch = color.as<rs2::video_frame>().get_height();
235 auto colorBuffer =
reinterpret_cast<const unsigned char*
>(color.get_data());
236 CByteImage rgbImage(cw, ch, CByteImage::eRGB24,
true);
237 rgbImage.pixels =
const_cast<unsigned char*
>(colorBuffer);
242 auto resultDepthBuffer = depthImage->pixels;
243 auto depthBuffer =
reinterpret_cast<const unsigned short*
>(depthFrame.get_data());
244 const int dw = depthFrame.as<rs2::video_frame>().get_width();
245 const int dh = depthFrame.as<rs2::video_frame>().get_height();
247 for (
int y = 0; y < dh; ++y)
249 for (
int x = 0;
x < dw; ++
x)
251 int depthValue = 1000.f * depthBuffer[index2] * depthScale;
253 resultDepthBuffer[
index],
254 resultDepthBuffer[
index + 1],
255 resultDepthBuffer[
index + 2]);
261 CByteImage* images[2] = {&rgbImage, depthImage.get()};
270 points =
pc.calculate(depthFrame);
276 pointcloud->width = dw;
277 pointcloud->height = dh;
278 pointcloud->is_dense =
false;
279 pointcloud->points.resize(points.size());
280 auto ptr = points.get_vertices();
281 auto texPtr = points.get_texture_coordinates();
284 float maxDepth = getProperty<float>(
"MaxDepth").getValue() / 1000.f;
287 for (
auto& p : pointcloud->points)
289 p.x = ptr->x * 1000.f;
290 p.y = ptr->y * 1000.f;
291 p.z = ptr->z <= maxDepth ? ptr->z * 1000.f : std::nan(
"");
294 int x = texPtr->u * cw;
295 int y = texPtr->v * ch;
296 if (
x >= 0 && x < cw && y >= 0 && y < ch)
298 auto colorIndex = int((
x + y * cw) * 3);
299 p.r = colorBuffer[colorIndex];
300 p.g = colorBuffer[colorIndex + 1];
301 p.b = colorBuffer[colorIndex + 2];
305 p.r = p.g = p.b = 255;
314 pointcloud->header.stamp =
timestamp.toMicroSeconds();
315 providePointCloud(pointcloud);
320 IntelRealSenseProvider::getDefaultName()
const
322 return "IntelRealSenseProvider";
326 IntelRealSenseProvider::createPropertyDefinitions()
333 IntelRealSenseProvider::onInitComponent()
335 ImageProvider::onInitComponent();
336 CapturingPointCloudProvider::onInitComponent();
340 IntelRealSenseProvider::onConnectComponent()
343 ImageProvider::onConnectComponent();
344 CapturingPointCloudProvider::onConnectComponent();
348 IntelRealSenseProvider::onDisconnectComponent()
350 captureEnabled =
false;
352 ImageProvider::onDisconnectComponent();
353 CapturingPointCloudProvider::onDisconnectComponent();
357 IntelRealSenseProvider::onExitComponent()
359 ImageProvider::onExitComponent();
360 CapturingPointCloudProvider::onExitComponent();
363 visionx::StereoCalibration
364 IntelRealSenseProvider::getStereoCalibration(
const Ice::Current&)
370 IntelRealSenseProvider::getImagesAreUndistorted(
const Ice::Current&
c)
376 IntelRealSenseProvider::getReferenceFrame(
const Ice::Current&
c)
378 return getProperty<std::string>(
"frameName");
382 IntelRealSenseProvider::createStereoCalibration(
const rs2::pipeline_profile& selection)
const
387 [](visionx::CameraParameters& params, rs2::video_stream_profile& streamProfile)
390 auto i = streamProfile.get_intrinsics();
396 params.focalLength.resize(2, 0.0f);
397 params.principalPoint.resize(2, 0.0f);
398 params.distortion.resize(4, 0.0f);
404 params.principalPoint[0] = i.ppx;
405 params.principalPoint[1] = i.ppy;
406 params.focalLength[0] = i.fx;
407 params.focalLength[1] = i.fy;
408 params.width = streamProfile.width();
409 params.height = streamProfile.height();
411 rs2::video_stream_profile rgb_stream =
412 selection.get_stream(RS2_STREAM_COLOR).as<rs2::video_stream_profile>();
413 rs2::video_stream_profile depth_stream =
414 selection.get_stream(RS2_STREAM_DEPTH).as<rs2::video_stream_profile>();
416 visionx::CameraParameters rgbParams;
417 visionx::CameraParameters depthParams;
419 getIntrinsics(rgbParams, rgb_stream);
420 getIntrinsics(depthParams, depth_stream);
422 StereoCalibration calibration;
424 auto depth_stream = selection.get_stream(RS2_STREAM_DEPTH);
425 auto color_stream = selection.get_stream(RS2_STREAM_COLOR);
426 rs2_extrinsics e = depth_stream.get_extrinsics_to(color_stream);
428 float origin[3]{0.f, 0.f, 0.f};
430 rs2_transform_point_to_point(
target, &e, origin);
435 calibration.calibrationLeft.cameraParam = rgbParams;
436 calibration.calibrationRight.cameraParam = depthParams;
437 calibration.rectificationHomographyLeft =
439 calibration.rectificationHomographyRight =
441 calibration.calibrationRight.cameraParam.translation = {
target[0],
target[1],
target[2]};