Convertion Between cv::Mat and XnDepthPixel*

孤街浪徒 提交于 2019-12-07 17:49:28

I think there is a possible problem here:

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

If the depth image really is 16-bit, should it really be:

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

Because int is 32-bits, not 16.

Just as an addition, if you've built OpenCV with OpenNI support, you can retrieve the depth image as a cv::Mat which you can easily save with cv::imwrite Here's a minimal example:

#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
    }

}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!