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

18 篇文章 1 订阅
#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
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值