1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304
|
/*
* Software License Agreement (BSD License)
*
* Point Cloud Library (PCL) - www.pointclouds.org
* Copyright (c) 2011, Willow Garage, Inc.
*
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following
* disclaimer in the documentation and/or other materials provided
* with the distribution.
* * Neither the name of the copyright holder(s) nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
*/
#include <pcl/common/time.h> //fps calculations
#include <pcl/io/openni_grabber.h>
#include <pcl/io/openni_camera/openni_driver.h>
#include <pcl/console/parse.h>
#include <pcl/visualization/pcl_visualizer.h>
#include <vtkSmartPointer.h>
#include <vtkImageImport.h>
#include <vtkTIFFWriter.h>
#include <vtkImageFlip.h>
#include <mutex>
#include <string>
#include <vector>
#include <boost/date_time/posix_time/posix_time.hpp> // for to_iso_string, local_time
#define SHOW_FPS 1
#if SHOW_FPS
#define FPS_CALC(_WHAT_) \
do \
{ \
static unsigned count = 0;\
static double last = pcl::getTime ();\
double now = pcl::getTime (); \
++count; \
if (now - last >= 1.0) \
{ \
std::cout << "Average framerate("<< _WHAT_ << "): " << double(count)/double(now - last) << " Hz" << std::endl; \
count = 0; \
last = now; \
} \
}while(false)
#else
#define FPS_CALC(_WHAT_) \
do \
{ \
}while(false)
#endif
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
class SimpleOpenNIViewer
{
public:
SimpleOpenNIViewer (pcl::OpenNIGrabber& grabber)
: grabber_ (grabber)
, importer_ (vtkSmartPointer<vtkImageImport>::New ())
, depth_importer_ (vtkSmartPointer<vtkImageImport>::New ())
, writer_ (vtkSmartPointer<vtkTIFFWriter>::New ())
, flipper_ (vtkSmartPointer<vtkImageFlip>::New ())
{
importer_->SetNumberOfScalarComponents (3);
importer_->SetDataScalarTypeToUnsignedChar ();
depth_importer_->SetNumberOfScalarComponents (1);
depth_importer_->SetDataScalarTypeToUnsignedShort ();
writer_->SetCompressionToPackBits ();
flipper_->SetFilteredAxes (1);
}
void
image_callback (const openni_wrapper::Image::Ptr &image,
const openni_wrapper::DepthImage::Ptr &depth_image, float)
{
FPS_CALC ("image callback");
std::lock_guard<std::mutex> lock (image_mutex_);
image_ = image;
depth_image_ = depth_image;
}
void
run ()
{
std::function<
void (const openni_wrapper::Image::Ptr&, const openni_wrapper::DepthImage::Ptr&, float)
> image_cb = [this] (const openni_wrapper::Image::Ptr& img, const openni_wrapper::DepthImage::Ptr& depth, float f)
{
image_callback (img, depth, f);
};
boost::signals2::connection image_connection = grabber_.registerCallback (image_cb);
grabber_.start ();
unsigned char* rgb_data = nullptr;
unsigned rgb_data_size = 0;
const void* data;
while (true)
{
std::lock_guard<std::mutex> lock (image_mutex_);
std::string time = boost::posix_time::to_iso_string (boost::posix_time::microsec_clock::local_time ());
if (image_)
{
FPS_CALC ("writer callback");
openni_wrapper::Image::Ptr image;
image.swap (image_);
if (image->getEncoding() == openni_wrapper::Image::RGB)
{
data = reinterpret_cast<const void*> (image->getMetaData ().Data ());
importer_->SetWholeExtent (0, image->getWidth () - 1, 0, image->getHeight () - 1, 0, 0);
importer_->SetDataExtentToWholeExtent ();
}
else
{
if (rgb_data_size < image->getWidth () * image->getHeight ())
{
rgb_data_size = image->getWidth () * image->getHeight ();
rgb_data = new unsigned char [rgb_data_size * 3];
importer_->SetWholeExtent (0, image->getWidth () - 1, 0, image->getHeight () - 1, 0, 0);
importer_->SetDataExtentToWholeExtent ();
}
image->fillRGB (image->getWidth (), image->getHeight (), rgb_data);
data = reinterpret_cast<const void*> (rgb_data);
}
const std::string filename = "frame_" + time + "_rgb.tiff";
importer_->SetImportVoidPointer (const_cast<void*>(data), 1);
importer_->Update ();
flipper_->SetInputConnection (importer_->GetOutputPort ());
flipper_->Update ();
writer_->SetFileName (filename.c_str ());
writer_->SetInputConnection (flipper_->GetOutputPort ());
writer_->Write ();
}
if (depth_image_)
{
openni_wrapper::DepthImage::Ptr depth_image;
depth_image.swap (depth_image_);
const std::string filename = "frame_" + time + "_depth.tiff";
depth_importer_->SetWholeExtent (0, depth_image->getWidth () - 1, 0, depth_image->getHeight () - 1, 0, 0);
depth_importer_->SetDataExtentToWholeExtent ();
depth_importer_->SetImportVoidPointer (const_cast<void*> (reinterpret_cast<const void*> (depth_image->getDepthMetaData ().Data ())), 1);
depth_importer_->Update ();
flipper_->SetInputConnection (depth_importer_->GetOutputPort ());
flipper_->Update ();
writer_->SetFileName (filename.c_str ());
writer_->SetInputConnection (flipper_->GetOutputPort ());
writer_->Write ();
}
}
grabber_.stop ();
image_connection.disconnect ();
delete[] rgb_data;
}
pcl::OpenNIGrabber& grabber_;
std::mutex image_mutex_;
openni_wrapper::Image::Ptr image_;
openni_wrapper::DepthImage::Ptr depth_image_;
vtkSmartPointer<vtkImageImport> importer_, depth_importer_;
vtkSmartPointer<vtkTIFFWriter> writer_;
vtkSmartPointer<vtkImageFlip> flipper_;
};
void
usage (char ** argv)
{
std::cout << "usage: " << argv[0] << " [((<device_id> | <path-to-oni-file>) [-imagemode <mode>] | -l [<device_id>]| -h | --help)]" << std::endl;
std::cout << argv[0] << " -h | --help : shows this help" << std::endl;
std::cout << argv[0] << " -l : list all available devices" << std::endl;
std::cout << argv[0] << " -l <device-id> : list all available modes for specified device" << std::endl;
std::cout << " device_id may be #1, #2, ... for the first, second etc device in the list"
#ifndef _WIN32
<< " or" << std::endl
<< " bus@address for the device connected to a specific usb-bus / address combination or" << std::endl
<< " <serial-number>"
#endif
<< std::endl;
std::cout << std::endl;
std::cout << "examples:" << std::endl;
std::cout << argv[0] << " \"#1\"" << std::endl;
std::cout << " uses the first device." << std::endl;
std::cout << argv[0] << " \"./temp/test.oni\"" << std::endl;
std::cout << " uses the oni-player device to play back oni file given by path." << std::endl;
std::cout << argv[0] << " -l" << std::endl;
std::cout << " lists all available devices." << std::endl;
std::cout << argv[0] << " -l \"#2\"" << std::endl;
std::cout << " lists all available modes for the second device" << std::endl;
#ifndef _WIN32
std::cout << argv[0] << " A00361800903049A" << std::endl;
std::cout << " uses the device with the serial number \'A00361800903049A\'." << std::endl;
std::cout << argv[0] << " 1@16" << std::endl;
std::cout << " uses the device on address 16 at USB bus 1." << std::endl;
#endif
return;
}
int
main(int argc, char ** argv)
{
std::string device_id;
pcl::OpenNIGrabber::Mode image_mode = pcl::OpenNIGrabber::OpenNI_Default_Mode;
pcl::OpenNIGrabber::Mode depth_mode = pcl::OpenNIGrabber::OpenNI_Default_Mode;
if (argc >= 2)
{
device_id = argv[1];
if (device_id == "--help" || device_id == "-h")
{
usage(argv);
return 0;
}
if (device_id == "-l")
{
if (argc >= 3)
{
pcl::OpenNIGrabber grabber (argv[2]);
openni_wrapper::OpenNIDevice::Ptr device = grabber.getDevice ();
std::vector<std::pair<int, XnMapOutputMode> > modes;
if (device->hasImageStream ())
{
std::cout << std::endl << "Supported image modes for device: " << device->getVendorName () << " , " << device->getProductName () << std::endl;
modes = grabber.getAvailableImageModes ();
for (const auto& mode : modes)
{
std::cout << mode.first << " = " << mode.second.nXRes << " x " << mode.second.nYRes << " @ " << mode.second.nFPS << std::endl;
}
}
}
else
{
openni_wrapper::OpenNIDriver& driver = openni_wrapper::OpenNIDriver::getInstance ();
if (driver.getNumberDevices () > 0)
{
for (unsigned deviceIdx = 0; deviceIdx < driver.getNumberDevices (); ++deviceIdx)
{
std::cout << "Device: " << deviceIdx + 1 << ", vendor: " << driver.getVendorName (deviceIdx) << ", product: " << driver.getProductName (deviceIdx)
<< ", connected: " << driver.getBus (deviceIdx) << " @ " << driver.getAddress (deviceIdx) << ", serial number: \'" << driver.getSerialNumber (deviceIdx) << "\'" << std::endl;
}
}
else
std::cout << "No devices connected." << std::endl;
std::cout <<"Virtual Devices available: ONI player" << std::endl;
}
return (0);
}
}
else
{
openni_wrapper::OpenNIDriver& driver = openni_wrapper::OpenNIDriver::getInstance ();
if (driver.getNumberDevices () > 0)
std::cout << "Device Id not set, using first device." << std::endl;
}
unsigned imagemode;
if (pcl::console::parse (argc, argv, "-imagemode", imagemode) != -1)
image_mode = pcl::OpenNIGrabber::Mode (imagemode);
unsigned depthmode;
if (pcl::console::parse (argc, argv, "-depthmode", depthmode) != -1)
depth_mode = pcl::OpenNIGrabber::Mode (depthmode);
pcl::OpenNIGrabber grabber (device_id, depth_mode, image_mode);
SimpleOpenNIViewer v (grabber);
v.run ();
return (0);
}
|