Go to the documentation of this file.00001 #include <sdf_tracker.h>
00002 #include <OpenNI.h>
00003 #include <opencv2/opencv.hpp>
00004
00005 class FrameGrabber
00006 {
00007 public:
00008 FrameGrabber(int width, int height, int fps=0);
00009 ~FrameGrabber();
00010
00011 void getColor(cv::Mat &color);
00012 void getDepth(cv::Mat &depth);
00013 private:
00014 openni::Device* rgbd_camera_;
00015 openni::VideoStream *depth_stream_;
00016 openni::VideoStream *color_stream_;
00017 openni::VideoFrameRef depth_FrameRef_;
00018 openni::VideoFrameRef color_FrameRef_;
00019 };
00020
00021
00022 int main(int argc, char* argv[])
00023 {
00024
00025 SDF_Parameters myParameters;
00026
00027 myParameters.interactive_mode = true;
00028 myParameters.resolution = 0.02;
00029 myParameters.Dmax = 0.2;
00030 myParameters.Dmin = -0.1;
00031
00032
00033
00034 myParameters.XSize = 250;
00035 myParameters.YSize = 250;
00036 myParameters.ZSize = 250;
00037
00038
00039 myParameters.image_width = 320;
00040 myParameters.image_height = 240;
00041 int fps = 60;
00042
00043
00044 Eigen::Matrix4d initialTransformation =
00045 Eigen::MatrixXd::Identity(4,4);
00046
00047
00048
00049
00050
00051 myParameters.pose_offset = initialTransformation;
00052
00053
00054 SDFTracker myTracker(myParameters);
00055
00056
00057 FrameGrabber myCamera(myParameters.image_width, myParameters.image_height, fps);
00058
00059 cv::Mat depth;
00060
00061 do
00062 {
00063 myCamera.getDepth(depth);
00064 myTracker.FuseDepth(depth);
00065
00066 }while(!myTracker.Quit());
00067
00068 printf("Dumping volume and exiting.\n");
00069 myTracker.SaveSDF();
00070
00071 return 0;
00072 }
00073
00074 FrameGrabber::FrameGrabber(int width, int height, int fps)
00075 {
00076 if (fps==0)
00077 {
00078 if (width==640 && height ==480) fps = 30;
00079 else if (width==320 && height ==240) fps = 60;
00080 else{ fps=25; }
00081 std::cout << "fps not set, assuming " << fps << " to match resolution of "<< width << "x" << height << std::endl;
00082 }
00083 openni::Status rc = openni::OpenNI::initialize();
00084 if (rc != openni::STATUS_OK)
00085 {
00086 printf("Initialize failed\n%s\n", openni::OpenNI::getExtendedError());
00087 exit(1);
00088 }
00089 std::cout << "OpenNI initialized" << std::endl;
00090
00091 rgbd_camera_ = new openni::Device();
00092 color_stream_ = new openni::VideoStream;
00093 depth_stream_ = new openni::VideoStream;
00094 openni::VideoMode vm;
00095
00096 rc = rgbd_camera_->open(openni::ANY_DEVICE);
00097 if (rc != openni::STATUS_OK)
00098 {
00099 printf("Couldn't open device\n%s\n", openni::OpenNI::getExtendedError());
00100 exit(2);
00101 }
00102 std::cout << "Opened device" << std::endl;
00103
00104 rc = depth_stream_->create(*rgbd_camera_, openni::SENSOR_DEPTH);
00105 if (rc != openni::STATUS_OK)
00106 {
00107 printf("Couldn't create depth stream\n%s\n", openni::OpenNI::getExtendedError());
00108 exit(3);
00109 }
00110 std::cout << "Created depth stream" << std::endl;
00111
00112 for(int idx = 0; idx< depth_stream_->getSensorInfo().getSupportedVideoModes().getSize(); idx++)
00113 {
00114 vm = depth_stream_->getSensorInfo().getSupportedVideoModes()[idx];
00115 if( (height == vm.getResolutionY()) && (width == vm.getResolutionX()) && (fps == vm.getFps()) && (vm.getPixelFormat()==openni::PIXEL_FORMAT_DEPTH_1_MM))
00116 {
00117 std::cout << "matching depth mode :" << vm.getResolutionX()<<"x"<<vm.getResolutionY()<<"@"<<vm.getFps()<<"Hz using format "<< vm.getPixelFormat() <<std::endl;
00118 break;
00119 }
00120 }
00121 std::cout << "setting depth mode :" << vm.getResolutionX()<<"x"<<vm.getResolutionY()<<"@"<<vm.getFps()<<"Hz using format "<< vm.getPixelFormat() <<std::endl;
00122
00123 rc = depth_stream_->setVideoMode(vm);
00124 if (rc != openni::STATUS_OK) printf("Couldn't set desired video mode \n%s\n", openni::OpenNI::getExtendedError());
00125
00126 rc = depth_stream_->setMirroringEnabled(TRUE);
00127 if (rc != openni::STATUS_OK) printf("Couldn't set mirroring \n%s\n", openni::OpenNI::getExtendedError());
00128
00129 rc = color_stream_->create(*rgbd_camera_, openni::SENSOR_COLOR);
00130 if (rc != openni::STATUS_OK)
00131 {
00132 printf("Couldn't create color stream\n%s\n", openni::OpenNI::getExtendedError());
00133 exit(5);
00134 }
00135 std::cout << "Created color stream" << std::endl;
00136
00137 rc = rgbd_camera_->setImageRegistrationMode(openni::IMAGE_REGISTRATION_DEPTH_TO_COLOR);
00138 if (rc != openni::STATUS_OK) printf("Couldn't enable depth registration \n%s\n", openni::OpenNI::getExtendedError());
00139
00140 rc = rgbd_camera_->setDepthColorSyncEnabled(true);
00141 if (rc != openni::STATUS_OK) printf("Couldn't enable frame syncronization \n%s\n", openni::OpenNI::getExtendedError());
00142
00143 for(int idx = 0; idx< color_stream_->getSensorInfo().getSupportedVideoModes().getSize(); idx++)
00144 {
00145 vm = color_stream_->getSensorInfo().getSupportedVideoModes()[idx];
00146 if( (height == vm.getResolutionY()) && (width == vm.getResolutionX()) && (fps == vm.getFps()) && (vm.getPixelFormat()==openni::PIXEL_FORMAT_RGB888))
00147 {
00148 std::cout << "found matching color mode :" << vm.getResolutionX()<<"x"<<vm.getResolutionY()<<"@"<<vm.getFps()<<"Hz using format "<< vm.getPixelFormat() <<std::endl;
00149 break;
00150 }
00151 }
00152
00153 std::cout << "setting color mode :" << vm.getResolutionX()<<"x"<<vm.getResolutionY()<<"@"<<vm.getFps()<<"Hz using format "<< vm.getPixelFormat() <<std::endl;
00154
00155 rc = color_stream_->setVideoMode(vm);
00156 if (rc != openni::STATUS_OK) printf("Couldn't set desired video mode \n%s\n", openni::OpenNI::getExtendedError());
00157
00158 rc = color_stream_->setMirroringEnabled(TRUE);
00159 if (rc != openni::STATUS_OK) printf("Couldn't set mirroring \n%s\n", openni::OpenNI::getExtendedError());
00160
00161 rc = depth_stream_->start();
00162 if (rc != openni::STATUS_OK)
00163 {
00164 printf("Couldn't start the depth stream\n%s\n", openni::OpenNI::getExtendedError());
00165 exit(4);
00166 }
00167 std::cout << "Started depth stream" << std::endl;
00168
00169 rc = color_stream_->start();
00170 if (rc != openni::STATUS_OK)
00171 {
00172 printf("Couldn't start the color stream\n%s\n", openni::OpenNI::getExtendedError());
00173 exit(6);
00174 }
00175 std::cout << "Started color stream" << std::endl;
00176 std::cout << "Finished setting up camera " << std::endl;
00177 }
00178
00179 FrameGrabber::~FrameGrabber()
00180 {
00181
00182
00183 depth_stream_->stop();
00184 depth_stream_->destroy();
00185 delete depth_stream_;
00186
00187
00188 color_stream_->stop();
00189 color_stream_->destroy();
00190 delete color_stream_;
00191
00192 rgbd_camera_->close();
00193
00194 delete rgbd_camera_;
00195 openni::OpenNI::shutdown();
00196 }
00197
00198 void FrameGrabber::getDepth(cv::Mat &depthmap)
00199 {
00200
00201 depth_stream_->readFrame(&depth_FrameRef_);
00202 const openni::DepthPixel* pDepth = (const openni::DepthPixel*)depth_FrameRef_.getData();
00203
00204 if(depthmap.data == NULL)
00205 depthmap = cv::Mat(depth_FrameRef_.getHeight(), depth_FrameRef_.getWidth(), CV_32FC1, cv::Scalar(0.0));
00206
00207 for(int i=0; i<depth_FrameRef_.getHeight(); ++i)
00208 #pragma omp parallel
00209 for(int j=0; j<depth_FrameRef_.getWidth(); ++j)
00210 {
00211 depthmap.at<float>(i,depth_FrameRef_.getWidth()-(j+1)) = (pDepth[i*depth_FrameRef_.getWidth() + j] == 0) ? std::numeric_limits<float>::quiet_NaN() : float(pDepth[i*depth_FrameRef_.getWidth() + j])/1000;
00212 }
00213 }
00214
00215 void FrameGrabber::getColor(cv::Mat &rgbImage)
00216 {
00217 color_stream_->readFrame(&color_FrameRef_);
00218 const openni::RGB888Pixel* pColor = (const openni::RGB888Pixel*)color_FrameRef_.getData();
00219
00220 if(rgbImage.data == NULL)
00221 rgbImage = cv::Mat(color_FrameRef_.getHeight(), color_FrameRef_.getWidth(), CV_8UC3, cv::Scalar(0));
00222
00223 for(int i=0; i<color_FrameRef_.getHeight(); ++i)
00224 #pragma omp parallel
00225 for(int j=0; j<color_FrameRef_.getWidth(); ++j)
00226 {
00227 rgbImage.at<cv::Vec3b>(i,color_FrameRef_.getWidth()-(j+1))[0] = pColor[i*color_FrameRef_.getWidth() + j ].b;
00228 rgbImage.at<cv::Vec3b>(i,color_FrameRef_.getWidth()-(j+1))[1] = pColor[i*color_FrameRef_.getWidth() + j ].g;
00229 rgbImage.at<cv::Vec3b>(i,color_FrameRef_.getWidth()-(j+1))[2] = pColor[i*color_FrameRef_.getWidth() + j ].r;
00230 }
00231 }