Test SR300 Intel RealSense acquisition.
#include <iostream>
#include <visp3/sensor/vpRealSense.h>
#include <visp3/gui/vpDisplayX.h>
#include <visp3/gui/vpDisplayGDI.h>
#include <visp3/io/vpImageIo.h>
#include <visp3/core/vpImageConvert.h>
#include <visp3/core/vpMutex.h>
#include <visp3/core/vpThread.h>
#if defined(VISP_HAVE_REALSENSE) && defined(VISP_HAVE_CPP11_COMPATIBILITY)
# include <librealsense/rs.h>
#if( ! defined(__APPLE__) && ! defined(__MACH__) ) // Not OSX
# if (defined(VISP_HAVE_PTHREAD) || defined(_WIN32)) // Threading available
# define USE_THREAD
# endif
#endif
#ifdef VISP_HAVE_PCL
# include <pcl/visualization/cloud_viewer.h>
# include <pcl/visualization/pcl_visualizer.h>
#endif
#ifdef VISP_HAVE_PCL
#ifdef USE_THREAD
typedef enum {
capture_waiting,
capture_started,
capture_stopped
} t_CaptureState;
t_CaptureState s_capture_state = capture_waiting;
{
pcl::PointCloud<pcl::PointXYZRGB>::Ptr pointcloud_ = *((pcl::PointCloud<pcl::PointXYZRGB>::Ptr *) args);
pcl::visualization::PCLVisualizer::Ptr viewer (new pcl::visualization::PCLVisualizer ("3D Viewer"));
pcl::visualization::PointCloudColorHandlerRGBField<pcl::PointXYZRGB> rgb(pointcloud_);
viewer->setBackgroundColor (0, 0, 0);
viewer->addCoordinateSystem (0.1);
viewer->initCameraParameters ();
viewer->setPosition(2*640+80, 480+80);
viewer->setCameraPosition(0,0,-0.5, 0,-1,0);
viewer->setSize(640, 480);
t_CaptureState capture_state_;
do {
capture_state_ = s_capture_state;
if (capture_state_ == capture_started) {
static bool update = false;
if (! update) {
viewer->addPointCloud<pcl::PointXYZRGB> (pointcloud_, rgb, "sample cloud");
viewer->setPointCloudRenderingProperties (pcl::visualization::PCL_VISUALIZER_POINT_SIZE, 1, "sample cloud");
update = true;
}
else {
viewer->updatePointCloud<pcl::PointXYZRGB> (pointcloud_, rgb, "sample cloud");
}
viewer->spinOnce (10);
}
} while(capture_state_ != capture_stopped);
std::cout << "End of point cloud display thread" << std::endl;
return 0;
}
#endif
#endif
#endif
int main(
#if defined(VISP_HAVE_PCL)
int argc, char *argv[]
#endif
)
{
#if defined(VISP_HAVE_REALSENSE) && defined(VISP_HAVE_CPP11_COMPATIBILITY) && ( defined(VISP_HAVE_X11) || defined(VISP_HAVE_GDI) )
try {
if ( rs_get_device_name((
const rs_device *) rs.
getHandler(),
nullptr) != std::string(
"Intel RealSense SR300") ) {
std::cout << "This test file is used to test the Intel RealSense SR300 only." << std::endl;
return EXIT_SUCCESS;
}
std::cout << "API version: " << rs_get_api_version(nullptr) << std::endl;
std::cout <<
"Firmware: " << rs_get_device_firmware_version((
const rs_device *) rs.
getHandler(),
nullptr) << std::endl;
std::cout << "RealSense sensor characteristics: \n" << rs << std::endl;
std::vector<double> time_vector;
#if defined(VISP_HAVE_X11)
vpDisplayX dd(I_display_depth, 0, 0,
"Depth image");
#elif defined(VISP_HAVE_GDI)
#endif
while (true) {
rs.
acquire( NULL, (
unsigned char *) depth.bitmap, NULL, NULL );
break;
}
break;
}
}
std::cout <<
"SR300_DEPTH_Z16_640x240_110FPS - Mean time: " <<
vpMath::getMean(time_vector) <<
" ms ; Median time: "
dd.close(I_display_depth);
#if defined(VISP_HAVE_X11)
#elif defined(VISP_HAVE_GDI)
#endif
time_vector.clear();
while (true) {
rs.
acquire( (
unsigned char *) I_color.bitmap, NULL, NULL, NULL );
break;
}
break;
}
}
std::cout <<
"SR300_COLOR_RGBA8_1920x1080_30FPS - Mean time: " <<
vpMath::getMean(time_vector) <<
" ms ; Median time: "
dc.close(I_color);
#if defined(VISP_HAVE_X11)
vpDisplayX di(I_display_infrared, 0, 0,
"Infrared image");
#elif defined(VISP_HAVE_GDI)
vpDisplayGDI di(I_display_infrared, 0, 0,
"Infrared image");
#endif
time_vector.clear();
while (true) {
rs.
acquire( NULL, NULL, NULL, (
unsigned char *) infrared.bitmap );
break;
}
break;
}
}
std::cout <<
"SR300_INFRARED_Y16_640x480_200FPS - Mean time: " <<
vpMath::getMean(time_vector) <<
" ms ; Median time: "
di.close(I_display_infrared);
depth.resize( (
unsigned int) rs.
getIntrinsics(rs::stream::depth).height, (
unsigned int) rs.
getIntrinsics(rs::stream::depth).width );
I_display_depth.resize( depth.getHeight(), depth.getWidth() );
#if defined(VISP_HAVE_X11)
dd.init(I_display_depth, 0, 0, "Depth image");
#elif defined(VISP_HAVE_GDI)
dd.init(I_display_depth, 0, 0, "Depth image");
#endif
time_vector.clear();
while (true) {
rs.
acquire( NULL, (
unsigned char *) depth.bitmap, NULL, NULL );
break;
}
break;
}
}
std::cout <<
"SR300_DEPTH_Z16_640x480_60FPS - Mean time: " <<
vpMath::getMean(time_vector) <<
" ms ; Median time: "
dd.close(I_display_depth);
#if defined(VISP_HAVE_X11)
dd.init(I_display_depth, 0, 0, "Depth image");
di.init(I_display_infrared, (int) I_display_depth.getWidth(), 0, "Infrared image");
#elif defined(VISP_HAVE_GDI)
dd.init(I_display_depth, 0, 0, "Depth image");
di.init(I_display_infrared, I_display_depth.getWidth(), 0, "Infrared image");
#endif
time_vector.clear();
while (true) {
rs.
acquire( NULL, (
unsigned char *) depth.bitmap, NULL, (
unsigned char *) I_display_infrared.bitmap );
break;
}
break;
}
}
std::cout <<
"SR300_DEPTH_Z16_640x480_60FPS + SR300_INFRARED_Y8_640x480_60FPS - Mean time: " <<
vpMath::getMean(time_vector)
dd.close(I_display_depth);
di.close(I_display_infrared);
#ifdef VISP_HAVE_PCL
pcl::PointCloud<pcl::PointXYZRGB>::Ptr pointcloud(new pcl::PointCloud<pcl::PointXYZRGB>);
#ifdef USE_THREAD
#else
pcl::visualization::PCLVisualizer::Ptr viewer (new pcl::visualization::PCLVisualizer ("3D Viewer"));
pcl::visualization::PointCloudColorHandlerRGBField<pcl::PointXYZRGB> rgb(pointcloud);
viewer->setBackgroundColor (0, 0, 0);
viewer->addCoordinateSystem (1.0);
viewer->initCameraParameters ();
viewer->setPosition(2*640+80, 480+80);
viewer->setCameraPosition(0,0,-0.5, 0,-1,0);
viewer->setSize(640, 480);
#endif
I_color.resize(480, 640);
#if defined(VISP_HAVE_X11)
dc.init(I_color, 0, 0, "Color image");
dd.init(I_display_depth, 0, (int) I_color.getHeight()+80, "Depth image");
di.init(I_display_infrared, (int) I_display_depth.getWidth(), 0, "Infrared image");
#elif defined(VISP_HAVE_GDI)
dc.init(I_color, 0, 0, "Color image");
dd.init(I_display_depth, 0, (int) I_color.getHeight()+80, "Depth image");
di.init(I_display_infrared, (int) I_display_depth.getWidth(), 0, "Infrared image");
#endif
rs::stream color_stream = argc > 1 ? (rs::stream) atoi(argv[1]) : rs::stream::color;
std::cout << "color_stream: " << color_stream << std::endl;
rs::stream depth_stream = argc > 2 ? (rs::stream) atoi(argv[2]) : rs::stream::depth;
std::cout << "depth_stream: " << depth_stream << std::endl;
time_vector.clear();
while (true) {
rs.
acquire( (
unsigned char *) I_color.bitmap, (
unsigned char *) depth.bitmap, NULL, pointcloud, (
unsigned char *) I_display_infrared.bitmap,
NULL, color_stream, depth_stream);
#ifdef VISP_HAVE_PCL
#ifdef USE_THREAD
{
s_capture_state = capture_started;
}
#else
static bool update = false;
if (! update) {
viewer->addPointCloud<pcl::PointXYZRGB> (pointcloud, rgb, "sample cloud");
viewer->setPointCloudRenderingProperties (pcl::visualization::PCL_VISUALIZER_POINT_SIZE, 1, "sample cloud");
viewer->setPosition( (int) I_color.getWidth()+80, (int) I_color.getHeight()+80) ;
update = true;
}
else {
viewer->updatePointCloud<pcl::PointXYZRGB> (pointcloud, rgb, "sample cloud");
}
viewer->spinOnce (10);
#endif
#endif
break;
}
break;
}
}
std::cout <<
"SR300_COLOR_RGBA8_640x480_60FPS + SR300_DEPTH_Z16_640x480_60FPS + SR300_INFRARED_Y8_640x480_60FPS - Mean time: " <<
vpMath::getMean(time_vector)
#ifdef VISP_HAVE_PCL
#ifdef USE_THREAD
{
s_capture_state = capture_stopped;
}
#endif
#endif
dc.close(I_color);
dd.close(I_display_depth);
di.close(I_display_infrared);
#endif
dc.init(I_color, 0, 0, "Color aligned to depth");
di.init(I_display_depth, (int) I_color.getWidth(), 0, "Depth image");
while (true) {
rs.
acquire( (
unsigned char *) I_color.bitmap, (
unsigned char *) depth.bitmap, NULL, NULL, NULL, rs::stream::color_aligned_to_depth );
break;
}
break;
}
}
dc.close(I_color);
dd.close(I_display_depth);
dc.
init(I_color, 0, 0,
"Color image");
di.init(I_display_depth, (int) I_color.getWidth(), 0, "Depth aligned to color");
while (true) {
rs.
acquire( (
unsigned char *) I_color.bitmap, (
unsigned char *) depth.bitmap, NULL, NULL, NULL, rs::stream::color, rs::stream::depth_aligned_to_color );
break;
}
break;
}
}
dc.close(I_color);
dd.close(I_display_depth);
#if VISP_HAVE_OPENCV_VERSION >= 0x020409
cv::Mat color_mat(480, 640, CV_8UC3);
cv::Mat infrared_mat(480, 640, CV_8U);
while (true) {
rs.
acquire( color_mat.data, NULL, NULL, infrared_mat.data );
cv::imshow("Color mat", color_mat);
cv::imshow("Infrared mat", infrared_mat);
char c = cv::waitKey(10);
if (c == 27) {
break;
}
break;
}
}
#endif
std::cerr <<
"RealSense error " << e.
what() << std::endl;
} catch(const rs::error & e) {
std::cerr << "RealSense error calling " << e.get_failed_function() << "(" << e.get_failed_args() << "): " << e.what() << std::endl;
} catch(const std::exception & e) {
std::cerr << e.what() << std::endl;
}
#elif !defined(VISP_HAVE_REALSENSE)
std::cout << "Install RealSense SDK to make this test working. X11 or GDI are needed also." << std::endl;
#elif !defined(VISP_HAVE_CPP11_COMPATIBILITY)
std::cout << "Build ViSP with c++11 compiler flag (cmake -DUSE_CPP11=ON) to make this test working" << std::endl;
#endif
return EXIT_SUCCESS;
}