#include <windows.h>
#include <iostream>
#include <NuiApi.h>
#include <fstream>
#include <opencv2/opencv.hpp>
#include <math.h>
#include <highgui.h>
#include "string.h"
using namespace std;
using namespace cv;
//通过传入关节点的位置,把骨骼画出来
void drawSkeleton(Mat &image, #include <windows.h>
#include <iostream>
#include <NuiApi.h>
#include <fstream>
#include <opencv2/opencv.hpp>
#include <math.h>
#include <highgui.h>
#include "string.h"
using namespace std;
using namespace cv;
//通过传入关节点的位置,把骨骼画出来
void drawSkeleton(Mat &image, CvPoint pointSet[], int whichone);
void getColorImage(HANDLE &colorEvent, HANDLE &colorStreamHandle, Mat &colorImage);
void numDet(CvPoint pointSet[],int num);
int main(int argc, char *argv[])
{
Mat skeletonImage;
Mat colorImage;
colorImage.create(480,640,CV_8UC3);
skeletonImage.create(240, 320, CV_8UC3);
CvPoint skeletonPoint[NUI_SKELETON_COUNT][NUI_SKELETON_POSITION_COUNT]={cvPoint(0,0)};
bool tracked[NUI_SKELETON_COUNT]={FALSE};
//1、初始化NUI,注意这里是USES_SKELETON
HRESULT hr = NuiInitialize(NUI_INITIALIZE_FLAG_USES_COLOR | NUI_INITIALIZE_FLAG_USES_SKELETON);
if (FAILED(hr))
{
cout<<"NuiInitialize failed"<<endl;
return hr;
}
//2、定义骨骼信号事件句柄
HANDLE colorEvent = CreateEvent( NULL, TRUE, FALSE, NULL );
HANDLE colorStreamHandle = NULL; //保存图像数据流的句柄,用以提取数据
HANDLE skeletonEvent = CreateEvent( NULL, TRUE, FALSE, NULL );
//3、打开骨骼跟踪事件
hr = NuiImageStreamOpen(NUI_IMAGE_TYPE_COLOR, NUI_IMAGE_RESOLUTION_640x480,
0, 2, colorEvent, &colorStreamHandle);
if( FAILED( hr ) )//判断是否提取正确
{
cout<<"Could not open color image stream video"<<endl;
NuiShutdown();
return hr;
}
namedWindow("colorImage", CV_WINDOW_AUTOSIZE);
hr = NuiSkeletonTrackingEnable( skeletonEvent, 0 );
if( FAILED( hr ) )
{
cout<<"Could not open color image stream video"<<endl;
NuiShutdown();
return hr;
}
namedWindow("skeletonImage", CV_WINDOW_AUTOSIZE);
//4、开始读取骨骼跟踪数据
while(1)
{
int num=0;
const NUI_IMAGE_FRAME * pImageFrame = NULL;
NUI_SKELETON_FRAME skeletonFrame = {0}; //骨骼帧的定义
bool bFoundSkeleton = false;
if(WaitForSingleObject(colorEvent, 0)==0)
getColorImage(colorEvent, colorStreamHandle, colorImage);
//4.1、无限等待新的数据,等到后返回
if (WaitForSingleObject(skeletonEvent, INFINITE)==0)
{
//4.2、从刚才打开数据流的流句柄中得到该帧数据,读取到的数据地址存于skeletonFrame
hr = NuiSkeletonGetNextFrame( 0, &skeletonFrame);
if (SUCCEEDED(hr))
{
//NUI_SKELETON_COUNT是检测到的骨骼数(即,跟踪到的人数)
for( int i = 0 ; i < NUI_SKELETON_COUNT ; i++ )
{
NUI_SKELETON_TRACKING_STATE trackingState = skeletonFrame.SkeletonData[i].eTrackingState;
//4.3、Kinect最多检测六个人,但只能跟踪两个人的骨骼,再检查每个“人”(有可能是空,不是人)
//是否跟踪到了
if( trackingState == NUI_SKELETON_TRACKED )
{
bFoundSkeleton = true;
}
}
}
if( !bFoundSkeleton )
{
continue;
}
//4.4、平滑骨骼帧,消除抖动
NuiTransformSmooth(&skeletonFrame, NULL);
skeletonImage.setTo(0);
for( int i = 0 ; i < NUI_SKELETON_COUNT ; i++ )
{
//断定是否是一个正确骨骼的条件:骨骼被跟踪到并且肩部中心(颈部位置)必须跟踪到。
if( skeletonFrame.SkeletonData[i].eTrackingState == NUI_SKELETON_TRACKED &&
skeletonFrame.SkeletonData[i].eSkeletonPositionTrackingState[NUI_SKELETON_POSITION_SHOULDER_CENTER] != NUI_SKELETON_POSITION_NOT_TRACKED)
{
float fx, fy;
//拿到所有跟踪到的关节点的坐标,并转换为我们的深度空间的坐标,因为我们是在深度图像中
//把这些关节点标记出来的
//NUI_SKELETON_POSITION_COUNT为跟踪到的一个骨骼的关节点的数目,为20
for (int j = 0; j < NUI_SKELETON_POSITION_COUNT; j++)
{
NuiTransformSkeletonToDepthImage(skeletonFrame.SkeletonData[i].SkeletonPositions[j], &fx, &fy );
skeletonPoint[i][j].x = (int)fx;
skeletonPoint[i][j].y = (int)fy;
}
for (int j=0; j<NUI_SKELETON_POSITION_COUNT ; j++)
{
if (skeletonFrame.SkeletonData[i].eSkeletonPositionTrackingState[j] != NUI_SKELETON_POSITION_NOT_TRACKED)//跟踪点一用有三种状态:1没有被跟踪到,2跟踪到,3根据跟踪到的估计到
{
circle(skeletonImage, skeletonPoint[i][j], 3, cvScalar(0, 255, 255), 1, 8, 0);
tracked[i] = TRUE;
}
}
drawSkeleton(skeletonImage, skeletonPoint[i], i);
num++;
}
}
imshow("colorImage", colorImage);
imshow("skeletonImage", skeletonImage); //显示图像
static std::ofstream file;
file.open("C:\\Users\\Administrator\\Desktop\\number.txt",std::ios::app);
file<<"The number of the people is "<<num<<endl;
file.close();
}
else
{
cout<<"Buffer length of received texture is bogus\r\n"<<endl;
}
if (cvWaitKey(20) == 27)
break;
}
//5、关闭NUI链接
NuiShutdown();
return 0;
}
//////////////////////////////////////////////////////
/********************获取RGB图像*********************/
//////////////////////////////////////////////////////
void getColorImage(HANDLE &colorEvent, HANDLE &colorStreamHandle, Mat &colorImage)
{
const NUI_IMAGE_FRAME *colorFrame = NULL;
NuiImageStreamGetNextFrame(colorStreamHandle, 0, &colorFrame);
INuiFrameTexture *pTexture = colorFrame->pFrameTexture;
NUI_LOCKED_RECT LockedRect;
pTexture->LockRect(0, &LockedRect, NULL, 0);
if( LockedRect.Pitch != 0 )
{
for (int i=0; i<colorImage.rows; i++)
{
uchar *ptr = colorImage.ptr<uchar>(i); //第i行的指针
//每个字节代表一个颜色信息,直接使用uchar
uchar *pBuffer = (uchar*)(LockedRect.pBits) + i * LockedRect.Pitch;
for (int j=0; j<colorImage.cols; j++)
{
ptr[3*j] = pBuffer[4*j]; //内部数据是4个字节,0-1-2是BGR,第4个现在未使用
ptr[3*j+1] = pBuffer[4*j+1];
ptr[3*j+2] = pBuffer[4*j+2];
}
}
}
else
{
cout<<"捕捉色彩图像出现错误"<<endl;
}
pTexture->UnlockRect(0);
NuiImageStreamReleaseFrame(colorStreamHandle, colorFrame );
}
////////////////////////////////////
//通过传入关节点的位置,把骨骼画出来
////////////////////////////////////
void drawSkeleton(Mat &image, CvPoint pointSet[], int whichone)
{
CvScalar color;
switch(whichone) //跟踪不同的人显示不同的颜色
{
case 0:
color = cvScalar(255);
break;
case 1:
color = cvScalar(0,255);
break;
case 2:
color = cvScalar(0, 0, 255);
break;
case 3:
color = cvScalar(255, 255, 0);
break;
case 4:
color = cvScalar(255, 0, 255);
break;
case 5:
color = cvScalar(0, 255, 255);
break;
}
if((pointSet[NUI_SKELETON_POSITION_HEAD].x!=0 || pointSet[NUI_SKELETON_POSITION_HEAD].y!=0) &&
(pointSet[NUI_SKELETON_POSITION_SHOULDER_CENTER].x!=0 || pointSet[NUI_SKELETON_POSITION_SHOULDER_CENTER].y!=0))
line(image, pointSet[NUI_SKELETON_POSITION_HEAD], pointSet[NUI_SKELETON_POSITION_SHOULDER_CENTER], color, 2);
if((pointSet[NUI_SKELETON_POSITION_SHOULDER_CENTER].x!=0 || pointSet[NUI_SKELETON_POSITION_SHOULDER_CENTER].y!=0) &&
(pointSet[NUI_SKELETON_POSITION_SPINE].x!=0 || pointSet[NUI_SKELETON_POSITION_SPINE].y!=0))
line(image, pointSet[NUI_SKELETON_POSITION_SHOULDER_CENTER], pointSet[NUI_SKELETON_POSITION_SPINE], color, 2);
if((pointSet[NUI_SKELETON_POSITION_SPINE].x!=0 || pointSet[NUI_SKELETON_POSITION_SPINE].y!=0) &&
(pointSet[NUI_SKELETON_POSITION_HIP_CENTER].x!=0 || pointSet[NUI_SKELETON_POSITION_HIP_CENTER].y!=0))
line(image, pointSet[NUI_SKELETON_POSITION_SPINE], pointSet[NUI_SKELETON_POSITION_HIP_CENTER], color, 2);
//左上肢
if((pointSet[NUI_SKELETON_POSITION_S
没有合适的资源?快使用搜索试试~ 我知道了~
人脸识别C++源码
共238个文件
tlog:91个
manifest:22个
pdb:19个
5星 · 超过95%的资源 需积分: 48 185 下载量 76 浏览量
2016-10-10
11:55:29
上传
评论 12
收藏 30.16MB RAR 举报
温馨提示
VS2010+opencv+libfacedetection,里面包括人脸检测、性别识别、人脸识别、人数检测等小程序,都是借鉴别人的基础上自己改的,全部可以运行。其中人数检测是用kinect实现的,人脸识别程序可运行,但是可能由于特征选的不对,未能实现,后来不做这个了,也没深入研究。
资源推荐
资源详情
资源评论
收起资源包目录
人脸识别C++源码 (238个子文件)
getPictureN.vcxprojResolveAssemblyReference.cache 707B
getPicture.vcxprojResolveAssemblyReference.cache 707B
SVMtrain.vcxprojResolveAssemblyReference.cache 707B
testss.vcxprojResolveAssemblyReference.cache 707B
test.vcxprojResolveAssemblyReference.cache 707B
main.cpp 25KB
main.cpp 6KB
main.cpp 4KB
main.cpp 4KB
main.cpp 4KB
main.cpp 3KB
main.cpp 3KB
main.cpp 3KB
main.cpp 3KB
RECtrain.exe 136KB
SVMtrain.exe 136KB
testss.exe 136KB
getPictureN.exe 73KB
gender.exe 70KB
picTest.exe 70KB
recognition.exe 70KB
test.exe 70KB
face.exe 70KB
number.exe 60KB
getPicture.exe 51KB
FaceRec.vcxproj.filters 1KB
recognition.vcxproj.filters 942B
testss.vcxproj.filters 942B
SVMtrain.vcxproj.filters 942B
number.vcxproj.filters 942B
getPictureN.vcxproj.filters 942B
test.vcxproj.filters 942B
face.vcxproj.filters 942B
getPicture.vcxproj.filters 942B
FaceRec.h 1KB
vc100.idb 1MB
vc100.idb 795KB
vc100.idb 787KB
vc100.idb 611KB
vc100.idb 603KB
vc100.idb 603KB
vc100.idb 595KB
vc100.idb 587KB
test.ilk 1.14MB
testss.ilk 951KB
SVMtrain.ilk 820KB
RECtrain.ilk 819KB
getPicture.ilk 667KB
getPictureN.ilk 644KB
gender.ilk 599KB
face.ilk 569KB
recognition.ilk 569KB
picTest.ilk 550KB
number.ilk 531KB
getpicturen-e5203496.ipch 61.5MB
recognition.lastbuildstate 54B
SVMtrain.lastbuildstate 54B
number.lastbuildstate 54B
testss.lastbuildstate 54B
RECtrain.lastbuildstate 54B
getPictureN.lastbuildstate 54B
getPicture.lastbuildstate 54B
gender.lastbuildstate 54B
test.lastbuildstate 54B
picTest.lastbuildstate 54B
face.lastbuildstate 54B
getPicture.log 3KB
recognition.log 3KB
getPictureN.log 3KB
SVMtrain.log 3KB
testss.log 3KB
test.log 3KB
face.log 3KB
number.log 3KB
testss.exe.manifest 406B
SVMtrain.exe.manifest 406B
picTest.exe.manifest 406B
face.exe.manifest 406B
test.exe.manifest 406B
getPictureN.exe.manifest 406B
gender.exe.manifest 406B
getPicture.exe.manifest 406B
RECtrain.exe.manifest 406B
number.exe.manifest 406B
recognition.exe.manifest 406B
recognition.exe.intermediate.manifest 381B
SVMtrain.exe.intermediate.manifest 381B
RECtrain.exe.intermediate.manifest 381B
testss.exe.intermediate.manifest 381B
number.exe.intermediate.manifest 381B
getPictureN.exe.intermediate.manifest 381B
gender.exe.intermediate.manifest 381B
face.exe.intermediate.manifest 381B
picTest.exe.intermediate.manifest 381B
getPicture.exe.intermediate.manifest 381B
test.exe.intermediate.manifest 381B
main.obj 523KB
main.obj 523KB
main.obj 269KB
main.obj 257KB
共 238 条
- 1
- 2
- 3
Somebody丶
- 粉丝: 11
- 资源: 27
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
- 1
- 2
前往页