Assume that the VS2010 is installed and ready for use.
I. Installing and configuring the Kinect for Windows SDK v1.7
Download the Kinect for Windows SDK and developer Toolkit on the official website:
Kinectsdk-v1.7-setup.exe (mainly provides the Kinect driver and Device Access interface) (link address: https://www.microsoft.com/en-us/download/details.aspx?id=36996)
Kinectdevelopertoolkit-v1.7.0-setup.exe (mainly provides some tools to help develop, including Kinect studio and development routines for multiple programming languages) Link address: https:// www.microsoft.com/en-us/download/details.aspx%3Fid%3D36998)
After the default option is installed, access to the Kinect and the system will automatically install the search driver. Open Device Manager (right-click My Computer-> admin-> Device Manager) to see if the driver was installed successfully:
Then open developer Toolkit Browser v1.7.0 (Kinect for Windows):
To test an example of a read depth image:
two. Install OpenCV2.4.9
I have already introduced in another article, see http://blog.csdn.net/lizhiguo18/article/details/51037366 three VS2010 configuration
Open VS2010, New project, named "Kinectdepthdata." Next, you configure the associated libraries and packages and their paths
OpenCV:
(1) Include files to join the E:\opencv\build\include;
(2) Library files to join E:\opencv\build\x64\vc10\lib;
(3) In linker Input, add additional dependencies (added as needed): Opencv_core2410d.lib opencv_imgproc2410d.lib opencv_ Highgui2410d.lib opencv_ml2410d.lib opencv_video2410d.lib opencv_features2d2410d.lib opencv_calib3d2410d.lib OpenCV _objdetect2410d.lib opencv_contrib2410d.lib Opencv_legacy2410d.lib Opencv_flann2410d.lib
Kinect:
(1) Include Files to join C:\Program Files\Microsoft Sdks\kinect\v1.7\inc;
(2) Library Files to join C:\Program Files\Microsoft Sdks\kinect\v1.7\lib\amd64;
(3) In linker Input, add additional dependencies:
Kinect10.lib
four. Test using Kinect to read depth images and display with OpenCV
KinectDepthData.cpp: Defines the entry point for a console application. /************************************************ Code Original Author: zouxy09 from: http://http://blog.csdn.net/zouxy09/article/ details/8146266 Kinect for Windows SDK + OpenCV2 Read and display depth images ************************************************/#include "s
Tdafx.h "#include #include" iostream ";
#include "NuiApi.h";
#include "opencv2/opencv.hpp";
using namespace Std;
using namespace CV;
int main (int argc, char *argv[]) {Mat image;
Here we use the gray scale to express the depth of data, the farther the data darker image.create (the CV_8UC1); 1.
Initialize NUI, note that the parameters passed here have changed for DEPTH HRESULT hr = nuiinitialize (nui_initialize_flag_uses_depth);
if (FAILED (HR)) {cout << "nuiinitialize FAILED" << Endl;
return HR; }//2.
Define the event handle//create a signal event handle that reads the next frame to control whether Kinect can start reading the next frame of data HANDLE nextcolorframeevent = CreateEvent (null, TRUE, FALSE, NULL); HANDLE depthstreamhandle = NULL; Holds the handle of the image data stream to extract the data//3. Open the depth diagram information channel for the Kinect device and use Depthstreamhandle to save the handle of the stream for later reading of hr = Nuiimagestreamopen (Nui_image_type_depth, nui_image_resolution_320x240,0, 2, Nextcolorframeevent, &
Depthstreamhandle);
if (FAILED (HR))//Judge if Read is correct {cout << "could not open color image stream video" << Endl;
Nuishutdown ();
return HR;
} namedwindow ("Depthimage", cv_window_autosize); 4.
Start reading depth data while (true) {Const NUI_IMAGE_FRAME * pimageframe = NULL; 4.1 Infinite wait for new data, wait until after return if (WaitForSingleObject (nextcolorframeevent, INFINITE) = = 0) {//4.2 from just To open the stream handle of the data stream to get the frame data, read the data address stored in pimageframe hr = Nuiimagestreamgetnextframe (depthstreamhandle, 0, &pimageframe
);
if (FAILED (HR))//Judge if Read is correct {cout << "could not get depth image" << Endl;
Nuishutdown ();
return-1;
} inuiframetexture * Ptexture = pimageframe->pframetexture; Nui_locked_rect Lockedrect; 4.3 Extract the data frame to Lockedrect, which includes two data objects: Pitch per line of bytes, pbits the first byte address//and lock the data so that when we read the data, the Kinect will not modify it ptextu
Re->lockrect (0, &lockedrect, NULL, 0);
4.4 Determine whether the obtained data is valid if (lockedrect.pitch!= 0) {//4.5 convert data to OPENCV mat format for (int i = 0; i < image.rows ++i) {Uchar *ptr = image.ptr (i); Line I of the pointer//depth image data contains two formats, where the lower 12 bits of pixels represent a depth value, high 4 bit unused//Note that there is a need for conversion, because each data is 2 bytes, stored with the same
The color information obtained above is not the same uchar *pbufferrun = (uchar*) (lockedrect.pbits) + i * lockedrect.pitch;
USHORT *pbuffer = (ushort*) Pbufferrun; for (int j = 0; J < image.cols ++j) {Ptr[j] = 255-(UCHAR) (256 * pbuffer [j]/0X0FFF);
directly to the data processing}} imshow ("Depthimage", image);
} else {cout << "Buffer length of received texture is bogus\r\n" << Endl; }//5.
This frame has been disposed of, so unlock it ptexture->unlockrect (0); 6.
Release this frame data and prepare for the next frame nuiimagestreamreleaseframe (Depthstreamhandle, pimageframe);
} if (Cvwaitkey = =) {break; }//7.
Close Nui link return 0; }