使用OpenNI和OpenCV显示颜色和深度

时间:2016-09-09 11:53:26

标签: c++ opencv openni depth-testing

最近我发布了很多关于使用OpenNI和OpenCV从Kinect相机访问深度图像的文章。

根据其他一些帖子的一些教程和建议,我已经能够编写这个脚本来显示摄像机的2D颜色流(如果第二部分被注释掉),以及深度流,它还没有工作:

#include <opencv2/opencv.hpp>
#include <opencv2\highgui\highgui.hpp>
#include <opencv2\imgproc\imgproc.hpp>
#include <OpenNI.h>

int main()
{
    openni::Device device;

    openni::VideoStream  color;
    openni::VideoStream depth;

    openni::VideoFrameRef depthFrame;
    openni::VideoFrameRef colorFrame;

    openni::Status rc = openni::STATUS_OK;

    rc = openni::OpenNI::initialize();
    rc = device.open(openni::ANY_DEVICE);

    rc = color.create(device, openni::SENSOR_COLOR);
    rc = color.start();
    rc = depth.create(device, openni::SENSOR_DEPTH);
    rc = depth.start();

    cv::Mat framecolor;
    cv::Mat framedepth;

    while (true)
    {
        color.readFrame(&colorFrame);
        const openni::RGB888Pixel* imageBuffer = (const openni::RGB888Pixel*)colorFrame.getData();

        framecolor.create(colorFrame.getHeight(), colorFrame.getWidth(), CV_8UC3);
        memcpy(framecolor.data, imageBuffer, 3 * colorFrame.getHeight()*colorFrame.getWidth() * sizeof(uint8_t));

        cv::cvtColor(framecolor, framecolor, CV_BGR2RGB); //this will put colors right
        cv::imshow("framecolor", framecolor);
        ////////////////////////////Second Part/////////////////////////////////////
        depthFrame.getVideoMode();
        const openni::DepthPixel* imageBuffer2 = (const openni::DepthPixel*)depthFrame.getData();

        framedepth.create(depthFrame.getHeight(), depthFrame.getWidth(), CV_16U);
        memcpy(framecolor.data, imageBuffer2, 3 * depthFrame.getHeight()*depthFrame.getWidth() * sizeof(uint16_t));
        framedepth.convertTo(framedepth, CV_8U);
        cv::imshow("framedepth", framedepth);

        if (cvWaitKey(30)>=0)
        {
            break;
        }
    }
    cv::destroyAllWindows();
    return 0;
}

我得到的错误将我带到openNI.g文件,其中包含以下内容:

enter image description here

我猜它与文件类型或像素格式有关。然而,有很多选择,我甚至不确定这种方法甚至可以工作。

任何人都可以帮助处理所需的文件类型或程序吗?

1 个答案:

答案 0 :(得分:0)

代码中有错误

framedepth.create(depthFrame.getHeight(), depthFrame.getWidth(), CV_16U);
memcpy(**framecolor.data**, imageBuffer2, 3 * depthFrame.getHeight()*depthFrame.getWidth() * sizeof(uint16_t));
framedepth.convertTo(framedepth, CV_8U)

应该是

framedepth.create(depthFrame.getHeight(), depthFrame.getWidth(), CV_16U);
memcpy(**framedepth.data**, imageBuffer2, 3 * depthFrame.getHeight()*depthFrame.getWidth() * sizeof(uint16_t));
framedepth.convertTo(framedepth, CV_8U)