请大神指导下,怎么将深度图和RGB图转换为三维点云
默先生 2018-10-12 10:01:11 现在我的环境是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;
}