0
基本上我有一個循環遍歷所有的kinects深度像素。如果它們大於3000mm,則將像素值設置爲黑色。Openni opencv kinect壞的內存分配
由於某些原因,這隻適用於近距離同時指向牆壁。如果我拉回kinect(給它一個更大的區域掃描),我得到一個錯誤的內存分配錯誤。我的代碼可以在下面找到。該try try語句中出現錯誤的內存分配錯誤。大部分代碼來自opencv kinect示例here和here。
我想出了問題,它是因爲深度值存儲在一個數組而不是矩陣,我需要一個更好的方法來找出數組中的哪個位置,像素的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;
}