OPENNI学习实践-利用openni和opencv提取人的轮廓
2016-11-08 15:22
344 查看
#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); } }
进一步我们可以提取人的像素部分,将人分离出来。
相关文章推荐
- OPENNI学习实践-利用opencv显示图像
- opencv学习心得八----提取轮廓的点坐标
- Opencv2系列学习笔记10(提取连通区域轮廓)
- 学习OpenCV范例(十九)——轮廓提取和形状描述符
- opencv-ios开发笔记5 利用二值图像简单提取轮廓
- opencv学习_9 (轮廓提取)
- 我的OpenCV学习笔记(20):提取元素的轮廓及形状描述子
- openCV学习之提取外轮廓
- OpenCV 学习(利用滤波器进行边缘提取)
- OPENNI学习实践-win10(64bit)+openni1.5.4+vs2010+opencv2.4.11配置
- opencv学习-imgprocess-提取元素的轮廓及形状描述子
- opencv学习:提取物体的轮廓矩
- opencv学习笔记12 图像轮廓的提取和绘制
- Opencv2系列学习笔记10(提取连通区域轮廓) 另一个
- OpenCV学习之图像轮廓提取
- OpenCV学习——图像轮廓的提取和绘制
- 【转】Opencv2系列学习笔记10(提取连通区域轮廓)
- 学习OpenCV范例(十九)——轮廓提取和形状描述符
- opencv-ios开发笔记6 利用canny简单提取轮廓
- 我的OpenCV学习笔记(20):提取元素的轮廓及形状描述子