参考文章地址:http://blog.163.com/gz_ricky/blog/static/182049118201122311118325/
首先需要在程序的初始化部分, 也就是初始化DepthGenerator的地方初始化ImageGenerator
XnStatus eResult = XN_STATUS_OK; xn::ImageGenerator mImageGenerator; eResult = mImageGenerator.Create( mContext ); |
接着需要声明几个后边要用到的变量:
Mat m_rgb8u; xn::ImageMetaData m_ImageMD; |
然后需要设定图像流的格式:
// 5. set map mode XnMapOutputMode mapMode; mapMode.nXRes = 640; mapMode.nYRes = 480; mapMode.nFPS = 30; eResult = mDepthGenerator.SetMapOutputMode( mapMode ); eResult = mImageGenerator.SetMapOutputMode( mapMode ); |
这里要注意, 设定这一步不是必须的。
接下来就是要获取图像了:
// 9b. get the image map // 这里要注意一下xn::ImageMetaData实验证明在多线程中有些问题 mImageGenerator.GetMetaData(m_ImageMD); // 用Mat的create方法动态创建, create方法的行为见下 m_rgb8u.create(m_ImageMD.YRes(),m_ImageMD.XRes(),CV_8UC3); memcpy(m_rgb8u.data,m_ImageMD.Data(),m_ImageMD.YRes()*m_ImageMD.XRes()*3); cvtColor(m_rgb8u,m_ImageShow,CV_RGB2BGR); imshow("image", m_ImageShow); |
文档中creaete的行为:
This is one of the key Mat methods. Most new-style OpenCV functions and methods that produce arrays call this method for each output array. The method uses the following algorithm: 1.If the current array shape and the type match the new ones, return immediately. Otherwise, de-reference the previous data by calling Mat::release(). 2.Initialize the new header. 3.Allocate the new data of total()*elemSize() bytes. 4.Allocate the new, associated with the data, reference counter and set it to 1.