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>
49 depthUtils.setFieldOfView(fov(0), fov(1));
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));
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>();
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>();
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";
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();
141 new Variant(std::to_string(calibration.calibrationRight.cameraParam.translation[0]) +
", " +
142 std::to_string(calibration.calibrationRight.cameraParam.translation[1]) +
", " +
143 std::to_string(calibration.calibrationRight.cameraParam.translation[2])));
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()
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);
207 depthFrame = spat_filter.process(depthFrame);
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();
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();
322 return "IntelRealSenseProvider";
363visionx::StereoCalibration
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]};
SpamFilterDataPtr deactivateSpam(SpamFilterDataPtr const &spamFilter, float deactivationDurationSec, const std::string &identifier, bool deactivate)
static bool getAbsolutePath(const std::string &relativeFilename, std::string &storeAbsoluteFilename, const std::vector< std::string > &additionalSearchPaths={}, bool verbose=true)
std::string getConfigIdentifier()
Retrieve config identifier for this component as set in constructor.
Property< PropertyType > getProperty(const std::string &name)
virtual void onExitComponent()
Hook for subclass.
virtual void onDisconnectComponent()
Hook for subclass.
virtual void onConnectComponent()=0
Pure virtual hook for the subclass.
virtual void onInitComponent()=0
Pure virtual hook for the subclass.
void setMetaInfo(const std::string &id, const VariantBasePtr &value)
Allows to set meta information that can be queried live via Ice interface on the ArmarXManager.
static IceUtil::Time GetTime(TimeMode timeMode=TimeMode::VirtualTime)
Get the current time.
The Variant class is described here: Variants.
std::atomic_bool captureEnabled
Indicates that capturing is enabled and running.
void onInitComponent() override
void onDisconnectComponent() override
Hook for subclass.
ImageFormatInfo getImageFormat(const Ice::Current &c=Ice::emptyCurrent) override
Returns the entire image format info struct via Ice.
void setImageFormat(ImageDimension imageDimension, ImageType imageType, BayerPatternType bayerPatternType=visionx::eBayerPatternRg)
Sets the image basic format data.
void provideImages(void **inputBuffers, const IceUtil::Time &imageTimestamp=IceUtil::Time())
send images raw.
void onConnectComponent() override
void setNumberImages(int numberImages)
Sets the number of images on each capture.
void onExitComponent() override
std::string getReferenceFrame(const Ice::Current &c=Ice::emptyCurrent) override
void onInitComponent() override
Pure virtual hook for the subclass.
visionx::StereoCalibration createStereoCalibration(const rs2::pipeline_profile &selection) const
bool doCapture() override
Main capturing function.
void onExitCapturingPointCloudProvider() override
This is called when the Component::onExitComponent() setup is called.
void onDisconnectComponent() override
Hook for subclass.
void onStopCapture()
This is called when the point cloud provider capturing has been stopped.
visionx::StereoCalibration getStereoCalibration(const Ice::Current &c=Ice::emptyCurrent) override
virtual armarx::PropertyDefinitionsPtr createPropertyDefinitions() override
void onStartCapture(float framesPerSecond)
This is called when the point cloud provider capturing has been started.
void onInitCapturingPointCloudProvider() override
This is called when the Component::onInitComponent() is called.
void onConnectComponent() override
Pure virtual hook for the subclass.
void onInitImageProvider() override
This is called when the Component::onInitComponent() is called.
void onExitComponent()
Hook for subclass.
virtual std::string getDefaultName() const override
bool getImagesAreUndistorted(const ::Ice::Current &c=Ice::emptyCurrent) override
MetaPointCloudFormatPtr getPointCloudFormat(const Ice::Current &c=Ice::emptyCurrent) override
Returns the point cloud format info struct via Ice.
void providePointCloud(PointCloudPtrT pointCloudPtr)
offer the new point cloud.
#define ARMARX_CHECK_EXPRESSION(expression)
This macro evaluates the expression and if it turns out to be false it will throw an ExpressionExcept...
#define ARMARX_INFO
The normal logging level.
#define ARMARX_DEBUG
The logging level for output that is only interesting while debugging.
#define ARMARX_WARNING
The logging level for unexpected behaviour, but not a serious problem.
#define ARMARX_VERBOSE
The logging level for verbose information.
This file offers overloads of toIce() and fromIce() functions for STL container types.
IceUtil::Handle< class PropertyDefinitionContainer > PropertyDefinitionsPtr
PropertyDefinitions smart pointer type.