38 #include <opencv2/highgui/highgui.hpp>
39 #include <opencv2/imgproc/imgproc.hpp>
40 #include <opencv2/imgproc/types_c.h>
41 #if CV_MAJOR_VERSION >= 3
42 #include <opencv2/videoio/videoio_c.h>
44 #include <pcl/visualization/cloud_viewer.h>
51 "rtabmap-rgbd_camera [options] driver\n"
52 " driver Driver number to use: 0=OpenNI-PCL (Kinect)\n"
53 " 1=OpenNI2 (Kinect and Xtion PRO Live)\n"
54 " 2=Freenect (Kinect)\n"
55 " 3=OpenNI-CV (Kinect)\n"
56 " 4=OpenNI-CV-ASUS (Xtion PRO Live)\n"
57 " 5=Freenect2 (Kinect v2)\n"
58 " 6=DC1394 (Bumblebee2)\n"
59 " 7=FlyCapture2 (Bumblebee2)\n"
62 " 10=Kinect for Windows 2 SDK\n"
64 " 12=Kinect for Azure SDK\n"
66 " 14=ZED Open Capture\n"
68 " 16=XVSDK (SeerSense)\n"
70 " -rate #.# Input rate Hz (default 0=inf)\n"
71 " -device # Device ID (number or string)\n"
72 " -save_stereo \"path\" Save stereo images in a folder or a video file (side by side *.avi).\n"
73 " -fourcc \"XXXX\" Four characters FourCC code (default is \"MJPG\") used\n"
74 " when saving stereo images to a video file.\n"
75 " See http://www.fourcc.org/codecs.php for more codes.\n");
83 printf(
"\nSignal %d caught...\n", sig);
95 std::string stereoSavePath;
97 std::string fourcc =
"MJPG";
105 for(
int i=1;
i<argc; ++
i)
107 if(strcmp(
argv[
i],
"-rate") == 0)
124 if (strcmp(
argv[
i],
"-device") == 0)
137 if(strcmp(
argv[
i],
"-save_stereo") == 0)
142 stereoSavePath =
argv[
i];
150 if(strcmp(
argv[
i],
"-fourcc") == 0)
156 if(fourcc.size() != 4)
158 UERROR(
"fourcc should be 4 characters.");
168 if(strcmp(
argv[
i],
"--help") == 0 || strcmp(
argv[
i],
"-help") == 0)
174 printf(
"Unrecognized option \"%s\"",
argv[
i]);
179 driver = atoi(
argv[
i]);
180 if(driver < 0 || driver > 15)
182 UERROR(
"driver should be between 0 and 15.");
187 UINFO(
"Using driver %d (device=%s)", driver, deviceId.empty()?
"0": deviceId.c_str());
192 if(!stereoSavePath.empty())
194 UWARN(
"-save_stereo option cannot be used with RGB-D drivers.");
195 stereoSavePath.clear();
206 UERROR(
"Not built with OpenNI2 support...");
215 UERROR(
"Not built with Freenect support...");
224 UERROR(
"Not built with OpenNI from OpenCV support...");
233 UERROR(
"Not built with OpenNI from OpenCV support...");
242 UERROR(
"Not built with Freenect2 support...");
252 UERROR(
"Not built with DC1394 support...");
261 UERROR(
"Not built with FlyCapture2/Triclops support...");
270 UERROR(
"Not built with ZED sdk support...");
275 else if (driver == 9)
279 UERROR(
"Not built with RealSense support...");
284 else if (driver == 10)
288 UERROR(
"Not built with Kinect for Windows 2 SDK support...");
293 else if (driver == 11)
297 UERROR(
"Not built with RealSense2 SDK support...");
302 else if (driver == 12)
306 UERROR(
"Not built with Kinect for Azure SDK support...");
311 else if (driver == 13)
315 UERROR(
"Not built with Mynt Eye S support...");
320 else if (driver == 14)
324 UERROR(
"Not built with Zed Open Capture support...");
329 else if (driver == 15)
333 UERROR(
"Not built with depthai-core support...");
338 else if (driver == 16)
342 UERROR(
"Not built with XVisio SDK support...");
354 printf(
"Camera init failed! Please select another driver (see \"--help\").\n");
360 if (
data.imageRaw().empty())
362 printf(
"Cloud not get frame from the camera!\n");
366 if(
data.imageRaw().cols %
data.depthOrRightRaw().cols != 0 ||
data.imageRaw().rows %
data.depthOrRightRaw().rows != 0)
368 UWARN(
"RGB (%d/%d) and depth (%d/%d) frames are not the same size! The registered cloud cannot be shown.",
369 data.imageRaw().cols,
data.imageRaw().rows,
data.depthOrRightRaw().cols,
data.depthOrRightRaw().rows);
371 pcl::visualization::CloudViewer * viewer = 0;
372 if((
data.stereoCameraModels().empty() ||
data.stereoCameraModels()[0].isValidForProjection()) &&
373 (
data.cameraModels().empty() || !
data.cameraModels()[0].isValidForProjection()))
375 UWARN(
"Camera not calibrated! The registered cloud cannot be shown.");
379 viewer =
new pcl::visualization::CloudViewer(
"cloud");
382 cv::VideoWriter videoWriter;
384 if(!stereoSavePath.empty() &&
385 !
data.imageRaw().empty() &&
386 !
data.rightRaw().empty())
390 if(
data.imageRaw().size() ==
data.rightRaw().size())
394 UERROR(
"You should set the input rate when saving stereo images to a video file.");
397 cv::Size targetSize =
data.imageRaw().size();
398 targetSize.width *= 2;
402 CV_FOURCC(fourcc.at(0), fourcc.at(1), fourcc.at(2), fourcc.at(3)),
405 data.imageRaw().channels() == 3);
409 UERROR(
"Images not the same size, cannot save stereo images to the video file.");
419 UERROR(
"Directory \"%s\" doesn't exist.", stereoSavePath.c_str());
420 stereoSavePath.clear();
430 while(!
data.imageRaw().empty() && (viewer==0 || !viewer->wasStopped()) &&
running)
432 cv::Mat rgb =
data.imageRaw();
433 if(!
data.depthRaw().empty() && (
data.depthRaw().type() == CV_16UC1 ||
data.depthRaw().type() == CV_32FC1))
436 cv::Mat depth =
data.depthRaw();
437 if(depth.type() == CV_32FC1)
442 if(!rgb.empty() && rgb.cols % depth.cols == 0 && rgb.rows % depth.rows == 0 &&
443 data.cameraModels().size() &&
444 data.cameraModels()[0].isValidForProjection())
448 data.cameraModels()[0]);
451 viewer->showCloud(cloud,
"cloud");
453 else if(!depth.empty() &&
454 data.cameraModels().size() &&
455 data.cameraModels()[0].isValidForProjection())
459 data.cameraModels()[0]);
461 viewer->showCloud(cloud,
"cloud");
465 unsigned short min=0,
max = 2048;
466 uMinMax((
unsigned short*)depth.data, depth.rows*depth.cols,
min,
max);
467 depth.convertTo(tmp, CV_8UC1, 255.0/
max);
469 cv::imshow(
"Video", rgb);
470 cv::imshow(
"Depth", tmp);
472 else if(!
data.rightRaw().empty())
475 cv::Mat right =
data.rightRaw();
476 cv::imshow(
"Left", rgb);
477 cv::imshow(
"Right", right);
479 if(rgb.cols == right.cols && rgb.rows == right.rows &&
data.stereoCameraModels().size()==1 &&
data.stereoCameraModels()[0].isValidForProjection())
481 if(right.channels() == 3)
483 cv::cvtColor(right, right, CV_BGR2GRAY);
487 data.stereoCameraModels()[0]);
490 viewer->showCloud(cloud,
"cloud");
494 int c = cv::waitKey(10);
498 if(videoWriter.isOpened())
500 cv::Mat left =
data.imageRaw();
501 cv::Mat right =
data.rightRaw();
502 if(left.size() == right.size())
504 cv::Size targetSize = left.size();
505 targetSize.width *= 2;
506 cv::Mat targetImage(targetSize, left.type());
507 if(right.type() != left.type())
510 cv::cvtColor(right, tmp, left.channels()==3?CV_GRAY2BGR:CV_BGR2GRAY);
513 UASSERT(left.type() == right.type());
515 cv::Mat roiA(targetImage, cv::Rect( 0, 0, left.size().width, left.size().height ));
517 cv::Mat roiB( targetImage, cvRect( left.size().width, 0, left.size().width, left.size().height ) );
520 videoWriter.write(targetImage);
521 printf(
"Saved frame %d to \"%s\"\n",
id, stereoSavePath.c_str());
525 UERROR(
"Left and right images are not the same size!?");
528 else if(!stereoSavePath.empty())
530 cv::imwrite(stereoSavePath+
"/"+
"left/"+
uNumber2Str(
id) +
".jpg",
data.imageRaw());
531 cv::imwrite(stereoSavePath+
"/"+
"right/"+
uNumber2Str(
id) +
".jpg",
data.rightRaw());
532 printf(
"Saved frames %d to \"%s/left\" and \"%s/right\" directories\n",
id, stereoSavePath.c_str(), stereoSavePath.c_str());
537 printf(
"Closing...\n");
542 cv::destroyWindow(
"Video");
543 cv::destroyWindow(
"Depth");