2013-07-10 32 views
0

基本上我有一个循环遍历所有的kinects深度像素。如果它们大于3000mm,则将像素值设置为黑色。Openni opencv kinect坏的内存分配

由于某些原因,这只适用于近距离同时指向墙壁。如果我拉回kinect(给它一个更大的区域扫描),我得到一个错误的内存分配错误。我的代码可以在下面找到。该try try语句中出现错误的内存分配错误。大部分代码来自opencv kinect示例herehere

我想出了问题,它是因为深度值存储在一个数组而不是矩阵,我需要一个更好的方法来找出数组中的哪个位置,像素的xy从1,1点开始到的,而不是第(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; 
    } 

回答

0

简单地通过交换我的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++; 
         } 
       } 
解决了这个问题