SceneDrawer.cpp
Go to the documentation of this file.
00001 /*****************************************************************************
00002  *                                                                            *
00003  *  OpenNI 1.0 Alpha                                                          *
00004  *  Copyright (C) 2010 PrimeSense Ltd.                                        *
00005  *                                                                            *
00006  *  This file is part of OpenNI.                                              *
00007  *                                                                            *
00008  *  OpenNI is free software: you can redistribute it and/or modify            *
00009  *  it under the terms of the GNU Lesser General Public License as published  *
00010  *  by the Free Software Foundation, either version 3 of the License, or      *
00011  *  (at your option) any later version.                                       *
00012  *                                                                            *
00013  *  OpenNI is distributed in the hope that it will be useful,                 *
00014  *  but WITHOUT ANY WARRANTY; without even the implied warranty of            *
00015  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the              *
00016  *  GNU Lesser General Public License for more details.                       *
00017  *                                                                            *
00018  *  You should have received a copy of the GNU Lesser General Public License  *
00019  *  along with OpenNI. If not, see <http://www.gnu.org/licenses/>.            *
00020  *                                                                            *
00021  *****************************************************************************/
00022 
00023 //---------------------------------------------------------------------------
00024 // Includes
00025 //---------------------------------------------------------------------------
00026 #include "cob_people_detection/SceneDrawer.h"
00027 
00028 extern xn::UserGenerator g_UserGenerator;
00029 extern xn::DepthGenerator g_DepthGenerator;
00030 
00031 #define MAX_DEPTH 10000
00032 float g_pDepthHist[MAX_DEPTH];
00033 unsigned int getClosestPowerOfTwo(unsigned int n)
00034 {
00035         unsigned int m = 2;
00036         while (m < n)
00037                 m <<= 1;
00038 
00039         return m;
00040 }
00041 
00042 XnFloat Colors[][3] =
00043 {
00044 { 0, 1, 1 },
00045 { 0, 0, 1 },
00046 { 0, 1, 0 },
00047 { 1, 1, 0 },
00048 { 1, 0, 0 },
00049 { 1, .5, 0 },
00050 { .5, 1, 0 },
00051 { 0, .5, 1 },
00052 { .5, 0, 1 },
00053 { 1, 1, .5 },
00054 { 1, 1, 1 } };
00055 XnUInt32 nColors = 10;
00056 
00057 void DrawLimb(XnUserID player, XnSkeletonJoint eJoint1, XnSkeletonJoint eJoint2)
00058 {
00059         if (!g_UserGenerator.GetSkeletonCap().IsTracking(player))
00060         {
00061                 printf("not tracked!\n");
00062                 return;
00063         }
00064 
00065         XnSkeletonJointPosition joint1, joint2;
00066         g_UserGenerator.GetSkeletonCap().GetSkeletonJointPosition(player, eJoint1, joint1);
00067         g_UserGenerator.GetSkeletonCap().GetSkeletonJointPosition(player, eJoint2, joint2);
00068 
00069         if (joint1.fConfidence < 0.5 || joint2.fConfidence < 0.5)
00070         {
00071                 return;
00072         }
00073 
00074         XnPoint3D pt[2];
00075         pt[0] = joint1.position;
00076         pt[1] = joint2.position;
00077 
00078         g_DepthGenerator.ConvertRealWorldToProjective(2, pt, pt);
00079 }
00080 
00081 void PublishPeopleImage(const xn::DepthMetaData& dmd, const xn::SceneMetaData& smd, image_transport::Publisher& pub)
00082 {
00083         const XnDepthPixel* pDepth = dmd.Data();
00084         const XnLabel* pLabels = smd.Data();
00085         unsigned int nValue = 0;
00086         XnUInt16 g_nXRes = dmd.XRes();
00087         XnUInt16 g_nYRes = dmd.YRes();
00088         cv::Mat peopleSegmentation(g_nYRes, g_nXRes, CV_8UC3);
00089 
00090         // Prepare the texture map
00091         for (unsigned int nY = 0; nY < g_nYRes; nY++)
00092         {
00093                 uchar* pDestImage = peopleSegmentation.ptr(nY);
00094                 for (unsigned int nX = 0; nX < g_nXRes; nX++)
00095                 {
00096 
00097                         pDestImage[0] = 0;
00098                         pDestImage[1] = 0;
00099                         pDestImage[2] = 0;
00100                         if (*pLabels != 0)
00101                         {
00102                                 nValue = *pDepth;
00103                                 XnLabel label = *pLabels;
00104                                 XnUInt32 nColorID = label % nColors;
00105 
00106                                 if (nValue != 0)
00107                                 {
00108                                         pDestImage[0] = 255 * Colors[nColorID][0];
00109                                         pDestImage[1] = 255 * Colors[nColorID][1];
00110                                         pDestImage[2] = 255 * Colors[nColorID][2];
00111                                 }
00112                         }
00113 
00114                         pDepth++;
00115                         pLabels++;
00116                         pDestImage += 3;
00117                 }
00118         }
00119 
00120         // todo: stop and start with respect to odometry: segmentation works best if robot is standing still
00121 
00122         // publish
00123         try
00124         {
00125                 IplImage img = (IplImage)peopleSegmentation;
00126                 sensor_msgs::ImagePtr msg = (sensor_msgs::CvBridge::cvToImgMsg(&img, "bgr8"));
00127                 msg->header.stamp = ros::Time::now();
00128                 pub.publish(msg);
00129         } catch (sensor_msgs::CvBridgeException error)
00130         {
00131                 ROS_ERROR("[openni_tracker] Could not convert IplImage to ROS message");
00132         }
00133 
00134         //      cv_bridge::CvImage bridgeImage;         did not work
00135         //      bridgeImage.image = peopleSegmentation.clone();
00136         //      sensor_msgs::ImagePtr msg = bridgeImage.toImageMsg();
00137         //      pub.publish(msg);
00138 
00139 
00140         // display for checking the output
00141         //      cv::namedWindow("Test");
00142         //      imshow("Test", peopleSegmentation);
00143         //      uchar key = cv::waitKey(10);
00144         //      if (key == 'r')
00145         //      {
00146         //        g_UserGenerator.StopGenerating();
00147         //        std::cout << "stop\n";
00148         //      }
00149         //      if (key == 'c')
00150         //      {
00151         //        g_UserGenerator.StartGenerating();
00152         //        std::cout << "start\n";
00153         //      }
00154 }


cob_people_detection
Author(s): Richard Bormann , Thomas Zwölfer
autogenerated on Fri Aug 28 2015 10:24:13