我有一个kinect将数据流传输到cv::Mat
。我正在尝试运行一些使用OpenNI的示例代码。
我可以通过某种方式将Mat转换为OpenNI格式的图像吗?
我只需要深度图像,并且在与OpenNI战斗了很长时间之后,就放弃了安装它。
我正在使用Windows的OpenCV 3,Visual Studio 2013,Kinect v2。
相关代码为:
void CDifodoCamera::loadFrame()
{
//Read the newest frame
openni::VideoFrameRef framed; //I assume I need to replace this with my Mat...
depth_ch.readFrame(&framed);
const int height = framed.getHeight();
const int width = framed.getWidth();
//Store the depth values
const openni::DepthPixel* pDepthRow = (const openni::DepthPixel*)framed.getData();
int rowSize = framed.getStrideInBytes() / sizeof(openni::DepthPixel);
for (int yc = height-1; yc >= 0; --yc)
{
const openni::DepthPixel* pDepth = pDepthRow;
for (int xc = width-1; xc >= 0; --xc, ++pDepth)
{
if (*pDepth < 4500.f)
depth_wf(yc,xc) = 0.001f*(*pDepth);
else
depth_wf(yc,xc) = 0.f;
}
pDepthRow += rowSize;
}
}
首先,您需要了解数据的传输方式...如果已经存在cv::Mat
,则应该接收两张图像,一张用于RGB信息,通常是3通道uchar cv::Mat
,另一张用于深度信息,通常将其保存在其中。以毫米为单位的16位表示形式(您不能将浮子垫另存为图像,但是可以使用opencv保存为yml / xml文件)。
假设您要读取和处理包含深度信息的图像,可以将代码更改为:
void CDifodoCamera::loadFrame()
{
//Read the newest frame
//the depth image should be png since it is the one which supports 16 bits and it must have the ANYDEPTH flag
cv::Mat depth_im = cv::imread("img_name.png",CV_LOAD_IMAGE_ANYDEPTH);
const int height = depth_im.rows;
const int width = depth_im.cols;
for (int y = 0; y < height; y++)
{
for (int x = 0; x < width; x++)
{
if (depth_im<unsigned short>(y,x) < 4500)
depth_wf(y,x) = 0.001f * (float)depth_im<unsigned short>(y,x);
else
depth_wf(y,x) = 0.f;
}
}
}
我希望这可以帮助你。如果您有任何问题,请问:)
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句