-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBodyIndex.cpp
122 lines (109 loc) · 3.54 KB
/
BodyIndex.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
#include "BodyIndex.h"
BodyIndex::BodyIndex()
{
}
BodyIndex::~BodyIndex()
{
SafeRelease(pBodyIndexFrameReader);
}
HRESULT BodyIndex::InitBodyIndex(IKinectSensor *kinect){
pBodyIndexFrameSource = NULL;
pBodyIndexFrameReader = NULL;
/*初始化BodyIndex*/
hr = kinect->get_BodyIndexFrameSource(&pBodyIndexFrameSource);
if (!SUCCEEDED(hr)){
cout << "获取Body源失败" << endl;
return E_FAIL;
}
hr = pBodyIndexFrameSource->OpenReader(&pBodyIndexFrameReader);
if (!SUCCEEDED(hr)){
cout << "打开读取器失败" << endl;
return E_FAIL;
}
SafeRelease(pBodyIndexFrameSource);
return S_OK;
}
Mat BodyIndex::updateBodyIndex(Mat &IndexImg){
hr = S_OK;
IBodyIndexFrame * pBodyIndexFrame = NULL;
Mat element = getStructuringElement(MORPH_RECT, Size(5, 5));
/*BodyIndex图像获取及显示*/
if (SUCCEEDED(hr)){
hr = pBodyIndexFrameReader->AcquireLatestFrame(&pBodyIndexFrame);
}
if (SUCCEEDED(hr)){
//BYTE *bodyIndexArray = new BYTE[height* width];//背景二值图是8为uchar,有人是黑色,没人是白色
//pBodyIndexFrame->CopyFrameDataToArray(height* width, bodyIndexArray);
//uchar* Data = (uchar*)img.data;
//for (int j = 0; j < height * width; ++j){
// *Data = bodyIndexArray[j]; ++Data;
// *Data = bodyIndexArray[j]; ++Data;
// *Data = bodyIndexArray[j]; ++Data;
//}
//delete[] bodyIndexArray;
UINT nBufferSize = 0;
unsigned char* pBuffer = nullptr;
pBodyIndexFrame->AccessUnderlyingBuffer(&nBufferSize, &pBuffer);
for (int x = 0; x < height; x++)
{
for (int y = 0; y < width; y++)
{
unsigned int index = x *width + y;
if (pBuffer[index] != 255)
{
IndexImg.at<Vec3b>(x, y) = Vec3b(0, 255, 0);
}
else
{
IndexImg.at<Vec3b>(x, y) = Vec3b(0, 0, 0);
}
}
}
}
dilate(IndexImg, IndexImg, element);
erode(IndexImg, IndexImg, element);
SafeRelease(pBodyIndexFrame);
cvtColor(IndexImg, thresholdImage, COLOR_BGR2GRAY);
threshold(thresholdImage, thresholdImage, 25, 255, THRESH_BINARY);
//searchForMovement(thresholdImage, img);
//DrawRec(thresholdImage, IndexImg);
//cvNamedWindow("BodyIndexImage");
//imshow("BodyIndexImage", IndexImg);
//if (waitKey(34) == VK_ESCAPE){
// cvDestroyAllWindows();
// exit(0);
//}
return thresholdImage;
}
double * BodyIndex::DrawRec(Mat thresholdImage, Mat &cameraFeed, UINT16 *pixData){
Mat temp;
Rect objectBoundingRectangle = Rect(0, 0, 0, 0);
memset(location, 0, sizeof(double)*3);
thresholdImage.copyTo(temp);
vector<vector<Point>> contours;
vector<Vec4i> hierarchy;
findContours(temp, contours, hierarchy, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_SIMPLE); //寻找轮廓
if (contours.size() > 0){
vector < vector<Point> > largestContours;
largestContours.push_back(contours.at(contours.size() - 1));
objectBoundingRectangle = boundingRect(largestContours.at(0));//找到外接最小轮廓
rectangle(cameraFeed, objectBoundingRectangle, Scalar(255, 0, 0), 5, 8, 0);//画出外接轮廓
int x = objectBoundingRectangle.x + objectBoundingRectangle.width / 2;
int y = objectBoundingRectangle.y + objectBoundingRectangle.height / 2;
circle(cameraFeed, Point(x, y), 5, Scalar(255, 0, 0), 4);
//putText(cameraFeed, "Tracking object at (" + intToString(x) + "," + intToString(y) + ")", Point(x, y), 1, 1, Scalar(255, 0, 0), 2);
INT32 pixelIndex = (INT32)(x + ((INT32)y *width));
//double depth = (pixData[pixelIndex]>>3);
double depth = (pixData[pixelIndex]);
double distance = depth / 10;
// count++;
if (depth > 0){
/*cout << "(" << x << ", " << y << ", ";
cout << distance << "cm )" << endl;*/
location[0] = x;
location[1] = y;
location[2] = distance;
}
}
return location;
}