openni_cam.cpp
Go to the documentation of this file.
00001 #include "openni_cam.h"
00002 
00003 #include <XnOS.h>
00004 #include <math.h>
00005 #include "geom/nmmatrix.h"
00006 
00007 #include <XnCppWrapper.h>
00008 
00009 namespace openni_cam{
00010 
00011 using namespace xn;
00012 
00013 //---------------------------------------------------------------------------
00014 // Globals
00015 //---------------------------------------------------------------------------
00016 XnRGB24Pixel* g_pTexMap = NULL;
00017 unsigned int g_nTexMapX = 0;
00018 unsigned int g_nTexMapY = 0;
00019 
00020 Context g_context;
00021 ScriptNode g_scriptNode;
00022 DepthGenerator g_depth;
00023 ImageGenerator g_image;
00024 DepthMetaData g_depthMD;
00025 ImageMetaData g_imageMD;
00026 
00027 void openni_cam::init() {
00028 
00029     XnStatus rc;
00030 
00031     EnumerationErrors errors;
00032     rc = g_context.InitFromXmlFile("../config/SamplesConfig.xml", g_scriptNode, &errors);
00033     if (rc == XN_STATUS_NO_NODE_PRESENT) {
00034         XnChar strError[1024];
00035         errors.ToString(strError, 1024);
00036         printf("%s\n", strError);
00037         exit(1);
00038     } else if (rc != XN_STATUS_OK) {
00039         printf("Open failed: %s\n", xnGetStatusString(rc));
00040         exit(1);
00041     }
00042 
00043     rc = g_context.FindExistingNode(XN_NODE_TYPE_DEPTH, g_depth);
00044     if (rc != XN_STATUS_OK) {
00045         printf("No depth node exists! Check your XML.");
00046         exit(1);
00047     }
00048 
00049     rc = g_context.FindExistingNode(XN_NODE_TYPE_IMAGE, g_image);
00050     if (rc != XN_STATUS_OK) {
00051         printf("No image node exists! Check your XML.");
00052         exit(1);
00053     }
00054 
00055     g_depth.GetMetaData(g_depthMD);
00056     g_image.GetMetaData(g_imageMD);
00057 
00058     // RGB is the only image format supported.
00059     if (g_imageMD.PixelFormat() != XN_PIXEL_FORMAT_RGB24) {
00060         printf("The device image format must be RGB24\n");
00061         exit(1);
00062     };
00063 
00064     int w_rgb = g_imageMD.XRes();
00065     int h_rgb = g_imageMD.YRes();
00066     int w_depth = g_depthMD.XRes();
00067     int h_depth = g_depthMD.YRes();
00068 
00069     rgb.resize(exttype::mint3(3,w_rgb,h_rgb));
00070     depth.resize(exttype::mint2(w_depth,h_depth));
00071     g_depth.GetAlternativeViewPointCap().SetViewPoint(g_image);
00072 
00073     // get kinect inv proj matrix
00074     geom::matrix3 Kinv;
00075     Kinv <<0;
00076     XnPoint3D Point2D, Point3D;
00077     Point2D.X = 0;
00078     Point2D.Y = 0;
00079     Point2D.Z = 1;
00080     g_depth.ConvertProjectiveToRealWorld(1, &Point2D, &Point3D);
00081     Kinv(0,2) = Point3D.X;
00082     Kinv(1,2) = Point3D.Y;
00083     Kinv(2,2) = 1;
00084     Point2D.X = 1;
00085     Point2D.Y = 0;
00086     Point2D.Z = 1;
00087     g_depth.ConvertProjectiveToRealWorld(1, &Point2D, &Point3D);
00088     Kinv(0,0) = Point3D.X-Kinv(0,2);
00089     Kinv(1,0) = Point3D.Y-Kinv(1,2);
00090     Point2D.X = 0;
00091     Point2D.Y = 1;
00092     Point2D.Z = 1;
00093     g_depth.ConvertProjectiveToRealWorld(1, &Point2D, &Point3D);
00094     Kinv(0,1) = Point3D.X-Kinv(0,2);
00095     Kinv(1,1) = Point3D.Y-Kinv(1,2);
00096     //
00097     //K = Kinv.invert();
00098 /*
00099     K << 0;
00100     Point3D.X = 0;
00101     Point3D.Y = 0;
00102     Point3D.Z = 1;
00103     g_depth.ConvertRealWorldToProjective(1, &Point3D, &Point2D);
00104     Point2D.X = Point2D.X/
00105     K(0,2) = Point2D.X;
00106     K(1,2) = Point2D.Y;
00107     K(2,2) = Point2D.Z;
00108     Point3D.X = 1;
00109     Point3D.Y = 0;
00110     Point3D.Z = 1;
00111     g_depth.ConvertRealWorldToProjective(1, &Point3D, &Point2D);
00112     K(0,0) = Point2D.X-K(0,2);
00113     K(1,0) = Point2D.Y-K(1,2);
00114     Point3D.X = 0;
00115     Point3D.Y = 1;
00116     Point3D.Z = 1;
00117     g_depth.ConvertProjectiveToRealWorld(1, &Point3D, &Point2D);
00118     K(0,1) = Point2D.X-K(0,2);
00119     K(1,1) = Point2D.Y-K(1,2);
00120 */
00121     std::ofstream f("../config/Kinv.txt");
00122     if(!f.is_open()) {
00123         throw debug_exception("cant create ../config/Kinv.txt");
00124     };
00125     for(int i=0; i<3; ++i) {
00126         for(int j=0; j<3; ++j) {
00127             f<<Kinv(i,j);
00128             if(j<3)f<<" ";
00129         };
00130         if(i<3)f<<"\n";
00131     };
00132 
00133     Kinv <<0;
00134     Point2D.X = 0;
00135     Point2D.Y = 0;
00136     Point2D.Z = 1;
00137     g_depth.ConvertProjectiveToRealWorld(1, &Point2D, &Point3D);
00138     Kinv(0,2) = Point3D.X;
00139     Kinv(1,2) = -Point3D.Y;
00140     Kinv(2,2) = 1;
00141     Point2D.X = 1;
00142     Point2D.Y = 0;
00143     Point2D.Z = 1;
00144     g_depth.ConvertProjectiveToRealWorld(1, &Point2D, &Point3D);
00145     Kinv(0,0) = Point3D.X-Kinv(0,2);
00146     Kinv(1,0) = -Point3D.Y-Kinv(1,2);
00147     Point2D.X = 0;
00148     Point2D.Y = 1;
00149     Point2D.Z = 1;
00150     g_depth.ConvertProjectiveToRealWorld(1, &Point2D, &Point3D);
00151     Kinv(0,1) = Point3D.X-Kinv(0,2);
00152     Kinv(1,1) = -Point3D.Y-Kinv(1,2);
00153     K = Kinv.invert();
00154 };
00155 
00156 
00157 bool openni_cam::get_image() {
00158     XnStatus rc = XN_STATUS_OK;
00159     // Read a new frame
00160     rc = g_context.WaitAnyUpdateAll();
00161     if (rc != XN_STATUS_OK) {
00162         printf("Read failed: %s\n", xnGetStatusString(rc));
00163         return false;
00164     };
00165 
00166     g_depth.GetMetaData(g_depthMD);
00167     g_image.GetMetaData(g_imageMD);
00168 
00169     const XnDepthPixel* pDepth = g_depthMD.Data();
00170     const XnUInt8* pImage = g_imageMD.Data();
00171 
00172     memcpy(depth.begin(),pDepth,depth.length()*sizeof(unsigned short));
00173     memcpy(rgb.begin(),pImage,rgb.length());
00174     return true;
00175 };
00176 };
 All Classes Namespaces Files Functions Variables Typedefs Defines


openni_cam
Author(s): Alexander Shekhovtsov
autogenerated on Sun Dec 1 2013 17:19:20