Question

Basically I've got a loop which goes through all the kinects depth pixels. If they are greater than 3000mm it sets the pixel value to black.

For some reason this works only at a close range while pointed to a wall. If I pull the kinect back (giving it a larger area to scan) I get a Bad Memory allocation error. My code can be found below. I get the bad memory allocation error inside that try catch statement. Most of the code is from the opencv kinect sample here and here.

i figured out the problem, its because the depth values are stored in an array instead of matrix, i need a better way of finding out which location in the array, the x.y of the pixels which start from 1,1 point to instead of the (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;
    }
Was it helpful?

Solution

solved the problem simply by swapping my x and y around

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++;
                      }
                 }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top