Openni opencv kinect 错误的内存分配

Posted

技术标签:

【中文标题】Openni opencv kinect 错误的内存分配【英文标题】:Openni opencv kinect Bad Memory allocation 【发布时间】:2013-07-10 08:59:02 【问题描述】:

基本上我有一个循环遍历所有 kinects 深度像素。如果它们大于 3000 毫米,则将像素值设置为黑色。

由于某种原因,这只能在指向墙壁的近距离范围内有效。如果我将 kinect 拉回(给它更大的扫描区域),我会收到错误的内存分配错误。我的代码可以在下面找到。我在 try catch 语句中得到错误的内存分配错误。大部分代码来自 opencv kinect 示例 here 和 here。

我发现了问题,因为深度值存储在数组而不是矩阵中,我需要一种更好的方法来找出数组中的哪个位置,从 1,1 开始的像素的 x.y 指向而不是 (i = x+y*640)

#include <opencv.hpp>
#include <iostream>
#include <string>
#include <stdio.h>
#include <OpenNI.h>

using namespace std;
using namespace cv;



    int main()
       
        openni::Device device;
        openni::VideoStream depth;
        const char* device_uri = openni::ANY_DEVICE;
        openni::Status ret = openni::OpenNI::initialize();
        // Open
        ret =device.open( device_uri );
        ret = depth.create( device, openni::SENSOR_DEPTH );

        if ( ret == openni::STATUS_OK )
            
            // Start Depth
            depth.start();
            

        // Get Depth Stream Min-Max Value
    int minDepthValue = depth.getMinPixelValue();
    int maxDepthValue = depth.getMaxPixelValue();
    //cout << "Depth min-Max Value : " << minDepthValue << "-" << maxDepthValue << endl;

    // Frame Information Reference
    openni::VideoFrameRef depthFrame;

        // Get Sensor Resolution Information
    int dImgWidth = depth.getVideoMode().getResolutionX();
    int dImgHeight = depth.getVideoMode().getResolutionY();

    // Depth Image Matrix
    cv::Mat dImg = cv::Mat( dImgHeight, dImgWidth, CV_8UC3 );
    Mat grey= cvCreateImage(cvSize(640, 480), 8, 1); ;

    for(;;)
    
        depth.readFrame( &depthFrame );

        openni::DepthPixel* depthImgRaw = (openni::DepthPixel*)depthFrame.getData();

        for ( int i = 0 ; i < ( depthFrame.getDataSize() / sizeof( openni::DepthPixel ) ) ; i++ )
        
            int idx = i * 3; // Grayscale
            unsigned char* data = &dImg.data[idx];
            int gray_scale = ( ( depthImgRaw[i] * 255 ) / ( maxDepthValue - minDepthValue ) );
            data[0] = (unsigned char)~gray_scale;
            data[1] = (unsigned char)~gray_scale;
            data[2] = (unsigned char)~gray_scale;
        


        openni::DepthPixel* depthpixels = (openni::DepthPixel*)depthFrame.getData();

        cvtColor(dImg, grey, CV_RGB2GRAY);
        int i ;

        try
                for( int y =0; y < 480 ; y++)
                //getting in to each pixel in a row
                    for(int x = 0; x < 640; x++)
                    //getting out the corresponding pixel value from the array
                     i = x+y*640;


                     if (depthpixels[i] >3000)
                     
                         grey.at<unsigned char>(x,y) = 0;
                     
                  
             

        catch(exception e) 
            cout << e.what() <<endl ;
            cout <<depthpixels[i] <<endl ;          
            cout << i <<endl ;

            



    //  cv:imshow( "depth", dImg );
        imshow("dpeth2", grey);


        int k = cvWaitKey( 30 );        // About 30fps
        if ( k == 0x1b )
        break;
    
    // Destroy Streams
    depth.destroy();
    // Close Device
    device.close();
    // Shutdown OpenNI
    openni::OpenNI::shutdown();

            return 0;
    

【问题讨论】:

【参考方案1】:

只需交换我的 x 和 y 即可解决问题

for(  y =0; y < 480 ; y++)
                
                //getting in to each pixel in a row
                    for( x = 0; x < 640; x++)
                    



                         if (depthpixels[i]>1500)
                          
                             grey.at<unsigned char >(y,x) = 0;
                         

                         if (depthpixels[i] <500)
                         
                             grey.at<unsigned char >(y,x) = 0;
                         
                         i++;
                      
                 

【讨论】:

以上是关于Openni opencv kinect 错误的内存分配的主要内容,如果未能解决你的问题,请参考以下文章

ubuntu 14.04 +Kinect+ openNI+ openCV+ Sensorkinect 配置 +获取深度图像及相关问题

openni,opencv,opengl这三个到底是啥?有啥关系?做啥用?

安装了kinect和OpenNI 2,kinect可正常启动,但使用NiViewer,显示不出图像,画面是一片空白。求解!

通过 OpenNI 建立 Kinect 3D Point Cloud

kinect/xiton 的环境搭建

将 OpenCV 阈值与 Kinect 图像结合使用