現在我的環境是Kinect2.0+opencv2.4.13+openni2
我獲取并保存深度影像和RGB影像的代碼,如下,請大神指導下,怎么將深度圖和RGB圖轉換為三維點云,不使用pcl庫,由于是新手PCl環境配置老是出問題:
#include "opencv2/core/core.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include <iostream>
#include "stdafx.h"
#include "ImageRenderer.h"
using namespace cv;
using namespace std;
//申明全域函式
cv::Mat ConvertMat(const UINT16* pBuffer, int nWidth, int nHeight, USHORT nMinDepth, USHORT nMaxDepth);// 轉換depth影像到cv::Mat
cv::Mat ConvertMat(const RGBQUAD* pBuffer, int nWidth, int nHeight);// 轉換color影像到cv::Mat
int main()
{
int depth_width = 512; //depth影像就是這么小
int depth_height = 424;
int color_widht = 1920; //color影像就是辣么大
int color_height = 1080;
cv::Mat depthImg_show = cv::Mat::zeros(depth_height, depth_width, CV_8UC3);//原始UINT16 深度影像不適合用來顯示,所以需要砍成8位的就可以了,但是顯示出來也不是非常好,最好能用原始16位影像顏色編碼,湊合著看了
cv::Mat depthImg = cv::Mat::zeros(depth_height, depth_width, CV_16UC1);//the depth image
cv::Mat colorImg = cv::Mat::zeros(color_height, color_widht, CV_8UC3);//the color image
HRESULT hr;
// Current Kinect
IKinectSensor* m_pKinectSensor = NULL;
// Depth reader
IDepthFrameReader* m_pDepthFrameReader = NULL;
// Color reader
IColorFrameReader* m_pColorFrameReader = NULL;
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);
// for color
// Initialize the Kinect and get the color reader
IColorFrameSource* pColorFrameSource = NULL;
if (SUCCEEDED(hr))
{
hr = m_pKinectSensor->get_ColorFrameSource(&pColorFrameSource);
}
if (SUCCEEDED(hr))
{
hr = pColorFrameSource->OpenReader(&m_pColorFrameReader);
}
SafeRelease(pColorFrameSource);
}
//valify the depth reader
if (!m_pDepthFrameReader)
{
cout << "FUCK! Can not find the m_pDepthFrameReader!" << endl;
cv::waitKey(0);
exit(0);
}
//valify the color reader
if (!m_pDepthFrameReader)
{
cout << "FUCK! Can not find the m_pColorFrameReader!" << endl;
cv::waitKey(0);
exit(0);
}
// get the data!
UINT nBufferSize_depth = 0;
UINT16 *pBuffer_depth = NULL;
UINT nBufferSize_coloar = 0;
RGBQUAD *pBuffer_color = NULL;
char key = 0;
while (true)
{
IDepthFrame* pDepthFrame = NULL;
HRESULT hr = m_pDepthFrameReader->AcquireLatestFrame(&pDepthFrame);
if (SUCCEEDED(hr))
{
USHORT nDepthMinReliableDistance = 0;
USHORT nDepthMaxReliableDistance = 0;
if (SUCCEEDED(hr))
{
hr = pDepthFrame->get_DepthMinReliableDistance(&nDepthMinReliableDistance);
}
if (SUCCEEDED(hr))
{
hr = pDepthFrame->get_DepthMaxReliableDistance(&nDepthMaxReliableDistance);
}
if (SUCCEEDED(hr))
{
hr = pDepthFrame->AccessUnderlyingBuffer(&nBufferSize_depth, &pBuffer_depth);
depthImg_show = ConvertMat(pBuffer_depth, depth_width, depth_height, nDepthMinReliableDistance, nDepthMaxReliableDistance);
}
}
SafeRelease(pDepthFrame);
//for color
IColorFrame* pColorFrame = NULL;
hr = m_pColorFrameReader->AcquireLatestFrame(&pColorFrame);
ColorImageFormat imageFormat = ColorImageFormat_None;
if (SUCCEEDED(hr))
{
ColorImageFormat imageFormat = ColorImageFormat_None;
if (SUCCEEDED(hr))
{
hr = pColorFrame->get_RawColorImageFormat(&imageFormat);
}
RGBQUAD* m_pColorRGBX = NULL;
m_pColorRGBX = new RGBQUAD[color_widht * color_height];
if (SUCCEEDED(hr))
{
if (imageFormat == ColorImageFormat_Bgra)//這里有兩個format,不知道具體含義,大概一個預先分配記憶體,一個需要自己開空間吧
{
hr = pColorFrame->AccessRawUnderlyingBuffer(&nBufferSize_coloar, reinterpret_cast<BYTE**>(&pBuffer_color));
}
else if (m_pColorRGBX)
{
pBuffer_color = m_pColorRGBX;
nBufferSize_coloar = color_widht * color_height * sizeof(RGBQUAD);
hr = pColorFrame->CopyConvertedFrameDataToArray(nBufferSize_coloar, reinterpret_cast<BYTE*>(pBuffer_color), ColorImageFormat_Bgra);
}
else
{
hr = E_FAIL;
}
colorImg = ConvertMat(pBuffer_color, color_widht, color_height);
}
SafeRelease(pColorFrame);
//-delete[] m_pColorRGBX;
}
namedWindow("depth", 0);
cv::imshow("depth", depthImg_show);
namedWindow("color", 0);
cv::imshow("color", colorImg);
//-自己加的,把資訊保存下來
cv::imwrite("de.jpg", depthImg_show);
cv::imwrite("co.jpg", colorImg);
key = cv::waitKey(1);
if (key == 27)
{
break;
}
}
return 0;
}
// 轉換depth影像到cv::Mat
cv::Mat ConvertMat(const UINT16* pBuffer, int nWidth, int nHeight, USHORT nMinDepth, USHORT nMaxDepth)
{
cv::Mat img(nHeight, nWidth, CV_8UC3);
uchar* p_mat = img.data;
//cv::Mat img(nHeight, nWidth, CV_16UC1);
//UINT16* p_mat = (UINT16*)img.data;
const UINT16* pBufferEnd = pBuffer + (nWidth * nHeight);
while (pBuffer < pBufferEnd)
{
USHORT depth = *pBuffer;
BYTE intensity = static_cast<BYTE>((depth >= nMinDepth) && (depth <= nMaxDepth) ? (depth % 256) : 0);
*p_mat = intensity;
p_mat++;
*p_mat = intensity;
p_mat++;
*p_mat = intensity;
p_mat++;
++pBuffer;
}
return img;
}
// 轉換color影像到cv::Mat
cv::Mat ConvertMat(const RGBQUAD* pBuffer, int nWidth, int nHeight)
{
cv::Mat img(nHeight, nWidth, CV_8UC3);
uchar* p_mat = img.data;
const RGBQUAD* pBufferEnd = pBuffer + (nWidth * nHeight);
while (pBuffer < pBufferEnd)
{
*p_mat = pBuffer->rgbBlue;
p_mat++;
*p_mat = pBuffer->rgbGreen;
p_mat++;
*p_mat = pBuffer->rgbRed;
p_mat++;
++pBuffer;
}
return img;
}
uj5u.com熱心網友回復:
深度影像與點云資料之間的轉換關系uj5u.com熱心網友回復:
不怎么會C++求幫助!!!uj5u.com熱心網友回復:
https://blog.csdn.net/woniu199166/article/details/78625061 看對應的演算法即可。。。uj5u.com熱心網友回復:
請問有沒有深度影像,能分享一下嗎 想看看是什么樣的轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/21872.html
標籤:硬件/系統
上一篇:關于LoadImage 和 BitBlt 配合在HDC上繪制影像使影像降低質量的問題
下一篇:selenium模擬出現的問題
