OPENNI学习实践-利用openni和opencv提取人的轮廓

来源:互联网 发布:手机淘宝如何管理店铺 编辑:程序博客网 时间:2024/05/01 04:58
#include <stdlib.h>#include <iostream>#include <string>#include <XnCppWrapper.h>#include <opencv2/opencv.hpp>#include <opencv2/highgui/highgui.hpp>#include <opencv2/core/core.hpp>using namespace std;using namespace xn;using namespace cv;void XN_CALLBACK_TYPE LostUser(UserGenerator& generator,XnUserID user,void* pCookie);void XN_CALLBACK_TYPE NewUser(UserGenerator& generator,XnUserID user,void* pCookie);void XN_CALLBACK_TYPE poseDetected(xn::PoseDetectionCapability& poseDetection,const XnChar* strPose,XnUserID user, void *pCookie);void XN_CALLBACK_TYPE CalibrationStart(xn::SkeletonCapability& skeleton,XnUserID user,void* pCookie);void XN_CALLBACK_TYPE CalibrationEnd(xn::SkeletonCapability& skeleton,XnUserID user,XnBool bSuccess,void* pCookie);bool Calibflage;int startSkelPoints[14]={1,2,6,6,12,17,6,7,12,13,17,18,21,22};int endSkelPoints[14]={2,3,12,21,17,21,7,9,13,15,18,20,22,24};void CheckOpenNIError(XnStatus eResult,string sStatus){if(eResult != XN_STATUS_OK)cout << sStatus << "Error: " << xnGetStatusString(eResult) << endl;//P53}int main(){XnStatus eResult = XN_STATUS_OK;namedWindow("clone");namedWindow("mask");DepthMetaData mDepth;ImageMetaData mImage;Context mContext;eResult = mContext.Init();CheckOpenNIError(eResult,"Initialize context");UserGenerator xUserGenerator;xUserGenerator.Create(mContext);ImageGenerator mImageGenerator;mImageGenerator.Create(mContext);DepthGenerator mDepthGenerator;mDepthGenerator.Create(mContext);XnMapOutputMode mapMode;mapMode.nXRes=640;mapMode.nYRes=480;mapMode.nFPS=30;mDepthGenerator.SetMapOutputMode(mapMode);mImageGenerator.SetMapOutputMode(mapMode);mDepthGenerator.GetMirrorCap().SetMirror(true);mImageGenerator.GetMirrorCap().SetMirror(true);mDepthGenerator.GetAlternativeViewPointCap().SetViewPoint(mImageGenerator);XnCallbackHandle hUseCB;//检测用户xUserGenerator.RegisterUserCallbacks(NewUser,LostUser,NULL,hUseCB);XnCallbackHandle hposeCB;//检测姿势xUserGenerator.GetPoseDetectionCap().RegisterToPoseCallbacks(poseDetected,NULL,&xUserGenerator,hposeCB);XnCallbackHandle hCalibCB;//骨骼标定xn::SkeletonCapability mSC=NULL;mSC=xUserGenerator.GetSkeletonCap();mSC.SetSkeletonProfile(XN_SKEL_PROFILE_ALL);mSC.RegisterCalibrationCallbacks(CalibrationStart,CalibrationEnd,&xUserGenerator,hCalibCB);mContext.StartGeneratingAll();while (1){mContext.WaitAndUpdateAll();mImageGenerator.GetMetaData(mImage);Mat cvRGBImage(mImage.FullYRes(),mImage.FullXRes(),CV_8UC3,(char*) mImage.Data());Mat cvBGRImage;cvtColor(cvRGBImage,cvBGRImage,CV_RGB2BGR);Mat clImage=cvBGRImage.clone();Mat mask(mImage.FullYRes(),mImage.FullXRes(),CV_8UC1,Scalar(255));Mat dstimage;dstimage=clImage.clone();XnUInt16 nUsers=xUserGenerator.GetNumberOfUsers();if (nUsers>0&&Calibflage){XnUserID* aUserID=new XnUserID[nUsers];xUserGenerator.GetUsers(aUserID,nUsers);xn::SceneMetaData* SceneMD=new xn::SceneMetaData[nUsers];for (int i=0;i<nUsers;i++){if( xUserGenerator.GetSkeletonCap().IsTracking( aUserID[i] ) ){xUserGenerator.GetUserPixels(aUserID[i],SceneMD[i]);for (int j=0;j<clImage.cols;j++){for (int k=0;k<clImage.rows;k++){if (SceneMD[i](j,k)==0){mask.at<uchar>(k,j)=0;}}}}}delete[] aUserID;Mat element=getStructuringElement(MORPH_RECT,Size(11,11));dilate(mask,mask,element);vector<vector<Point>> contours;findContours(mask,contours,CV_RETR_EXTERNAL,CV_CHAIN_APPROX_NONE);for (int i=0;i<contours.size();i++){drawContours(dstimage,contours,i,Scalar(255,255,255),5);}Rect brect=boundingRect(contours[0]);line(dstimage,Point(brect.x,brect.y),Point(brect.x+brect.width,brect.y),Scalar(255,255,255),5);line(dstimage,Point(brect.x+brect.width,brect.y),Point(brect.x+brect.width,brect.y+brect.height),Scalar(255,255,255),5);line(dstimage,Point(brect.x+brect.width,brect.y+brect.height),Point(brect.x,brect.y+brect.height),Scalar(255,255,255),5);line(dstimage,Point(brect.x,brect.y+brect.height),Point(brect.x,brect.y),Scalar(255,255,255),5);imshow("mask",mask);//clImage.copyTo(dstimage,mask);imshow("clone",dstimage);}waitKey(30);}mContext.Shutdown();return 0;}void XN_CALLBACK_TYPE LostUser(UserGenerator& generator,XnUserID user,void* pCookie){printf("Lost User:%d\n",user);}void XN_CALLBACK_TYPE NewUser(UserGenerator& generator,XnUserID user,void* pCookie){printf("New User identified:%d\n",user);generator.GetPoseDetectionCap().StartPoseDetection("Psi",user);}void XN_CALLBACK_TYPE poseDetected(xn::PoseDetectionCapability& poseDetection,const XnChar* strPose,XnUserID user, void *pCookie){printf("pose %s,detected for user %d",strPose,user);((xn::UserGenerator*)pCookie)->GetSkeletonCap().RequestCalibration(user,FALSE);poseDetection.StopPoseDetection( user );}void XN_CALLBACK_TYPE CalibrationStart(xn::SkeletonCapability& skeleton,XnUserID user,void* pCookie){printf("Calibration start for user:%d\n",user);}void XN_CALLBACK_TYPE CalibrationEnd(xn::SkeletonCapability& skeleton,XnUserID user,XnBool bSuccess,void* pCookie){printf("Calibration complete: user");if (bSuccess){printf("success");Calibflage=true;skeleton.StartTracking(user);}else{printf("Failure\n");((xn::UserGenerator*)pCookie)->GetPoseDetectionCap().StartPoseDetection("Psi",user);}}

进一步我们可以提取人的像素部分,将人分离出来。


0 0