前几天看骨骼数据发现确实挺复杂的,那还是先搞定深度数据吧。
代码托管在Github上,你可以进入这个页面点击右侧的“Clone in Desktop”下载本系列所有代码~
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 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 |
// DepthBasic-OpenCV.cpp : 定义控制台应用程序的入口点。 /**************************************************** 程序功能:Kinect V2深度数据用OpenCV显示 开发环境:win32控制台应用程序 x86程序 (程序类型) VisualStudio 2012 (开发工具) OpenCV2.4.10 (显示界面库 vc11库, http://brightguo.com/opencv) KinectSDK-v2.0-PublicPreview1409-Setup (Kinect SDK驱动版本, http://brightguo.com/kinect2) Windows 8.1 (操作系统) 博客文章:http://brightguo.com/kinect2-depth-opencv 代码地址:https://github.com/guoming0000/KinectV2/tree/master/DepthBasic-OpenCV 开发人员:小明 开发时间:2014-10-20~ 2014-10-21 联系方式: i@guoming.me (邮箱,推荐联系方式) http://brightguo.com (网站,体感资讯和知识汇总) http://weibo.com/guoming0000 (新浪微博,用于发布体感等科技资讯,请勿私信) ******************************************************/ #include "stdafx.h" #include "opencv2/opencv.hpp" #include <windows.h> #include <Kinect.h>// Kinect Header files using namespace cv; // Safe release for interfaces template<class Interface> inline void SafeRelease(Interface *& pInterfaceToRelease) { if (pInterfaceToRelease != NULL) { pInterfaceToRelease->Release(); pInterfaceToRelease = NULL; } } //定义Kinect方法类 class Kinect { public: static const int cDepthWidth = 512; static const int cDepthHeight = 424; Kinect(); ~Kinect(); HRESULT InitKinect();//初始化Kinect void Update();//更新数据 void ProcessDepth(const UINT16* pBuffer, int nWidth, int nHeight, USHORT nMinDepth, USHORT nMaxDepth);//处理得到的数据 private: IKinectSensor* m_pKinectSensor;// Current Kinect IDepthFrameReader* m_pDepthFrameReader;// Color reader RGBQUAD* m_pDepthRGBX; }; //主函数 int main() { Kinect kinect; kinect.InitKinect(); while(1) { kinect.Update(); if(waitKey(1) >= 0)//按下任意键退出 { break; } } return 0; } Kinect::Kinect() { m_pKinectSensor = NULL; m_pDepthFrameReader = NULL; m_pDepthRGBX = new RGBQUAD[cDepthWidth * cDepthHeight];// create heap storage for color pixel data in RGBX format } Kinect::~Kinect() { if (m_pDepthRGBX) { delete [] m_pDepthRGBX; m_pDepthRGBX = NULL; } SafeRelease(m_pDepthFrameReader);// done with color frame reader if (m_pKinectSensor) { m_pKinectSensor->Close();// close the Kinect Sensor } SafeRelease(m_pKinectSensor); } HRESULT Kinect::InitKinect() { HRESULT hr; hr = GetDefaultKinectSensor(&m_pKinectSensor); if (FAILED(hr)) { return hr; } if (m_pKinectSensor) { // Initialize the Kinect and get the depth reader IDepthFrameSource* pDepthFrameSource = NULL; hr = m_pKinectSensor->Open(); if (SUCCEEDED(hr)) { hr = m_pKinectSensor->get_DepthFrameSource(&pDepthFrameSource); } if (SUCCEEDED(hr)) { hr = pDepthFrameSource->OpenReader(&m_pDepthFrameReader); } SafeRelease(pDepthFrameSource); } if (!m_pKinectSensor || FAILED(hr)) { printf("No ready Kinect found! \n"); return E_FAIL; } return hr; } void Kinect::Update() { if (!m_pDepthFrameReader) { return; } IDepthFrame* pDepthFrame = NULL; HRESULT hr = m_pDepthFrameReader->AcquireLatestFrame(&pDepthFrame); if (SUCCEEDED(hr)) { IFrameDescription* pFrameDescription = NULL; int nWidth = 0; int nHeight = 0; USHORT nDepthMinReliableDistance = 0; USHORT nDepthMaxDistance = 0; UINT nBufferSize = 0; UINT16 *pBuffer = NULL; if (SUCCEEDED(hr)) { hr = pDepthFrame->get_FrameDescription(&pFrameDescription); } if (SUCCEEDED(hr)) { hr = pFrameDescription->get_Width(&nWidth); } if (SUCCEEDED(hr)) { hr = pFrameDescription->get_Height(&nHeight); } if (SUCCEEDED(hr)) { hr = pDepthFrame->get_DepthMinReliableDistance(&nDepthMinReliableDistance); } if (SUCCEEDED(hr)) { // In order to see the full range of depth (including the less reliable far field depth) // we are setting nDepthMaxDistance to the extreme potential depth threshold nDepthMaxDistance = USHRT_MAX; // Note: If you wish to filter by reliable depth distance, uncomment the following line. //// hr = pDepthFrame->get_DepthMaxReliableDistance(&nDepthMaxDistance); } if (SUCCEEDED(hr)) { hr = pDepthFrame->AccessUnderlyingBuffer(&nBufferSize, &pBuffer); } if (SUCCEEDED(hr)) { ProcessDepth( pBuffer, nWidth, nHeight, nDepthMinReliableDistance, nDepthMaxDistance); } SafeRelease(pFrameDescription); } SafeRelease(pDepthFrame); } void Kinect::ProcessDepth(const UINT16* pBuffer, int nWidth, int nHeight, USHORT nMinDepth, USHORT nMaxDepth) { // Make sure we've received valid data if (m_pDepthRGBX && pBuffer && (nWidth == cDepthWidth) && (nHeight == cDepthHeight)) { RGBQUAD* pRGBX = m_pDepthRGBX; // end pixel is start + width*height - 1 const UINT16* pBufferEnd = pBuffer + (nWidth * nHeight); while (pBuffer < pBufferEnd) { USHORT depth = *pBuffer; // To convert to a byte, we're discarding the most-significant // rather than least-significant bits. // We're preserving detail, although the intensity will "wrap." // Values outside the reliable depth range are mapped to 0 (black). // Note: Using conditionals in this loop could degrade performance. // Consider using a lookup table instead when writing production code. BYTE intensity = static_cast<BYTE>((depth >= nMinDepth) && (depth <= nMaxDepth) ? (depth % 256) : 0); pRGBX->rgbRed = intensity; pRGBX->rgbGreen = intensity; pRGBX->rgbBlue = intensity; ++pRGBX; ++pBuffer; } // Draw the data with OpenCV Mat DepthImage(nHeight, nWidth, CV_8UC4, m_pDepthRGBX); Mat show = DepthImage.clone(); imshow("DepthImage", show); } } |