linux - 使用 ubuntu 12.04 LTS 和 opencv 获取 kinect 深度图像的距离

标签 linux opencv ubuntu kinect robotics

我从一个网站发现,可以通过分配给特定像素的 2 个字节找到与 Kinect 的原始深度视频输出的距离,如本链接所示 - tutorial 。基于此,我编写了一段代码来找出 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;
    }

最佳答案

代码看起来没问题。将范围(150-1147)的图像缩放至(0-255)并以灰度显示。它将帮助您更好地理解图像。这样做会导致最近的物体为深色,最远的物体为浅色。这比使用 GlViewColor 函数更好。

关于linux - 使用 ubuntu 12.04 LTS 和 opencv 获取 kinect 深度图像的距离,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21765044/

相关文章:

python - 如何从文本文件中的特定行获取计时并用Python中的另一行减去它?

linux - 安装油煎面包 block 报错

linux - 替换文件中的未知行linux命令

linux - Matlab VideoReader 编解码器错误?

c++ - OpenCV/C++ 程序比它的 numpy 对应程序慢,我该怎么办?

Android OpenCV 查找最大的正方形或矩形

ios - Apple Mach-O 链接器警告直接访问...到全局弱符号

shell - 绑定(bind)字符串(文本)粘贴到键 ubuntu

wordpress - 如何在 bitnami wordpress ubuntu 实例上升级 apache

ubuntu - 如何在 Ubuntu -2021 上更新 VSCode