標籤:kinect
V1深度解析度:320x240
V2深度解析度:512x424
1, 開啟深度映像幀的方式
對於V1:
hr = m_PNuiSensor->NuiImageStreamOpen( NUI_IMAGE_TYPE_DEPTH,NUI_IMAGE_RESOLUTION_320x240,0, 2, m_hNextDepthFrameEvent, &m_hDepthStreamHandle); if( FAILED( hr ) ) { cout<<"Could notopen image stream video"<<endl; return hr; }這種方式可以設定解析度
對於V2:
// Initialize the Kinect and get the depth reader IDepthFrameSource* pDepthFrameSource =NULL;首先使用 hr = m_pKinectSensor->Open();//開啟Kinect if (SUCCEEDED(hr)) { hr =m_pKinectSensor->get_DepthFrameSource(&pDepthFrameSource); }方法get_DepthFrameSource開啟彩色幀的源。然後使用 if (SUCCEEDED(hr)) { hr =pDepthFrameSource->OpenReader(&m_pDepthFrameReader); } SafeRelease(pDepthFrameSource);方法OpenReader開啟彩色幀讀取器。
2, 更新深度幀的方式
對於V1:使用NuiImageStreamGetNextFrame方法
NuiImageStreamGetNextFrame(m_hDepthStreamHandle,0, &pImageFrame);;//得到該幀資料</span>
對於V2:使用AcquireLatestFrame方法
if (!m_pDepthFrameReader) { return; } IDepthFrame* pDepthFrame = NULL; HRESULT hr =m_pDepthFrameReader->AcquireLatestFrame(&pDepthFrame);
3, 資料的處理方式
對於V1:這種資料擷取方式比較明朗看到資料內部結構,
INuiFrameTexture *pTexture =pImageFrame->pFrameTexture; NUI_LOCKED_RECT LockedRect; pTexture->LockRect(0, &LockedRect,NULL, 0); RGBQUAD q; if( LockedRect.Pitch != 0 ) { //BYTE * pBuffer = (BYTE*)(LockedRect.pBits); //INT size = LockedRect.size; //memcpy_s(m_pDepthBuffer,size, pBuffer, size); //USHORT* pBufferRun =reinterpret_cast<USHORT*>(m_pDepthBuffer); for (int i=0; i<image.rows; i++) { //USHORT* ptr = (USHORT*)depthIndexImage->height; //USHORT* pDepthRow =(USHORT*)(i); //BYTE * pBuffer = (BYTE*)(LockedRect.pBits); uchar *ptr =image.ptr<uchar>(i); //第i行的指標 uchar * pBuffer =(uchar*)(LockedRect.pBits)+i*LockedRect.Pitch; USHORT* pBufferRun =(USHORT*) pBuffer;//注意這裡需要轉換,因為每個資料是2個位元組,儲存的同上面的顏色資訊不一樣,這裡是2個位元組一個資訊,不能再用BYTE,轉化為USHORT for (int j=0; j<image.cols; j++) { //ptr[j] = 255 -(BYTE)(256*pBufferRun[j]/0x0fff);//直接將資料歸一化處理 //ptr[j] = pBufferRun[i * 640 + j]; // ptr[j] = 255 -(uchar)(256 * pBufferRun[j]/0x0fff); //直接將資料歸一化處理 int player =pBufferRun[j]&7; int data =(pBufferRun[j]&0xfff8) >> 3; uchar imageData = 255-(uchar)(256*data/0x0fff); q.rgbBlue = q.rgbGreen =q.rgbRed = 0; switch(player) { case 0: q.rgbRed = imageData /2; q.rgbBlue = imageData / 2; q.rgbGreen = imageData/ 2; break; case 1: q.rgbRed =imageData; break; case 2: q.rgbGreen =imageData; break; case 3: q.rgbRed = imageData /4; q.rgbGreen = q.rgbRed*4; //這裡利用乘的方法,而不用原來的方法可以避免不整除的情況 q.rgbBlue =q.rgbRed*4; //可以在後面的getTheContour()中配合使用,避免遺漏一些情況 break; case 4: q.rgbBlue = imageData /4; q.rgbRed = q.rgbBlue*4; q.rgbGreen =q.rgbBlue*4; break; case 5: q.rgbGreen = imageData/ 4; q.rgbRed =q.rgbGreen*4; q.rgbBlue =q.rgbGreen*4; break; case 6: q.rgbRed = imageData /2; q.rgbGreen = imageData/ 2; q.rgbBlue =q.rgbGreen*2; break; case 7: q.rgbRed = 255 - (imageData / 2 ); q.rgbGreen = 255 - (imageData / 2 ); q.rgbBlue = 255 - (imageData / 2 ); } ptr[3*j] = q.rgbBlue; ptr[3*j+1] = q.rgbGreen; ptr[3*j+2] = q.rgbRed; } } imshow("depthImage",image); //顯示映像得到的最終形式可以用OpenCV顯示。
對於V2:
RGBQUAD* m_pDepthRGBX;;//深度資料存放區位置m_pDepthRGBX(NULL)//建構函式初始化 // create heap storage for color pixel data in RGBXformat m_pDepthRGBX = new RGBQUAD[cDepthWidth *cDepthHeight]; //下邊就是AcquireLatestFrame之後處理資料 INT64 nTime = 0; IFrameDescription* pFrameDescription =NULL; int nWidth = 0; int nHeight = 0; USHORTnDepthMinReliableDistance = 0; USHORT nDepthMaxDistance =0; UINT nBufferSize = 0; UINT16 *pBuffer = NULL; if (SUCCEEDED(hr)) { hr =pDepthFrame->AccessUnderlyingBuffer(&nBufferSize, &pBuffer); } if (SUCCEEDED(hr)) { ProcessDepth(nTime, pBuffer,nWidth, nHeight, nDepthMinReliableDistance, nDepthMaxDistance); }
感覺目前得到的pBuffer就是儲存的深度資料,問題是如何用OpenCV來顯示呢? 這種資料的內部結構是神馬樣子呢?然後如何用OpenCV顯示出映像資料呢?待查…
Kinect for Windows V2和V1對比開發___深度資料擷取