19 using namespace openni;
21 #ifndef RETURN_FALSE_STATUS_NOT_OK
22 #define RETURN_FALSE_STATUS_NOT_OK(s) if(s != STATUS_OK) { yCError(DEPTHCAMERA) << OpenNI::getExtendedError(); return false; }
27 constexpr
char accuracy [] =
"accuracy";
28 constexpr
char clipPlanes [] =
"clipPlanes";
29 constexpr
char depth_Fov [] =
"depth_Fov";
30 constexpr
char depthRes [] =
"depthResolution";
31 constexpr
char rgb_Fov [] =
"rgb_Fov";
32 constexpr
char rgbRes [] =
"rgbResolution";
33 constexpr
char rgbMirroring [] =
"rgbMirroring";
34 constexpr
char depthMirroring [] =
"depthMirroring";
37 static std::map<std::string, RGBDSensorParamParser::RGBDParam>
params_map =
57 openni::PixelFormat pixF{openni::PixelFormat::PIXEL_FORMAT_DEPTH_1_MM};
65 bool isValid(){
return frameRef.isValid() & isReady;}
69 std::lock_guard<std::mutex> guard(mutex);
70 return inputImage.
copy(image);
75 std::lock_guard<std::mutex> guard(mutex);
80 void onNewFrame(openni::VideoStream& stream)
override;
81 openni::VideoFrameRef frameRef;
89 void streamFrameListener::onNewFrame(openni::VideoStream& stream)
91 std::lock_guard<std::mutex> guard(mutex);
92 stream.readFrame(&frameRef);
94 if (!frameRef.isValid() || !frameRef.getData())
96 yCInfo(DEPTHCAMERA) <<
"Frame lost";
102 pixF = stream.getVideoMode().getPixelFormat();
104 w = frameRef.getWidth();
105 h = frameRef.getHeight();
106 dataSize = frameRef.getDataSize();
108 if (isReady ==
false)
115 yCError(DEPTHCAMERA) <<
"Pixel Format not recognized";
119 image.setPixelCode(pixC);
122 if (image.getRawImageSize() != (
size_t) frameRef.getDataSize())
124 yCError(DEPTHCAMERA) <<
"Device and local copy data size doesn't match";
128 memcpy((
void*)image.getRawImage(), (
void*)frameRef.getData(), frameRef.getDataSize());
137 m_depthRegistration =
true;
159 delete m_paramParser;
163 bool depthCameraDriver::initializeOpeNIDevice()
167 rc = OpenNI::initialize();
170 yCError(DEPTHCAMERA) <<
"Initialize failed," << OpenNI::getExtendedError();
174 rc = m_device.open(ANY_DEVICE);
177 yCError(DEPTHCAMERA) <<
"Couldn't open device," << OpenNI::getExtendedError();
181 if (m_device.getSensorInfo(SENSOR_COLOR) !=
nullptr)
183 rc = m_imageStream.create(m_device, SENSOR_COLOR);
186 yCError(DEPTHCAMERA) <<
"Couldn't create color stream," << OpenNI::getExtendedError();
191 rc = m_imageStream.start();
194 yCError(DEPTHCAMERA) <<
"Couldn't start the color stream," << OpenNI::getExtendedError();
198 if (m_device.getSensorInfo(SENSOR_DEPTH) !=
nullptr)
200 rc = m_depthStream.create(m_device, SENSOR_DEPTH);
203 yCError(DEPTHCAMERA) <<
"Couldn't create depth stream," << OpenNI::getExtendedError();
208 if (m_depthRegistration)
210 if (m_device.isImageRegistrationModeSupported(IMAGE_REGISTRATION_DEPTH_TO_COLOR))
212 if (m_device.setImageRegistrationMode(IMAGE_REGISTRATION_DEPTH_TO_COLOR) == STATUS_OK)
214 yCInfo(DEPTHCAMERA) <<
"Depth successfully registered on rgb sensor";
218 yCWarning(DEPTHCAMERA) <<
"Depth registration failed.. sending unregistered images";
223 yCWarning(DEPTHCAMERA) <<
"Depth image registration not supported by this device";
227 rc = m_depthStream.start();
230 yCError(DEPTHCAMERA) <<
"Couldn't start the depth stream," << OpenNI::getExtendedError();
234 m_imageStream.addNewFrameListener(m_imageFrame);
235 m_depthStream.addNewFrameListener(m_depthFrame);
241 void depthCameraDriver::settingErrorMsg(
const std::string& error,
bool&
ret)
247 bool depthCameraDriver::setParams()
257 if (!
params_map[accuracy].val[0].isFloat64()) {
258 settingErrorMsg(
"Param " +
params_map[accuracy].name +
" is not a double as it should be.",
ret);
262 settingErrorMsg(
"Setting param " +
params_map[accuracy].name +
" failed... quitting.",
ret);
269 if (!
params_map[clipPlanes].val[0].isFloat64()) {
270 settingErrorMsg(
"Param " +
params_map[clipPlanes].name +
" is not a double as it should be.",
ret);
273 if (!
params_map[clipPlanes].val[1].isFloat64()) {
274 settingErrorMsg(
"Param " +
params_map[clipPlanes].name +
" is not a double as it should be.",
ret);
278 settingErrorMsg(
"Setting param " +
params_map[clipPlanes].name +
" failed... quitting.",
ret);
290 settingErrorMsg(
"Param " +
params_map[depth_Fov].name +
" is not a double as it should be.",
ret);
294 settingErrorMsg(
"Setting param " +
params_map[depth_Fov].name +
" failed... quitting.",
ret);
309 settingErrorMsg(
"Param " +
params_map[rgb_Fov].name +
" is not a double as it should be.",
ret);
314 settingErrorMsg(
"Setting param " +
params_map[rgb_Fov].name +
" failed... quitting.",
ret);
327 settingErrorMsg(
"Param " +
params_map[depthRes].name +
" is not a int as it should be.",
ret);
332 settingErrorMsg(
"Setting param " +
params_map[depthRes].name +
" failed... quitting.",
ret);
345 settingErrorMsg(
"Param " +
params_map[rgbRes].name +
" is not a int as it should be.",
ret);
350 settingErrorMsg(
"Setting param " +
params_map[rgbRes].name +
" failed... quitting.",
ret);
365 settingErrorMsg(
"Param " +
params_map[rgbMirroring].name +
" is not a bool as it should be.",
ret);
369 for (
int t = 0;
t < 5;
t++)
371 yCInfo(DEPTHCAMERA) <<
"Trying to set rgb mirroring parameter for the" <<
t+1 <<
"time/s";
375 yCInfo(DEPTHCAMERA) <<
"Rgb mirroring parameter set successfully";
382 settingErrorMsg(
"Setting param " +
params_map[rgbMirroring].name +
" failed... quitting.",
ret);
396 settingErrorMsg(
"Param " +
params_map[depthMirroring].name +
" is not a bool as it should be.",
ret);
399 for (
int t = 0;
t < 5;
t++)
401 yCInfo(DEPTHCAMERA) <<
"Trying to set depth mirroring parameter for the" <<
t+1 <<
"time/s";
405 yCInfo(DEPTHCAMERA) <<
"Depth mirroring parameter set successfully";
412 settingErrorMsg(
"Setting param " +
params_map[depthMirroring].name +
" failed... quitting.",
ret);
421 std::vector<RGBDSensorParamParser::RGBDParam*> params;
424 params.push_back(&(p.second));
427 if (!m_paramParser->
parseParam(config, params))
429 yCError(DEPTHCAMERA) <<
"Failed to parse the parameters";
434 m_depthRegistration = !(config.
check(
"registered") && config.
find(
"registered").
isBool() && config.
find(
"registered").
asBool() ==
false);
436 if (!initializeOpeNIDevice())
452 m_imageStream.stop();
453 m_imageStream.destroy();
454 m_depthStream.stop();
455 m_depthStream.destroy();
465 return params_map[rgbRes].val.at(1).asInt32();
468 return m_imageStream.getVideoMode().getResolutionY();
475 return params_map[rgbRes].val.at(0).asInt32();
478 return m_imageStream.getVideoMode().getResolutionX();
483 yCWarning(DEPTHCAMERA) <<
"getRgbSupportedConfigurations not implemented yet";
491 return params_map[rgbRes].val.at(0).asInt32();
494 width = m_imageStream.getVideoMode().getResolutionX();
495 height = m_imageStream.getVideoMode().getResolutionY();
504 yCError(DEPTHCAMERA) <<
"Cannot set. Depth resolution is a description!";
508 return setResolution(width, height, m_depthStream);
511 bool depthCameraDriver::setResolution(
int width,
int height, VideoStream& stream)
516 vm = stream.getVideoMode();
517 vm.setResolution(width, height);
519 bRet = (stream.setVideoMode(vm) == STATUS_OK);
524 yCError(DEPTHCAMERA) << OpenNI::getExtendedError();
537 return setResolution(width, height, m_imageStream);
540 bool depthCameraDriver::setFOV(
double horizontalFov,
double verticalFov, VideoStream& stream)
553 return setFOV(horizontalFov, verticalFov, m_depthStream);
562 return setFOV(horizontalFov, verticalFov, m_depthStream);
572 a1 = fabs(_accuracy - 0.001) < 0.00001;
573 a2 = fabs(_accuracy - 0.0001) < 0.00001;
576 yCError(DEPTHCAMERA) <<
"Supporting accuracy of 1mm (0.001) or 100um (0.0001) only at the moment";
584 vm = m_imageStream.getVideoMode();
585 pf = fabs(_accuracy - 0.001) < 0.00001 ? PIXEL_FORMAT_DEPTH_1_MM : PIXEL_FORMAT_DEPTH_100_UM;
587 vm.setPixelFormat(pf);
588 m_depthStream.stop();
589 ret = m_depthStream.setVideoMode(vm) == STATUS_OK;
594 yCError(DEPTHCAMERA) << OpenNI::getExtendedError();
603 horizontalFov =
params_map[rgb_Fov].val[0].asFloat64();
604 verticalFov =
params_map[rgb_Fov].val[1].asFloat64();
607 horizontalFov = m_imageStream.getHorizontalFieldOfView() *
RAD2DEG;
608 verticalFov = m_imageStream.getVerticalFieldOfView() *
RAD2DEG;
616 mirror =
params_map[rgbMirroring].val[0].asBool();
620 mirror = m_imageStream.getMirroringEnabled();
631 if (m_imageStream.setMirroringEnabled(mirror) != STATUS_OK)
638 return (
ret == mirror);
649 return setIntrinsic(intrinsic, m_paramParser->
rgbIntrinsic);
656 return params_map[depthRes].val[1].asFloat64();
658 return m_depthStream.getVideoMode().getResolutionY();
665 return params_map[depthRes].val[0].asFloat64();
667 return m_depthStream.getVideoMode().getResolutionX();
674 horizontalFov =
params_map[depth_Fov].val[0].asFloat64();
675 verticalFov =
params_map[depth_Fov].val[1].asFloat64();
678 horizontalFov = m_depthStream.getHorizontalFieldOfView() *
RAD2DEG;
679 verticalFov = m_depthStream.getVerticalFieldOfView() *
RAD2DEG;
685 return setIntrinsic(intrinsic, m_paramParser->
rgbIntrinsic);
692 return params_map[accuracy].val[0].asFloat64();
694 return m_depthStream.getVideoMode().getPixelFormat() == PIXEL_FORMAT_DEPTH_1_MM ? 0.001 : 0.0001;
701 nearPlane =
params_map[clipPlanes].val[0].asFloat64();
702 farPlane =
params_map[clipPlanes].val[1].asFloat64();
707 nearPlane = m_depthStream.getMinPixelValue() * factor;
708 farPlane = m_depthStream.getMaxPixelValue() * factor;
729 return params_map[depthMirroring].val[0].asBool();
731 mirror = m_depthStream.getMirroringEnabled();
745 return (
ret == mirror);
756 return getImage(rgbImage, timeStamp, m_imageFrame);
761 return getImage(
depthImage, timeStamp, m_depthFrame);
788 case (PIXEL_FORMAT_RGB888):
791 case (PIXEL_FORMAT_DEPTH_1_MM):
794 case (PIXEL_FORMAT_DEPTH_100_UM):
797 case (PIXEL_FORMAT_SHIFT_9_2):
800 case (PIXEL_FORMAT_SHIFT_9_3):
803 case (PIXEL_FORMAT_GRAY8):
806 case (PIXEL_FORMAT_GRAY16):
824 std::lock_guard<std::mutex> guard(sourceFrame->
mutex);
827 yCError(DEPTHCAMERA) <<
"Device not ready";
834 if (sourceFrame->
dataSize !=
size_t(h * w *
sizeof(
short)) ||
835 (sourceFrame->
pixF != PIXEL_FORMAT_DEPTH_100_UM && sourceFrame->
pixF != PIXEL_FORMAT_DEPTH_1_MM))
837 yCError(DEPTHCAMERA) <<
"getImage: image format error";
846 factor = sourceFrame->
pixF == PIXEL_FORMAT_DEPTH_1_MM ? 0.001 : 0.0001;
849 rawImage =
reinterpret_cast<float*
> (Frame.
getRawImage());
852 for(i = 0; i < w * h; i++)
854 rawImage[i] = srcRawImage[i] * factor;
862 return getImage(colorFrame, colorStamp, m_imageFrame) & getImage(depthFrame, depthStamp, m_depthFrame);
867 openni::DeviceState status;
868 status = DEVICE_STATE_NOT_READY;
869 if (m_device.isValid() &&
870 m_imageStream.isValid() &&
871 m_depthStream.isValid() &&
875 status = DEVICE_STATE_OK;
879 case DEVICE_STATE_OK:
882 case DEVICE_STATE_NOT_READY:
894 return OpenNI::getExtendedError();
913 if (std::find(m_supportedFeatures.begin(), m_supportedFeatures.end(), f) != m_supportedFeatures.end())
930 yCError(DEPTHCAMERA) <<
"Feature not supported!";
947 vm = m_imageStream.getVideoMode();
948 vm.setFps(
int(value));
951 m_depthStream.getVideoMode();
952 vm.setFps(
int(value));
957 yCError(DEPTHCAMERA) <<
"No manual mode for white_balance. call hasManual() to know if a specific feature support Manual mode instead of wasting my time";
960 yCError(DEPTHCAMERA) <<
"Feature not supported!";
971 yCError(DEPTHCAMERA) <<
"Feature not supported!";
979 *value = m_imageStream.getCameraSettings()->getExposure();
982 *value = m_imageStream.getCameraSettings()->getGain();
985 *value = (m_imageStream.getVideoMode().getFps());
988 yCError(DEPTHCAMERA) <<
"No manual mode for white_balance. call hasManual() to know if a specific feature support Manual mode";
998 yCError(DEPTHCAMERA) <<
"No 2-valued feature are supported";
1004 yCError(DEPTHCAMERA) <<
"No 2-valued feature are supported";
1013 yCError(DEPTHCAMERA) <<
"Feature not supported!";
1032 yCError(DEPTHCAMERA) <<
"Feature not supported!";
1038 yCError(DEPTHCAMERA) <<
"Feature does not have OnOff.. call hasOnOff() to know if a specific feature support OnOff mode";
1064 yCError(DEPTHCAMERA) <<
"Feature not supported!";
1070 yCError(DEPTHCAMERA) <<
"Feature does not have OnOff.. call hasOnOff() to know if a specific feature support OnOff mode";
1074 *isActive = m_imageStream.getCameraSettings()->getAutoWhiteBalanceEnabled();
1083 yCError(DEPTHCAMERA) <<
"Feature not supported!";
1102 yCError(DEPTHCAMERA) <<
"Feature not supported!";
1121 yCError(DEPTHCAMERA) <<
"Feature not supported!";
1133 yCError(DEPTHCAMERA) <<
"Feature not supported!";
1156 yCError(DEPTHCAMERA) <<
"Feature does not have both auto and manual mode";
1165 yCError(DEPTHCAMERA) <<
"Feature not supported!";
1176 yCError(DEPTHCAMERA) <<
"Feature does not have both auto and manual mode";
1185 yCError(DEPTHCAMERA) <<
"Feature not supported!";
1191 yCError(DEPTHCAMERA) <<
"Feature doesn't have OnePush";
@ YARP_FEATURE_FRAME_RATE
@ YARP_FEATURE_WHITE_BALANCE
bool setActive(int feature, bool onoff) override
Set the requested feature on or off.
bool getRgbResolution(int &width, int &height) override
Get the resolution of the rgb image from the camera.
bool open(yarp::os::Searchable &config) override
Open the DeviceDriver.
bool getCameraDescription(CameraDescriptor *camera) override
Get a basic description of the camera hw.
double getDepthAccuracy() override
Get the minimum detectable variation in distance [meter].
bool setRgbResolution(int width, int height) override
Set the resolution of the rgb image from the camera.
bool getDepthFOV(double &horizontalFov, double &verticalFov) override
Get the field of view (FOV) of the depth camera.
bool setRgbFOV(double horizontalFov, double verticalFov) override
Set the field of view (FOV) of the rgb camera.
bool getRgbIntrinsicParam(Property &intrinsic) override
Get the intrinsic parameters of the rgb camera.
bool hasManual(int feature, bool *hasManual) override
Check if the requested feature has the 'manual' mode.
bool setDepthFOV(double horizontalFov, double verticalFov) override
Set the field of view (FOV) of the depth camera.
int getDepthHeight() override
Return the height of each frame.
bool getDepthClipPlanes(double &nearPlane, double &farPlane) override
Get the clipping planes of the sensor.
int getRgbHeight() override
Return the height of each frame.
RGBDSensor_status getSensorStatus() override
Get the surrent status of the sensor, using enum type.
bool setDepthResolution(int width, int height) override
Set the resolution of the depth image from the camera.
bool close() override
Close the DeviceDriver.
bool getRgbFOV(double &horizontalFov, double &verticalFov) override
Get the field of view (FOV) of the rgb camera.
bool getRgbImage(FlexImage &rgbImage, Stamp *timeStamp=NULL) override
Get the rgb frame from the device.
std::string getLastErrorMsg(Stamp *timeStamp=NULL) override
Return an error message in case of error.
bool hasFeature(int feature, bool *hasFeature) override
Check if camera has the requested feature (saturation, brightness ...
bool hasOnOff(int feature, bool *HasOnOff) override
Check if the camera has the ability to turn on/off the requested feature.
bool getActive(int feature, bool *isActive) override
Get the current status of the feature, on or off.
bool setMode(int feature, FeatureMode mode) override
Set the requested mode for the feature.
int getRgbWidth() override
Return the width of each frame.
bool getDepthMirroring(bool &mirror) override
Get the mirroring setting of the sensor.
bool getExtrinsicParam(yarp::sig::Matrix &extrinsic) override
Get the extrinsic parameters from the device.
bool setDepthClipPlanes(double nearPlane, double farPlane) override
Set the clipping planes of the sensor.
int getDepthWidth() override
Return the height of each frame.
bool setRgbMirroring(bool mirror) override
Set the mirroring setting of the sensor.
bool getDepthImage(depthImage &depthImage, Stamp *timeStamp=NULL) override
bool getRgbSupportedConfigurations(yarp::sig::VectorOf< yarp::dev::CameraConfig > &configurations) override
Get the possible configurations of the camera.
bool getFeature(int feature, double *value) override
Get the current value for the requested feature.
bool setFeature(int feature, double value) override
Set the requested feature to a value (saturation, brightness ...
bool getImages(FlexImage &colorFrame, depthImage &depthFrame, Stamp *colorStamp=NULL, Stamp *depthStamp=NULL) override
bool setDepthMirroring(bool mirror) override
Set the mirroring setting of the sensor.
bool hasAuto(int feature, bool *hasAuto) override
Check if the requested feature has the 'auto' mode.
bool hasOnePush(int feature, bool *hasOnePush) override
Check if the requested feature has the 'onePush' mode.
bool getRgbMirroring(bool &mirror) override
Get the mirroring setting of the sensor.
static int pixFormatToCode(openni::PixelFormat p)
bool setOnePush(int feature) override
Set the requested feature to a value (saturation, brightness ...
bool getMode(int feature, FeatureMode *mode) override
Get the current mode for the feature.
bool getDepthIntrinsicParam(Property &intrinsic) override
Get the intrinsic parameters of the depth camera.
bool setDepthAccuracy(double accuracy) override
Set the minimum detectable variation in distance [meter] when possible.
bool getImage(FlexImage &inputImage)
yarp::sig::FlexImage image
yarp::os::Stamp getStamp()
@ RGBD_SENSOR_GENERIC_ERROR
The RGBDSensorParamParser class.
yarp::sig::Matrix transformationMatrix
bool parseParam(const yarp::os::Searchable &config, std::vector< RGBDParam * > ¶ms)
parseParam, parse the params stored in a Searchable.
yarp::sig::IntrinsicParams rgbIntrinsic
A class for storing options and configuration information.
A base class for nested structures that can be searched.
virtual Value & find(const std::string &key) const =0
Gets a value corresponding to a given keyword.
virtual bool check(const std::string &key) const =0
Check if there exists a property of the given name.
An abstraction for a time stamp and/or sequence number.
static void delaySystem(double seconds)
A single value (typically within a Bottle).
virtual yarp::conf::float64_t asFloat64() const
Get 64-bit floating point value.
virtual bool isBool() const
Checks if value is a boolean.
virtual bool asBool() const
Get boolean value.
virtual std::int32_t asInt32() const
Get 32-bit integer value.
virtual bool isFloat64() const
Checks if value is a 64-bit floating point number.
virtual bool isInt32() const
Checks if value is a 32-bit integer.
Image class with user control of representation details.
unsigned char * getRawImage() const
Access to the internal image buffer.
bool copy(const Image &alt)
Copy operator.
void resize(size_t imgWidth, size_t imgHeight)
Reallocate an image to be of a desired size, throwing away its current contents.
#define RETURN_FALSE_STATUS_NOT_OK(s)
static std::map< std::string, RGBDSensorParamParser::RGBDParam > params_map
#define yCInfo(component,...)
#define yCError(component,...)
#define yCWarning(component,...)
#define YARP_LOG_COMPONENT(name,...)
For streams capable of holding different kinds of content, check what they actually have.
An interface to the operating system, including Port based communication.
std::string deviceDescription
The IntrinsicParams struct to handle the intrinsic parameter of cameras(RGB and RGBD either).
void toProperty(yarp::os::Property &intrinsic) const
toProperty, convert the struct to a Property.