2

我最近发布了很多关于使用 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 文件,其中包含以下内容:

在此处输入图像描述

我猜它与文件类型或像素格式有关。但是有很多选择,我什至不确定这种方法是否会奏效。

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

4

1 回答 1

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)
于 2018-05-25T04:39:20.513 回答