Linux 使用Ubuntu12.04 LTS和opencv获取与kinect深度图像的距离
我从一个网站上发现,可以通过分配给特定像素的2个字节找到Kinect原始深度视频输出的距离,如此链接所示-。基于此,我编写了一个代码,从Kinect传感器中找出中点的距离 我编译了它并在Ubuntu上运行了代码,它显示了输出。输出显示一些值作为距离。数值约为150->1147。我希望它以毫米为单位显示距离 但我不确定这是对还是错。我提供下面的代码。我的代码是否正常工作,或者是否需要进行一些更改 代码:Linux 使用Ubuntu12.04 LTS和opencv获取与kinect深度图像的距离,linux,opencv,ubuntu,kinect,robotics,Linux,Opencv,Ubuntu,Kinect,Robotics,我从一个网站上发现,可以通过分配给特定像素的2个字节找到Kinect原始深度视频输出的距离,如此链接所示-。基于此,我编写了一个代码,从Kinect传感器中找出中点的距离 我编译了它并在Ubuntu上运行了代码,它显示了输出。输出显示一些值作为距离。数值约为150->1147。我希望它以毫米为单位显示距离 但我不确定这是对还是错。我提供下面的代码。我的代码是否正常工作,或者是否需要进行一些更改 代码: 代码似乎很好。缩放范围为150-1147到0-255的图像,并将其显示为灰度。这将帮助您更好地
代码似乎很好。缩放范围为150-1147到0-255的图像,并将其显示为灰度。这将帮助您更好地理解图像。这样做将导致最近的对象为深色,而最远的对象为浅色。这比使用GlViewColor函数要好
#include <opencv/cv.h>
#include <opencv/highgui.h>
#include <stdio.h>
#include "libfreenect_cv.h"
int getDist(IplImage *depth){
int x = depth->width/2;
int y = depth->height/2;
printf("width= %d and height %d \n",x,y);
int d = depth->imageData[x*2+y*640*2+1];
printf("1st value is %d \n",d);
d= d << 8;
d= d+depth->imageData[x*2+y*640*2];
return d;
}
IplImage *GlViewColor(IplImage *depth)
{
static IplImage *image = 0;
if (!image) image = cvCreateImage(cvSize(640,480), 8, 3);
unsigned char *depth_mid = (unsigned char*)(image->imageData);
int i;
for (i = 0; i < 640*480; i++) {
int lb = ((short *)depth->imageData)[i] % 256;
int ub = ((short *)depth->imageData)[i] / 256;
switch (ub) {
case 0:
depth_mid[3*i+2] = 255;
depth_mid[3*i+1] = 255-lb;
depth_mid[3*i+0] = 255-lb;
break;
case 1:
depth_mid[3*i+2] = 255;
depth_mid[3*i+1] = lb;
depth_mid[3*i+0] = 0;
break;
case 2:
depth_mid[3*i+2] = 255-lb;
depth_mid[3*i+1] = 255;
depth_mid[3*i+0] = 0;
break;
case 3:
depth_mid[3*i+2] = 0;
depth_mid[3*i+1] = 255;
depth_mid[3*i+0] = lb;
break;
case 4:
depth_mid[3*i+2] = 0;
depth_mid[3*i+1] = 255-lb;
depth_mid[3*i+0] = 255;
break;
case 5:
depth_mid[3*i+2] = 0;
depth_mid[3*i+1] = 0;
depth_mid[3*i+0] = 255-lb;
break;
default:
depth_mid[3*i+2] = 0;
depth_mid[3*i+1] = 0;
depth_mid[3*i+0] = 0;
break;
}
}
return image;
}
int main(int argc, char **argv)
{
while (cvWaitKey(100) != 27) {
IplImage *image = freenect_sync_get_rgb_cv(0);
if (!image) {
printf("Error: Kinect not connected?\n");
return -1;
}
cvCvtColor(image, image, CV_RGB2BGR);
IplImage *depth = freenect_sync_get_depth_cv(0);
if (!depth) {
printf("Error: Kinect not connected?\n");
return -1;
}
cvShowImage("RGB", image);
//int d = getDist(depth);
printf("value is %d \n",getDist(depth));
cvShowImage("Depth", GlViewColor(depth));//GlViewColor(depth)
}
cvDestroyWindow("RGB");
cvDestroyWindow("Depth");
//cvReleaseImage(image);
//cvReleaseImage(depth);
return 0;
}