Ask Your Question
0

Opencv imshow can not show depth image from Kinect V2

asked 2016-11-24 01:35:56 -0500

ebrahimmasoomi gravatar image

updated 2016-11-24 03:14:41 -0500

LBerger gravatar image

Hi:):) I I wrote a c++ code to open Kinect and show Depth mage(after installing kinect sdk 2.0) .my code is:

    // emmb.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <Kinect.h>
#include <iostream>
#include <opencv2\core.hpp>
#include <opencv2\video.hpp>
#include <opencv2\highgui.hpp>
typedef unsigned short unit16;
using namespace cv;
using namespace std;
template <class T> void SafeRelease(T **ppT)
{
    if (*ppT)
    {
        (*ppT)->Release();
        *ppT = NULL;
    }
};

int _tmain(int argc, _TCHAR* argv[])
{
    IKinectSensor* pSensor;
    HRESULT hResult = S_OK;
    hResult = GetDefaultKinectSensor(&pSensor);
    if (FAILED(hResult)){
        std::cout << "Error : GetDefaultKinectSensor" << std::endl;
        return 11;
    }
    hResult = pSensor->Open();
    if (FAILED(hResult)){
        std::cout << "Error : IKinectSensor::Open()" << std::endl;
        return 22;
    }
    IDepthFrameSource* pSource;
    hResult = pSensor->get_DepthFrameSource(&pSource);
    if (FAILED(hResult)){
        std::cout << "Error : IKinectSensor::get_FrameSource()" << std::endl;
        return 33;
    }
    IDepthFrameReader* pDepthReader;
    hResult = pSource->OpenReader(&pDepthReader);
    if (FAILED(hResult)){
        std::cout << "Error : IFrameSource::OpenReader()" << std::endl;
        return -1;
    }
    int l = 0;
    int Width = 512;   // ...... 1
    int Height = 424;
    cv::Mat BufferMat(Height, Width, CV_16UC1);   // ...... 3
    cv::Mat DepthMat(Height, Width, CV_8UC1);   // ...... 3
//  cv::namedWindow("ebi");

    while (l<1){
        // Frame

        IDepthFrame* pDepthFrame = nullptr;
        hResult = pDepthReader->AcquireLatestFrame(&pDepthFrame);
        if (SUCCEEDED(hResult)){
            unsigned int bufferSize = 512 * 424 * sizeof(unsigned short);
            unsigned short* pBuffer = nullptr;
            hResult = pDepthFrame->AccessUnderlyingBuffer(&bufferSize, reinterpret_cast<UINT16**>(&BufferMat.data));
            if (SUCCEEDED(hResult)){
                /* Processing*/
                BufferMat.convertTo(DepthMat, CV_8U, -255.0f / 8000.0f, 255.0f);  
            }

        }


        cv::imshow("kdkk",DepthMat);
        if (cv::waitKey(30) == VK_ESCAPE){
            break;
        }
        SafeRelease(&pDepthFrame);



        l++;
    }
    cin.get();
    return 0;

}

But this warning is showed:

Unhandled exception at 0x60527648 (opencv_highgui2413d.dll) in emmb.exe: 0xC0000005: Access violation reading location 0x2F29BB00.**strong text**

and when I clicked on continue again this message is showed... how can I solve it? sorry for my English...:(

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
0

answered 2016-11-24 04:25:14 -0500

kbarni gravatar image

Didn't test your code, but you are probably using wrong the AccessUnderlyingBuffer. You shouldn't access directly the BufferMat.data. Try instead:

unsigned short *pBuffer;
pDepthFrame->AccessUnderlyingBuffer(&nBufferSize, &pBuffer);
DepthMat.create(width,height,pBuffer,CV_16U);
edit flag offensive delete link more

Comments

But in DepthMat there is not create!!!!!!!!

ebrahimmasoomi gravatar imageebrahimmasoomi ( 2016-11-24 06:10:27 -0500 )edit

This will create depthMat using the data in pBuffer. That isn't a problem, as the memory won't be allocated again.

On the other hand, it's a bad idea to use depthMat.data: it might be reallocated in the AccessUnderlyingBuffer function, and it might even be a non-continuous memory region.

kbarni gravatar imagekbarni ( 2016-11-24 06:35:58 -0500 )edit

I change my code:

unsigned int bufferSize = 512 * 424 * sizeof(unsigned short);
        unsigned short* pBuffer ;
        hResult = pDepthFrame->AccessUnderlyingBuffer(&bufferSize, &pBuffer);
        if (SUCCEEDED(hResult)){

            DepthMat.create(Width, Height, CV_16U);
            BufferMat.copyTo(DepthMat);
        }

But this error occurred :

OpenCV Error: Null pointer <NULL name>  in cvShowImage...
ebrahimmasoomi gravatar imageebrahimmasoomi ( 2016-11-24 10:17:34 -0500 )edit

use DepthMat.create(Width, Height, pBuffer, CV_16U); (you forgot the pBuffer parameter), and DepthMat.convertTo(BufferMat,CV_8U,-255.0f / 8000.0f, 255.0f); (and not BufferMat.copyTo...).

kbarni gravatar imagekbarni ( 2016-11-25 03:34:34 -0500 )edit

The result did not change.....

ebrahimmasoomi gravatar imageebrahimmasoomi ( 2016-11-25 04:31:00 -0500 )edit

try to debug your application and check if DepthMat and BufferMat are correctly created (check at least the type and size) and if buffersize is correct and pBuffer contains correct data (values between 0-4000).

kbarni gravatar imagekbarni ( 2016-11-25 05:59:34 -0500 )edit
Login/Signup to Answer

Question Tools

1 follower

Stats

Asked: 2016-11-24 01:35:56 -0500

Seen: 536 times

Last updated: Nov 24 '16