libeblearntools
/home/rex/ebltrunk/tools/libeblearntools/include/camera_kinect.hpp
00001 /***************************************************************************
00002  *   Copyright (C) 2010 by Soumith Chintala and Pierre Sermanet *
00003  *   chin@nyu.edu, sermanet@cs.nyu.edu  *
00004  *   All rights reserved.
00005  *
00006  * Redistribution and use in source and binary forms, with or without
00007  * modification, are permitted provided that the following conditions are met:
00008  *     * Redistributions of source code must retain the above copyright
00009  *       notice, this list of conditions and the following disclaimer.
00010  *     * Redistributions in binary form must reproduce the above copyright
00011  *       notice, this list of conditions and the following disclaimer in the
00012  *       documentation and/or other materials provided with the distribution.
00013  *     * Redistribution under a license not approved by the Open Source 
00014  *       Initiative (http://www.opensource.org) must display the 
00015  *       following acknowledgement in all advertising material:
00016  *        This product includes software developed at the Courant
00017  *        Institute of Mathematical Sciences (http://cims.nyu.edu).
00018  *     * The names of the authors may not be used to endorse or promote products
00019  *       derived from this software without specific prior written permission.
00020  *
00021  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED 
00022  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
00023  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00024  * DISCLAIMED. IN NO EVENT SHALL ThE AUTHORS BE LIABLE FOR ANY
00025  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
00026  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00027  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
00028  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00029  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00030  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00031  ***************************************************************************/
00032 
00033 #ifndef CAMERA_KINECT_HPP_
00034 #define CAMERA_KINECT_HPP_
00035 
00036 #ifdef __LINUX__
00037 
00038 #include <fcntl.h>              /* low-level i/o */
00039 #include <unistd.h>
00040 #include <errno.h>
00041 #include <malloc.h>
00042 #include <sys/stat.h>
00043 #include <sys/types.h>
00044 #include <sys/time.h>
00045 #include <sys/mman.h>
00046 #include <sys/ioctl.h>
00047 #endif
00048 
00049 #ifdef __KINECT__
00050 extern "C"{
00051 #include <libfreenect/libfreenect_sync.h>
00052 #include <libfreenect/libfreenect.h>
00053 }
00054 //#include <asm/types.h>          /* for videodev2.h */
00055 //#include <linux/videodev2.h>
00056 #endif
00057 
00058 namespace ebl {
00059 
00061   // constructors & initializations
00062 
00063   template <typename Tdata>
00064   camera_kinect<Tdata>::camera_kinect(int height_, int width_,
00065                                       std::ostream &out,
00066                                       std::ostream &err)
00067     : camera<Tdata>(height_, width_) {
00068     cout << "Initializing Kinect ..." << endl;
00069     frame = idx<Tdata>(480, 640, 3);
00070     mresize = false; // use regular resize instead of mean
00071     resize_mode = 1; // allow ratio loss
00072   }
00073   
00074   template <typename Tdata>
00075   camera_kinect<Tdata>::~camera_kinect() {
00076   }
00077   
00078   template <typename Tdata>
00079   idx<Tdata> camera_kinect<Tdata>::grab() {
00080 #ifdef __KINECT__
00081     void *v_depth; 
00082     unsigned int timestamp;
00083     int b = freenect_sync_get_depth
00084       (&v_depth, &timestamp, 0,
00085        (freenect_depth_format) FREENECT_DEPTH_11BIT_SIZE);
00086     if (b!=0) {
00087       out << "Kinect capture failed!" << endl;
00088       return frame;
00089     }
00090     uint16_t *depth = (uint16_t *)v_depth;
00091     //copy depth into an idx    
00092     for(int j=0;j<480;j++) {
00093       for(int i=0;i<640;i++) {
00094         for(int k=0;k<3;k++) {
00095           frame.set((Tdata)(depth[((640*j)+i)] ),j,i,k);
00096           //                      depth_idx.set((depth[((640*j)+i)]*0.124511719),j,i,k);
00097         }
00098       }
00099     }
00100 #endif
00101     return this->postprocess();
00102   }
00103 
00104 } // end namespace ebl
00105 
00106 #endif /* CAMERA_KINECT_HPP_ */