00001 /*!@file Devices/OpenNIGrabber.C Interface with a Kinect frame grabber via OpenNI*/ 00002 00003 // //////////////////////////////////////////////////////////////////// // 00004 // The iLab Neuromorphic Vision C++ Toolkit - Copyright (C) 2001 by the // 00005 // University of Southern California (USC) and the iLab at USC. // 00006 // See http://iLab.usc.edu for information about this project. // 00007 // //////////////////////////////////////////////////////////////////// // 00008 // Major portions of the iLab Neuromorphic Vision Toolkit are protected // 00009 // under the U.S. patent ``Computation of Intrinsic Perceptual Saliency // 00010 // in Visual Environments, and Applications'' by Christof Koch and // 00011 // Laurent Itti, California Institute of Technology, 2001 (patent // 00012 // pending; application number 09/912,225 filed July 23, 2001; see // 00013 // http://pair.uspto.gov/cgi-bin/final/home.pl for current status). // 00014 // //////////////////////////////////////////////////////////////////// // 00015 // This file is part of the iLab Neuromorphic Vision C++ Toolkit. // 00016 // // 00017 // The iLab Neuromorphic Vision C++ Toolkit is free software; you can // 00018 // redistribute it and/or modify it under the terms of the GNU General // 00019 // Public License as published by the Free Software Foundation; either // 00020 // version 2 of the License, or (at your option) any later version. // 00021 // // 00022 // The iLab Neuromorphic Vision C++ Toolkit is distributed in the hope // 00023 // that it will be useful, but WITHOUT ANY WARRANTY; without even the // 00024 // implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR // 00025 // PURPOSE. See the GNU General Public License for more details. // 00026 // // 00027 // You should have received a copy of the GNU General Public License // 00028 // along with the iLab Neuromorphic Vision C++ Toolkit; if not, write // 00029 // to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, // 00030 // Boston, MA 02111-1307 USA. // 00031 // //////////////////////////////////////////////////////////////////// // 00032 // 00033 // Primary maintainer for this file: Eric Hu <ehu@usc.edu> 00034 // $HeadURL: svn://isvn.usc.edu/software/invt/trunk/saliency/src/Devices/OpenNIGrabber.C $ 00035 // $Id: OpenNIGrabber.C 14130 2010-10-13 04:59:07Z itti $ 00036 // 00037 00038 #ifdef INVT_HAVE_OPENNI 00039 00040 #include "Devices/OpenNIGrabber.H" 00041 #include "Devices/DeviceOpts.H" 00042 00043 #define MYLOGID itsFd 00044 00045 #define CHECK_RC(rc, what) \ 00046 if (rc != XN_STATUS_OK) \ 00047 { \ 00048 LERROR("%s failed: %s\n", what, xnGetStatusString(rc)); \ 00049 } 00050 00051 00052 // ###################################################################### 00053 OpenNIGrabber::OpenNIGrabber(OptionManager& mgr, const std::string& descrName, 00054 const std::string& tagName, const ParamFlag flags) : 00055 FrameIstream(mgr, descrName, tagName), 00056 // NOTE that contrary to the common case, we may give (by default 00057 // value of 'flags') USE_MY_VAL here when we construct the 00058 // OModelParam objects; that means that we push our values into the 00059 // ModelManager as the new default values, rather than having our 00060 // param take its value from the ModelManager's default 00061 itsDims(&OPT_FrameGrabberDims, this, Dims(GL_WIN_SIZE_X, GL_WIN_SIZE_Y), flags), 00062 itsListener() 00063 { 00064 00065 } 00066 00067 // ###################################################################### 00068 void OpenNIGrabber::start1() 00069 { 00070 //initialize vars 00071 NI_DMap = NULL; 00072 NI_DMapX = 0; 00073 NI_DMapY = 0; 00074 00075 //this is a copy of the code from NiSimpleViewer. 00076 XnStatus rc; 00077 00078 // Initialize OpenNI 00079 rc = NI_context.InitFromXmlFile(SAMPLE_XML_PATH); 00080 CHECK_RC(rc, "InitFromXmlFile"); 00081 00082 //Check the nodes in the XML file 00083 rc = NI_context.FindExistingNode(XN_NODE_TYPE_DEPTH, NI_depth); 00084 rc = NI_context.FindExistingNode(XN_NODE_TYPE_IMAGE, NI_image); 00085 00086 //check specifications (ie dimensions, color type) of the acquired data 00087 NI_depth.GetMetaData(NI_depthMD); 00088 NI_image.GetMetaData(NI_imageMD); 00089 00090 // Needed to parse depth data 00091 NI_DMapX = (((unsigned short)(NI_depthMD.XRes()-1) / 512) + 1) * 512; 00092 NI_DMapY = (((unsigned short)(NI_depthMD.YRes()-1) / 512) + 1) * 512; 00093 NI_DMap = (XnUInt16*)malloc(NI_DMapX * NI_DMapY * sizeof(XnUInt16)); 00094 } 00095 00096 // ###################################################################### 00097 void OpenNIGrabber::stop2() 00098 { 00099 //Nothing to stop? 00100 NI_context.Shutdown(); 00101 } 00102 00103 // ###################################################################### 00104 OpenNIGrabber::~OpenNIGrabber() 00105 { } 00106 00107 // ###################################################################### 00108 void OpenNIGrabber::setListener(rutz::shared_ptr<FrameListener> listener) 00109 { itsListener = listener; } 00110 00111 // ###################################################################### 00112 GenericFrameSpec OpenNIGrabber::peekFrameSpec() 00113 { 00114 GenericFrameSpec result; 00115 00116 result.nativeType = GenericFrame::RGBD; 00117 result.videoFormat = VIDFMT_RGB24; 00118 result.videoByteSwap = false; 00119 result.dims = itsDims.getVal(); 00120 result.floatFlags = 0; 00121 00122 return result; 00123 } 00124 00125 // ###################################################################### 00126 GenericFrame OpenNIGrabber::readFrame() 00127 { 00128 XnStatus rc = XN_STATUS_OK; 00129 00130 // Read a new frame 00131 rc = NI_context.WaitAnyUpdateAll(); 00132 if (rc != XN_STATUS_OK) 00133 { 00134 LFATAL("Read failed: %s\n", xnGetStatusString(rc)); 00135 } 00136 00137 //Acquire Depth and Image Data 00138 NI_depth.GetMetaData(NI_depthMD); 00139 NI_image.GetMetaData(NI_imageMD); 00140 00141 //Construct depth image 00142 const XnDepthPixel* pDepth = NI_depthMD.Data(); 00143 XnUInt16* d_Tex = NI_DMap; 00144 00145 for (XnUInt y = 0; y < NI_depthMD.YRes(); ++y) 00146 { 00147 for (XnUInt x = 0; x < NI_depthMD.XRes(); ++x, ++pDepth, ++d_Tex) 00148 { 00149 if (*pDepth != 0) 00150 { 00151 int nHistValue = (*pDepth)/4; //scaling 00152 *d_Tex = nHistValue; 00153 } 00154 } 00155 } 00156 Image<uint16> img(static_cast<uint16*>(NI_DMap), NI_depthMD.XRes(), NI_depthMD.YRes()); 00157 { 00158 itsDepthImage = img; 00159 } 00160 00161 00162 //Constructs camera image 00163 Image<PixRGB<byte> > img2((PixRGB<byte>*)(NI_imageMD.RGB24Data()), NI_imageMD.XRes(), NI_imageMD.YRes()); 00164 { 00165 itsColorImage = img2; 00166 } 00167 00168 //create frame 00169 GenericFrame NIframe(itsColorImage, itsDepthImage); 00170 00171 return NIframe; 00172 } 00173 // ###################################################################### 00174 xn::Context* OpenNIGrabber::getContext() 00175 { 00176 return &NI_context; 00177 } 00178 00179 #endif 00180 00181 // ###################################################################### 00182 /* So things look consistent in everyone's emacs... */ 00183 /* Local Variables: */ 00184 /* indent-tabs-mode: nil */ 00185 /* End: */