rgbd
rgbd_to_pcd.cpp
Go to the documentation of this file.
1 #include <rgbd/serialization.h>
2 #include <rgbd/image.h>
3 #include <rgbd/view.h>
4 
5 #include <fstream>
6 #include <iostream>
7 
8 #include <pcl/io/pcd_io.h>
9 #include <pcl/point_types.h>
10 
11 #include <cmath>
12 
13 #include <opencv2/highgui/highgui.hpp>
14 
15 #include <typeinfo>
16 
17 
18 int main(int argc, char **argv) {
19 
20  if (argc < 2)
21  {
22  std::cout << "Usage:\n\n rgbd_to_pcd FILENAME.rgbd [FILENAME2.rgbd ...]\n\n";
23  return 1;
24  }
25 
26  for (int i=1; i < argc; ++i)
27  {
28  std::string name = std::string(argv[i]);
29 
30  // read
31  std::ifstream f_in;
32  f_in.open(name.c_str(), std::ifstream::binary);
33 
34  if (!f_in.is_open())
35  {
36  std::cerr << "Could not open '" << name << "'." << std::endl;
37  continue;
38  }
39 
41 
42  rgbd::Image image;
43  rgbd::deserialize(a_in, image);
44 
45  pcl::PointCloud<pcl::PointXYZRGB> cloud;
46 
47  // Fill in the cloud data
48  cloud.width = image.getRGBImage().cols;
49  cloud.height = image.getRGBImage().rows;
50  cloud.is_dense = false;
51  cloud.resize (cloud.width * cloud.height);
52 
53  double fx = image.getCameraModel().fx();
54  double fy = image.getCameraModel().fy();
55 
56  double half_height = 0.5 * cloud.height;
57  double half_width = 0.5 * cloud.width;
58  for (uint i=0; i < cloud.height; ++i)
59  {
60  for (uint j=0; j < cloud.width; ++j)
61  {
62  cv::Vec3b bgr = image.getRGBImage().at<cv::Vec3b>(i,j);
63  double d = image.getDepthImage().at<float>(i,j);
64 
65  cloud.at(j,i).x = (-half_width+j) * d / fx;
66  cloud.at(j,i).y = (-half_height+i) * d / fy;
67  cloud.at(j,i).z = d;
68  cloud.at(j,i).r = bgr[2];
69  cloud.at(j,i).g = bgr[1];
70  cloud.at(j,i).b = bgr[0];
71  }
72  }
73 
74  size_t lastindex = name.find_last_of(".");
75  name = name.substr(0, lastindex);
76 
77  std::string pcd_filename = name + ".pcd";
78 
79  pcl::io::savePCDFileASCII (pcd_filename, cloud);
80  std::cout << "Saved " << cloud.size () << " data points to " << pcd_filename << std::endl;
81  }
82 
83  return 0;
84 }
fstream
std::string
rgbd::Image::getDepthImage
const cv::Mat & getDepthImage() const
Get the depth image.
Definition: image.h:72
rgbd::deserialize
bool deserialize(tue::serialization::InputArchive &a, Image &image)
Definition: serialization.cpp:174
std::cerr
iostream
cmath
std::cout
rgbd::Image
Definition: image.h:43
std::string::c_str
T c_str(T... args)
std::string::find_last_of
T find_last_of(T... args)
std::ifstream::open
T open(T... args)
image.h
rgbd::Image::getRGBImage
const cv::Mat & getRGBImage() const
Get the RGB color image.
Definition: image.h:78
std::string::substr
T substr(T... args)
main
int main(int argc, char **argv)
Definition: rgbd_to_pcd.cpp:18
tue::serialization::InputArchive
std::endl
T endl(T... args)
rgbd::Image::getCameraModel
const image_geometry::PinholeCameraModel & getCameraModel() const
Get the camera model.
Definition: image.h:96
view.h
typeinfo
serialization.h
std::ifstream::is_open
T is_open(T... args)
std::ifstream