Kinect 输出以查找距离

Kinect 输出以查找距离

我从一个站点发现,可以通过分配给特定像素的 2 个字节来找到与 Kinect 的原始深度视频输出的距离,如下链接所示 -教程。基于此,我编写了一个代码来找出 Kinect 传感器中间点的距离。我编译了它并在 ubuntu 上运行代码,它显示了输出。输出显示了一些距离值。这些值在 150->1147 左右。我希望它显示的距离以毫米为单位。但我不确定它是对还是错。我提供了以下代码。请确保它是正确的,或者如果需要更改,请帮助我。谢谢大家!
代码:

    #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;
    }

相关内容