我與Kinect v2 的 第三天_利用opencv結合Kinect API 讀取深度影像(C++)


首先讓我們先看效果圖吧!!!!!






離Kinect v2這台深度攝影機愈近就愈深  有發現嗎????

深度影像中每一個點的值,所代表的都是該點離感應器的距離

距離越遠 --> 值就越大  顏色越亮白
距離越近 --> 值就越小  顏色越深黑

畫面中純黑值是 0 的點,則代表該點無法偵測到深度!!!!!!!!!!!!!



恩恩




那我們首先來認識  Kinect v2 API 是如何 套用至  visual studio 2013 環境的



首先你必須有載Kinect v2的  SDK


然後新增一個專案

檔案----->新增----->專案------>選Visual C++ --->目錄下的win32---->win32主控台應用程式



                             下一步


                                    完成

對右側   原始程式檔   右鍵  ---->  加入------>新增項目



新增


====================================================================

專案------>屬性






















在C/C++  ------> 一般 ------> 其他Include目錄

打上  Kinect v2 加上opencv2411的  第一道 組合套餐
KINECTSDK20的Include目錄 ; opencv2411的Include目錄

 「$(KINECTSDK20_DIR)\inc;C:\opencv2411\build\include




















在  連結器 ------> 一般  ------>  其他程式庫目錄   打入 以下字串
Kinect20.lib所在路徑 ; opencv2411 lib 所在路徑

C:\Program Files\Microsoft SDKs\Kinect\v2.0_1409\Lib\x86;C:\opencv2411\build\x86\vc12\lib

也是  Kinect v2 加上opencv2411的  第二道 組合套餐

中間用 ;  隔開  切記





















最後  在   連結器 -----> 輸入------->其他相依性  

在  kernel32  字眼前  加入  以下字串

Kinect v2 加上opencv2411的  第三道 組合套餐

opencv_videostab2411d.lib;opencv_photo2411d.lib;opencv_objdetect2411d.lib;opencv_video2411d.lib;opencv_imgproc2411d.lib;opencv_core2411d.lib;opencv_highgui2411d.lib;kinect20.lib;




















// Standard Library
#include <iostream>
// OpenCV Header
#include <opencv2/opencv.hpp>
#include<opencv2\highgui\highgui.hpp>
#include<opencv2\imgproc\imgproc.hpp>
#include<opencv2\video\background_segm.hpp>
using namespace cv;
using namespace std;
// Kinect for Windows SDK Header
#include <Kinect.h>
using namespace std;
int main(int argc, char** argv)
{
// stage1.a Get default Sensor
IKinectSensor* pSensor = nullptr;
GetDefaultKinectSensor(&pSensor);
// stage1.b Open sensor
pSensor->Open();
// stage2.a Get frame source
IDepthFrameSource* pFrameSource = nullptr;
pSensor->get_DepthFrameSource(&pFrameSource);
// stage2.b Get frame description
int iWidth = 0;
int iHeight = 0;
IFrameDescription* pFrameDescription = nullptr;
pFrameSource->get_FrameDescription(&pFrameDescription);
pFrameDescription->get_Width(&iWidth);
pFrameDescription->get_Height(&iHeight);
pFrameDescription->Release();
pFrameDescription = nullptr;
// stage2.c get some dpeth only meta
UINT16 uDepthMin = 0, uDepthMax = 0;
pFrameSource->get_DepthMinReliableDistance(&uDepthMin);
pFrameSource->get_DepthMaxReliableDistance(&uDepthMax);
cout << "可靠距離: "
<< uDepthMin << " – " << uDepthMax << endl;
// perpare OpenCV
Mat mDepthImg(iHeight, iWidth, CV_16UC1);
Mat mImg8bit(iHeight, iWidth, CV_8UC1);
namedWindow("深度影像");
// stage3.a get frame reader
IDepthFrameReader* pFrameReader = nullptr;
pFrameSource->OpenReader(&pFrameReader);
// Enter main loop
while (true)
{
// stage4.a Get last frame
IDepthFrame* pFrame = nullptr;
if (pFrameReader->AcquireLatestFrame(&pFrame) == S_OK)
{
// stage4.b copy the depth map to image
pFrame->CopyFrameDataToArray(iWidth * iHeight,
reinterpret_cast<UINT16*>(mDepthImg.data));
// stage4.c convert from 16bit to 8bit
mDepthImg.convertTo(mImg8bit, CV_8U, 255.0f / uDepthMax);
imshow("深度影像", mImg8bit);
// stage4.d release frame
pFrame->Release();
}
// stage4.e check keyboard input
int c = waitKey(10);
if ((char)c == 27)
break;
}
// stage3.a release frame reader
pFrameReader->Release();
pFrameReader = nullptr;
// 2d. release Frame source
pFrameSource->Release();
pFrameSource = nullptr;
// 1c. Close Sensor
pSensor->Close();
// 1d. Release Sensor
pSensor->Release();
pSensor = nullptr;
return 0;
}






留言

這個網誌中的熱門文章

何謂淨重(Net Weight)、皮重(Tare Weight)與毛重(Gross Weight)

(2021年度)駕訓學科筆試準備題庫歸納分析_法規是非題

經得起原始碼資安弱點掃描的程式設計習慣培養(五)_Missing HSTS Header