Home > Software engineering >  Please under the guidance of the great god, how to set the depth map and RGB image is converted into
Please under the guidance of the great god, how to set the depth map and RGB image is converted into

Time:11-16

Now I environment is Kinect2.0 + opencv2.4.13 + openni2
I get and keep the depth image and RGB image code, as follows, please under the guidance of the great god, how to set the depth map and RGB image is converted into a 3 d point cloud, do not use the photo library, because it is a novice PCLS environment configuration is always a problem:
# include "opencv2/core/core HPP"
# include "opencv2/highgui/highgui. HPP"
# include "opencv2/imgproc/imgproc HPP"
#include

# include "stdafx. H"
# include "ImageRenderer. H"

using namespace cv;
using namespace std;



//that global function
CV: : Mat ConvertMat (const UINT16 * pBuffer, int nWidth, int nHeight, USHORT nMinDepth, USHORT nMaxDepth);//convert the depth image to CV: : Mat
CV: : Mat ConvertMat (const RGBQUAD * pBuffer, int nWidth, int nHeight);//convert color images to CV: : Mat

Int main ()
{
Int depth_width=512;//the depth image is so small
Int depth_height=424;
Int color_widht=1920;//color image is hot?
Int color_height=1080;

CV: : Mat depthImg_show=CV: : Mat: : zeros (depth_height depth_width, CV_8UC3);//UINT16 depth of original image is not suitable for used to display, so you need to cut into eight is ok, but display is not very good, it is best to use the original 16-bit color image coding, make do with see
CV: : Mat depthImg=CV: : Mat: : zeros (depth_height depth_width, CV_16UC1);//the the depth image
CV: : Mat colorImg=CV: : Mat: : zeros (color_height color_widht, CV_8UC3);//the color image

Retrieves the hr;
//the Current device
IKinectSensor * m_pKinectSensor=NULL;
//the Depth reader
IDepthFrameReader * m_pDepthFrameReader=NULL;
//Color reader
IColorFrameReader * m_pColorFrameReader=NULL;

Hr=GetDefaultKinectSensor (& amp; M_pKinectSensor);
If (FAILED (hr))
{
Return the hr;
}

If (m_pKinectSensor)
{
//Initialize the device and get the the depth reader
IDepthFrameSource * pDepthFrameSource=NULL;

Hr=m_pKinectSensor - & gt; The Open ();

If (SUCCEEDED (hr))
{
Hr=m_pKinectSensor - & gt; Get_DepthFrameSource (& amp; PDepthFrameSource);
}

If (SUCCEEDED (hr))
{
Hr=pDepthFrameSource - & gt; OpenReader (& amp; M_pDepthFrameReader);
}

SafeRelease (pDepthFrameSource);

//for color
//Initialize the device and get the color reader
IColorFrameSource * pColorFrameSource=NULL;
If (SUCCEEDED (hr))
{
Hr=m_pKinectSensor - & gt; Get_ColorFrameSource (& amp; PColorFrameSource);
}

If (SUCCEEDED (hr))
{
Hr=pColorFrameSource - & gt; OpenReader (& amp; M_pColorFrameReader);
}

SafeRelease (pColorFrameSource);
}

//valify the the depth reader
if (! M_pDepthFrameReader)
{
cout <"FUCK! Can not find the m_pDepthFrameReader!" CV: : waitKey (0);
The exit (0);
}
//valify the color reader
if (! M_pDepthFrameReader)
{
cout <"FUCK! Can not find the m_pColorFrameReader!" CV: : waitKey (0);
The exit (0);
}

//get the data!
UINT nBufferSize_depth=0;
UINT16 * pBuffer_depth=NULL;
UINT nBufferSize_coloar=0;
The RGBQUAD * pBuffer_color=NULL;
Char key=0;

While (true)
{
IDepthFrame * pDepthFrame=NULL;
Retrieves the hr=m_pDepthFrameReader - & gt; AcquireLatestFrame (& amp; PDepthFrame);
If (SUCCEEDED (hr))
{
USHORT nDepthMinReliableDistance=0;
USHORT nDepthMaxReliableDistance=0;
If (SUCCEEDED (hr))
{
Hr=pDepthFrame - & gt; Get_DepthMinReliableDistance (& amp; NDepthMinReliableDistance);
}

If (SUCCEEDED (hr))
{
Hr=pDepthFrame - & gt; Get_DepthMaxReliableDistance (& amp; NDepthMaxReliableDistance);
}
If (SUCCEEDED (hr))
{
Hr=pDepthFrame - & gt; AccessUnderlyingBuffer (& amp; NBufferSize_depth, & amp; PBuffer_depth);
DepthImg_show=ConvertMat (pBuffer_depth depth_width, depth_height, nDepthMinReliableDistance, nDepthMaxReliableDistance);
}
}
SafeRelease (pDepthFrame);

//for color

IColorFrame * pColorFrame=NULL;
Hr=m_pColorFrameReader - & gt; AcquireLatestFrame (& amp; PColorFrame);
ColorImageFormat imageFormat=ColorImageFormat_None;
If (SUCCEEDED (hr))
{
ColorImageFormat imageFormat=ColorImageFormat_None;
If (SUCCEEDED (hr))
{
Hr=pColorFrame - & gt; Get_RawColorImageFormat (& amp; ImageFormat);
}
The RGBQUAD * m_pColorRGBX=NULL;
M_pColorRGBX=new RGBQUAD [color_widht * color_height];
If (SUCCEEDED (hr))
{
If (imageFormat==ColorImageFormat_Bgra)//there are two format, don't know the specific meaning, probably a pre-allocated memory, a need to open space
{
Hr=pColorFrame - & gt; AccessRawUnderlyingBuffer (& amp; NBufferSize_coloar, reinterpret_cast & lt; BYTE * * & gt; (& amp; PBuffer_color));
}
Else if (m_pColorRGBX)
{
PBuffer_color=m_pColorRGBX;
Color_height nBufferSize_coloar=color_widht * * sizeof (RGBQUAD);
Hr=pColorFrame - & gt; CopyConvertedFrameDataToArray (nBufferSize_coloar, reinterpret_cast & lt; BYTE * & gt; (pBuffer_color), ColorImageFormat_Bgra);
}

The else
{
Hr=E_FAIL;
}
ColorImg=ConvertMat (pBuffer_color color_widht, color_height);
}
nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related