KinectIO.cpp
Go to the documentation of this file.
1 
28 /*
29  * KinectIO.cpp
30  *
31  * Created on: 20.03.2012
32  * Author: Thomas Wiemann
33  */
34 
35 #include "lvr2/io/KinectIO.hpp"
36 #include "lvr2/io/PointBuffer.hpp"
37 #include "lvr2/io/DataStruct.hpp"
38 
39 #include <Eigen/Dense>
40 
41 #include <vector>
42 #include <set>
43 
44 namespace lvr2
45 {
46 
47 KinectIO* KinectIO::m_instance = 0;
48 
50 {
51  if(KinectIO::m_instance == 0)
52  {
54  }
55 
56  return KinectIO::m_instance;
57 
58 }
59 
61 {
62  // Dept calibration matrix initialization
63  float fx = 594.21f;
64  float fy = 591.04f;
65  float a = -0.0030711f;
66  float b = 3.3309495f;
67  float cx = 339.5f;
68  float cy = 242.7f;
69 
70  m_depthMatrix <<
71  1/fx, 0, 0, 0,
72  0, -1/fy, 0, 0,
73  0, 0, 0, a,
74  -cx/fx, cy/fy, -1, b;
75 
76  // Init freenect stuff
77  m_freenect = new Freenect::Freenect;
78 
79  m_grabber = &m_freenect->createDevice<KinectGrabber>(0);
80  m_grabber->setDepthFormat(FREENECT_DEPTH_11BIT);
81  m_grabber->startVideo();
82  m_grabber->startDepth();
83 }
84 
86 {
87  //delete m_freenect;
88 }
89 
91 {
92  // Get depth image from sensor
93  std::vector<short> depthImage(480 * 680, 0);
94  m_grabber->getDepthImage(depthImage);
95 
96  std::vector<uint8_t> colorImage(480 * 680 * 3, 0);
97  m_grabber->getColorImage(colorImage);
98 
99  std::set<int> nans;
100  for(size_t i = 0; i < depthImage.size(); i++)
101  {
102  if(isnan(depthImage[i])) nans.insert(i);
103  }
104 
105  // Return null pointer if no image was grabbed
106  if(depthImage.size() == 0) return PointBufferPtr();
107 
108  size_t numPoints = depthImage.size() - nans.size();
109 
110  // Convert depth image into point cloud
111  PointBufferPtr buffer(new PointBuffer);
112  floatArr points(new float[numPoints * 3]);
113  ucharArr colors(new unsigned char[numPoints * 3]);
114 
115  int i,j;
116  int index = 0;
117  int c = 0;
118  for (i = 0; i < 480; i++) {
119  for (j = 0; j < 640; j++) {
120 
121  if(nans.find(c) == nans.end())
122  {
123  Eigen::Vector4f v;
124  v << j, i, (float)(depthImage[i * 640 + j]), 1.0f;
125  v = m_depthMatrix.transpose() * v;
126 
127  points[3 * index ] = v(0) / v(3);
128  points[3 * index + 1] = v(1) / v(3);
129  points[3 * index + 2] = v(2) / v(3);
130 
131  colors[3 * index ] = colorImage[3 * c ];
132  colors[3 * index + 1] = colorImage[3 * c + 1];
133  colors[3 * index + 2] = colorImage[3 * c + 2];
134  index++;
135  }
136  c++;
137  }
138  }
139 
140  buffer->setPointArray(points, numPoints);
141  buffer->setColorArray(colors, numPoints);
142  return buffer;
143 }
144 
145 } // namespace lvr2
lvr2::floatArr
boost::shared_array< float > floatArr
Definition: DataStruct.hpp:133
lvr2::KinectIO
Definition: KinectIO.hpp:44
KinectIO.hpp
lvr2::KinectIO::m_grabber
KinectGrabber * m_grabber
Definition: KinectIO.hpp:57
lvr2::PointBufferPtr
std::shared_ptr< PointBuffer > PointBufferPtr
Definition: PointBuffer.hpp:130
isnan
#define isnan(x)
Definition: PCDIO.cpp:35
lvr2::KinectIO::m_freenect
Freenect::Freenect * m_freenect
Definition: KinectIO.hpp:58
lvr2::PointBuffer
A class to handle point information with an arbitrarily large number of attribute channels....
Definition: PointBuffer.hpp:51
lvr2::Vector4f
Eigen::Vector4f Vector4f
Eigen 4D vector, single precision.
Definition: MatrixTypes.hpp:131
lvr2::KinectIO::getBuffer
PointBufferPtr getBuffer()
Definition: KinectIO.cpp:90
lvr2::KinectIO::~KinectIO
virtual ~KinectIO()
Definition: KinectIO.cpp:85
PointBuffer.hpp
lvr2::KinectGrabber
Definition: KinectGrabber.hpp:47
DataStruct.hpp
Datastructures for holding loaded data.
lvr2::KinectIO::KinectIO
KinectIO()
Definition: KinectIO.cpp:60
lvr2::KinectGrabber::getDepthImage
void getDepthImage(std::vector< short > &img)
Returns the currently present point cloud data.
Definition: KinectGrabber.cpp:63
scripts.create_png.colors
colors
Definition: create_png.py:41
lvr2::KinectIO::m_depthMatrix
Eigen::Matrix4f m_depthMatrix
Definition: KinectIO.hpp:59
lvr2::KinectIO::instance
static KinectIO * instance()
Definition: KinectIO.cpp:49
lvr2::ucharArr
boost::shared_array< unsigned char > ucharArr
Definition: DataStruct.hpp:137
lvr2
Definition: BaseBufferManipulators.hpp:39
lvr2::KinectIO::m_instance
static KinectIO * m_instance
Definition: KinectIO.hpp:61
lvr2::KinectGrabber::getColorImage
void getColorImage(std::vector< uint8_t > &img)
Definition: KinectGrabber.cpp:79


lvr2
Author(s): Thomas Wiemann , Sebastian Pütz , Alexander Mock , Lars Kiesow , Lukas Kalbertodt , Tristan Igelbrink , Johan M. von Behren , Dominik Feldschnieders , Alexander Löhr
autogenerated on Wed Mar 2 2022 00:37:23