3 #include <visp3/core/vpConfig.h> 5 #if defined (VISP_HAVE_CPP11_COMPATIBILITY) && (defined (VISP_HAVE_X11) || defined (VISP_HAVE_GDI)) 10 #include <condition_variable> 13 #include <pcl/common/common.h> 14 #include <pcl/io/pcd_io.h> 15 #include <pcl/visualization/cloud_viewer.h> 18 #if defined (VISP_HAVE_PCL) && defined (VISP_HAVE_CPP11_COMPATIBILITY) 19 #define USE_PCL_VIEWER 22 #include <visp3/core/vpImageConvert.h> 23 #include <visp3/core/vpIoTools.h> 24 #include <visp3/gui/vpDisplayX.h> 25 #include <visp3/gui/vpDisplayGDI.h> 26 #include <visp3/io/vpParseArgv.h> 27 #include <visp3/io/vpImageIo.h> 28 #include <visp3/io/vpVideoWriter.h> 30 #define GETOPTARGS "ci:bodh" 34 void usage(
const char *name,
const char *badparam) {
36 Read RealSense data.\n\ 47 Pointcloud is in binary format.\n\ 50 Save color and depth side by side to image sequence.\n\ 53 Display depth in color.\n\ 60 fprintf(stdout,
"\nERROR: Bad parameter [%s]\n", badparam);
63 bool getOptions(
int argc,
char **argv,
64 std::string &input_directory,
66 bool &pointcloud_binary_format,
71 const char **argv1=(
const char**)argv;
76 case 'i': input_directory = optarg;
break;
77 case 'c': click =
true;
break;
78 case 'b': pointcloud_binary_format =
true;
break;
79 case 'o': save_video =
true;
break;
80 case 'd': color_depth =
true;
break;
82 case 'h': usage(argv[0], NULL);
return false;
break;
85 usage(argv[0], optarg);
90 if ((c == 1) || (c == -1)) {
93 std::cerr <<
"ERROR: " << std::endl;
94 std::cerr <<
" Bad argument " << optarg << std::endl << std::endl;
101 #ifdef USE_PCL_VIEWER 102 pcl::PointCloud<pcl::PointXYZ>::Ptr pointcloud(
new pcl::PointCloud<pcl::PointXYZ>());
103 bool cancelled =
false, update_pointcloud =
false;
107 explicit ViewerWorker(std::mutex &mutex) :
111 pcl::PointCloud<pcl::PointXYZ>::Ptr local_pointcloud(
new pcl::PointCloud<pcl::PointXYZ>());
113 bool local_update =
false, local_cancelled =
false;
114 pcl::visualization::PCLVisualizer::Ptr viewer (
new pcl::visualization::PCLVisualizer (
"3D Viewer"));
115 viewer->setBackgroundColor (0, 0, 0);
116 viewer->initCameraParameters ();
117 viewer->setPosition(640+80, 480+80);
118 viewer->setCameraPosition(0, 0, -0.25, 0, -1, 0);
119 viewer->setSize(640, 480);
121 bool first_init =
true;
122 while (!local_cancelled) {
124 std::unique_lock<std::mutex> lock(m_mutex, std::try_to_lock);
126 if (lock.owns_lock()) {
127 local_update = update_pointcloud;
128 update_pointcloud =
false;
129 local_cancelled = cancelled;
130 local_pointcloud = pointcloud->makeShared();
134 if (local_update && !local_cancelled) {
135 local_update =
false;
138 viewer->addPointCloud<pcl::PointXYZ>(local_pointcloud,
"sample cloud");
139 viewer->setPointCloudRenderingProperties (pcl::visualization::PCL_VISUALIZER_POINT_SIZE, 1,
"sample cloud");
142 viewer->updatePointCloud<pcl::PointXYZ>(local_pointcloud,
"sample cloud");
146 viewer->spinOnce(10);
149 std::cout <<
"End of point cloud display thread" << std::endl;
158 const bool pointcloud_binary_format
159 #ifdef USE_PCL_VIEWER
160 , pcl::PointCloud<pcl::PointXYZ>::Ptr pointcloud
164 std::stringstream ss;
165 ss << input_directory <<
"/color_image_%04d.jpg";
166 sprintf(buffer, ss.str().c_str(), cpt);
167 std::string filename_color = buffer;
170 ss << input_directory <<
"/depth_image_%04d.bin";
171 sprintf(buffer, ss.str().c_str(), cpt);
172 std::string filename_depth = buffer;
175 ss << input_directory <<
"/point_cloud_%04d" << (pointcloud_binary_format ?
".bin" :
".pcd");
176 sprintf(buffer, ss.str().c_str(), cpt);
177 std::string filename_pointcloud = buffer;
181 std::cerr <<
"End of sequence." << std::endl;
190 std::ifstream file_depth(filename_depth.c_str(), std::ios::in | std::ios::binary);
191 if (file_depth.is_open()) {
192 unsigned int height = 0, width = 0;
195 I_depth_raw.
resize(height, width);
197 uint16_t depth_value = 0;
198 for (
unsigned int i = 0; i < height; i++) {
199 for (
unsigned int j = 0; j < width; j++) {
201 I_depth_raw[i][j] = depth_value;
207 #ifdef USE_PCL_VIEWER 208 if (pointcloud_binary_format) {
209 std::ifstream file_pointcloud(filename_pointcloud.c_str(), std::ios::in | std::ios::binary);
210 if (!file_pointcloud.is_open()) {
211 std::cerr <<
"Cannot read pointcloud file: " << filename_pointcloud << std::endl;
214 uint32_t height = 0, width = 0;
218 file_pointcloud.read( (
char *)(&is_dense),
sizeof(is_dense) );
220 pointcloud->width = width;
221 pointcloud->height = height;
222 pointcloud->is_dense = (is_dense != 0);
223 pointcloud->resize((
size_t) width*height);
225 float x = 0.0f, y = 0.0f, z = 0.0f;
226 for (uint32_t i = 0; i < height; i++) {
227 for (uint32_t j = 0; j < width; j++) {
232 pointcloud->points[(size_t) (i*width + j)].x = x;
233 pointcloud->points[(size_t) (i*width + j)].y = y;
234 pointcloud->points[(size_t) (i*width + j)].z = z;
238 if (pcl::io::loadPCDFile<pcl::PointXYZ> (filename_pointcloud, *pointcloud) == -1) {
239 std::cerr <<
"Cannot read PCD: " << filename_pointcloud << std::endl;
248 int main(
int argc,
char *argv[]) {
249 std::string input_directory =
"";
251 bool pointcloud_binary_format =
false;
252 bool save_video =
false;
253 bool color_depth =
false;
256 if (!getOptions(argc, argv, input_directory, click, pointcloud_binary_format,
257 save_video, color_depth)) {
270 bool init_display =
false;
272 #ifdef USE_PCL_VIEWER 274 ViewerWorker viewer(mutex);
275 std::thread viewer_thread(&ViewerWorker::run, &viewer);
283 writer.
setFileName(output_directory +
"/%04d.png");
291 #ifdef USE_PCL_VIEWER 293 std::lock_guard<std::mutex> lock(mutex);
294 update_pointcloud =
true;
295 quit = !readData(cpt_frame, input_directory, I_color, I_depth_raw, pointcloud_binary_format, pointcloud);
298 quit = !readData(cpt_frame, input_directory, I_color, I_depth_raw, pointcloud_binary_format);
307 d1.
init(I_color, 0, 0,
"Color image");
309 d2.
init(I_depth_color, I_color.
getWidth()+10, 0,
"Depth image");
311 d2.
init(I_depth, I_color.
getWidth()+10, 0,
"Depth image");
320 std::stringstream ss;
321 ss <<
"Frame: " << cpt_frame;
368 #ifdef USE_PCL_VIEWER 370 std::lock_guard<std::mutex> lock(mutex);
373 viewer_thread.join();
380 std::cerr <<
"Need C++11 and displayX or displayGDI!" << std::endl;
VISP_EXPORT int wait(double t0, double t)
static bool getClick(const vpImage< unsigned char > &I, bool blocking=true)
unsigned int getWidth() const
static void convert(const vpImage< unsigned char > &src, vpImage< vpRGBa > &dest)
Display for windows using GDI (available on any windows 32 platform).
static void displayText(const vpImage< unsigned char > &I, const vpImagePoint &ip, const std::string &s, const vpColor &color)
Use the X11 console to display images on unix-like OS. Thus to enable this class X11 should be instal...
static void flush(const vpImage< unsigned char > &I)
VISP_EXPORT double measureTimeMs()
VISP_EXPORT std::string getDateTime(const std::string &format="%Y/%m/%d %H:%M:%S")
static bool parse(int *argcPtr, const char **argv, vpArgvInfo *argTable, int flags)
unsigned int getSize() const
static void display(const vpImage< unsigned char > &I)
Class that enables to write easily a video file or a sequence of images.
void resize(const unsigned int h, const unsigned int w)
resize the image : Image initialization
void saveFrame(vpImage< vpRGBa > &I)
void insert(const vpImage< Type > &src, const vpImagePoint &topLeft)
void open(vpImage< vpRGBa > &I)
void setFileName(const char *filename)
static void read(vpImage< unsigned char > &I, const std::string &filename)
unsigned int getHeight() const
void init(vpImage< unsigned char > &I, int winx=-1, int winy=-1, const std::string &title="")
Class that defines a 2D point in an image. This class is useful for image processing and stores only ...
static void createDepthHistogram(const vpImage< uint16_t > &src_depth, vpImage< vpRGBa > &dest_rgba)