cv :: Mat和深度像素之间的转换*

时间:2013-05-10 09:26:31

标签: c++ qt opencv image-processing openni

我正在使用OpenNI 1.5.4.0和OpenCV 2.4.5,以及用于可视化目的的Qt(仅限RGB图像)。 基本上,我正在从Kinect中检索深度和rgb帧并使用转换将它们存储在硬盘上:

/* Depth conversion */

cv::Mat depth = cv::Mat(2, sizes, CV_16UC1, (void*) pDepthMap); //XnDepthPixel *pDepthMap 

/* RGB conversion */

///image is a QImage* , pImageMap is a XnUInt8*

for(int i = 0; i < height; ++i)
{
    for (unsigned y = 0; y < height; ++y)
    {
       uchar *imagePtr = (*image).scanLine(y);
       for (unsigned x=0; x < width; ++x)
       {
          imagePtr[0] = pImageMap[2];
          imagePtr[1] = pImageMap[1];
          imagePtr[2] = pImageMap[0];
          imagePtr[3] = 0xff;
          imagePtr+=4;
          pImageMap+=3;
       }
    }
}

现在,我想从硬盘驱动器加载这些图像,以便将3D点云计算为后处理计算。 我正在加载深度贴图:

depth_image = cv::imread(m_rgb_files.at(w).toStdString(), CV_LOAD_IMAGE_ANYDEPTH | CV_LOAD_IMAGE_ANYCOLOR );

但应用公式:

depth = depth_image.at<int>(j,i);  //depth_image is stored as 16bit
p.z = (float)depth * 0.001f;    //converting from millimeters to meters
p.x = (float)(i - m_params.center_x) * depth * m_params.focal_length; //focal_length read using OpenNI function
p.y = (float)(j - m_params.center_y) * depth * m_params.focal_length;
获得的pointcloud是一团糟。

如果我进行“在线”处理,直接使用原生XnDepthPixel *数据,结果是完美的,使用之前编写的公式。 任何人都能给我一个关于我的错误的“暗示”吗?

提前致谢

编辑:我也跟着这个resource,但它对我不起作用,因为XnDepthPixel包含以毫米为单位的真实世界数据

2 个答案:

答案 0 :(得分:0)

我认为这里可能存在问题:

depth = depth_image.at<int>(j,i);  //depth_image is stored as 16bit

如果深度图像确实 16位,那么它应该是:

depth = depth_image.at<short>(j,i);  //depth_image is stored as 16bit

因为int是32位,而不是16位。

答案 1 :(得分:0)

另外,如果您已经构建了OpenCV并支持OpenNI,则可以使用cv::Mat检索深度图像,您可以使用cv::imwrite轻松保存该图像 这是一个最小的例子:

#include "opencv2/core/core.hpp"
#include "opencv2/highgui/highgui.hpp"

#include <iostream>

using namespace cv;
using namespace std;

int main(){
    VideoCapture capture;
    capture.open(CV_CAP_OPENNI);

    if( !capture.isOpened() ){
        cout << "Can not open a capture object." << endl;
        return -1;
    }
    cout << "ready" << endl;

    for(;;){
        Mat depthMap,depthShow;
        if( !capture.grab() ){
            cout << "Can not grab images." << endl;
            return -1;
        }else{
            if( capture.retrieve( depthMap, CV_CAP_OPENNI_DEPTH_MAP ) ){
                const float scaleFactor = 0.05f;
                depthMap.convertTo( depthShow, CV_8UC1, scaleFactor );
                imshow("depth",depthShow);
                if( waitKey( 30 ) == 115)    {//s to save
                    imwrite("your_depth_naming_convention_here.png",depthShow);
                }
            }
        }
        if( waitKey( 30 ) == 27 )    break;//esc to exit
    }

}
相关问题