#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("image");
namedWindow("clone");
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();
XnUInt16 nUsers=xUserGenerator.GetNumberOfUsers();
if (nUsers>0&&Calibflage)
{
XnUserID* aUserID=new XnUserID[nUsers];
xUserGenerator.GetUsers(aUserID,nUsers);
XnPoint3D* UserCoM=new XnPoint3D[nUsers];
XnPoint3D* projectpoint=new XnPoint3D[nUsers];
xn::SceneMetaData* SceneMD=new xn::SceneMetaData[nUsers];
for (int i=0;i<nUsers;i++)
{
xUserGenerator.GetCoM(aUserID[i],UserCoM[i]);
mDepthGenerator.ConvertRealWorldToProjective(1,UserCoM,projectpoint);
if( xUserGenerator.GetSkeletonCap().IsTracking( aUserID[i] ) )
{
XnPoint3D skelPointsIn[24],skelPointsOut[24];
XnSkeletonJointPosition mJointTran;
for(int iter=0;iter<24;iter++)
{
//XnSkeletonJoint from 1 to 24
xUserGenerator.GetSkeletonCap().GetSkeletonJointPosition(aUserID[i],XnSkeletonJoint(iter+1),mJointTran);
skelPointsIn[iter]=mJointTran.position;
}
mDepthGenerator.ConvertRealWorldToProjective(24,skelPointsIn,skelPointsOut);
for(int d=0;d<24;d++)
{
Point startpoint=Point(skelPointsOut[d].X,skelPointsOut[d].Y);
circle(cvBGRImage,startpoint,3,CV_RGB(0,0,255),12);
}
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)
{
for(int n=0;n<3;n++)
clImage.at<uchar>(k,j*3+n)=0;
}
}
}
}
circle(cvBGRImage,Point(projectpoint[i].X,projectpoint[i].Y),10,Scalar(255,255,255),10);
}
delete[] projectpoint;
delete[] UserCoM;
delete[] aUserID;
}
imshow("image",cvBGRImage);
imshow("clone",clImage);
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);
}
}
利用GetuserPIxels去抠人像,效果一般,还需要进行进一步处理。
OPENNI中usergenerator的使用
最新推荐文章于 2023-03-04 09:52:20 发布