基于libfreenect2 &ROS 下的iai_kinect2 实现kinectV2的图像获取和保存 【ubuntu18.04】

主要参考博客

使用iai_kinect2:

https://blog.csdn.net/sunbibei/article/details/51594824

在ubuntu18.04上安装ROS:

https://blog.csdn.net/qq_16481211/article/details/81137830

 

1.安装ROS

可以参照官网安装 (http://wiki.ros.org/melodic/Installation/Ubuntu)

在ubuntu18.04版本上只能安装 ROS Melodic

#Setup your sources.list
sudo sh -c 'echo "deb http://packages.ros.org/ros/ubuntu $(lsb_release -sc) main" > /etc/apt/sources.list.d/ros-latest.list'
#Setup your keys
sudo apt-key adv --keyserver hkp://ha.pool.sks-keyservers.net:80 --recv-key 421C365BD9FF1F717815A3895523BAEEB01FA116
#installation
sudo apt update
#recommand
sudo apt install ros-melodic-desktop-full     
#other options can see the websitehttp://wiki.ros.org/melodic/Installation/Ubuntu
#initialize rosdep
sudo rosdep init
rosdep update
#environment setup
echo "source /opt/ros/melodic/setup.bash" >> ~/.bashrc
source ~/.bashrc

庆幸装下来没有什么问题

2.安装libfreenect2

参考github上的安装说明,我删除了一些18.04用不到的语句,有需要的话可以直接去看原说明文档

  • Download libfreenect2 source
    git clone https://github.com/OpenKinect/libfreenect2.git
    cd libfreenect2
    
  • (Ubuntu 14.04 only) Download upgrade deb files
    cd depends; ./download_debs_trusty.sh
    
  • Install build tools
    sudo apt-get install build-essential cmake pkg-config
    
  • Install libusb. The version must be >= 1.0.20.
sudo apt-get install libusb-1.0-0-dev
  • Install TurboJPEG
 sudo apt-get install libturbojpeg0-dev
  • Install OpenGL
sudo apt-get install libglfw3-dev
  • Install OpenCL (optional)
    • Intel GPU
      1. (Other) sudo apt-get install beignet-dev
      2. For older kernels, # echo 0 >/sys/module/i915/parameters/enable_cmd_parser is needed. See more known issues at https://www.freedesktop.org/wiki/Software/Beignet/.
    • AMD GPU: Install the latest version of the AMD Catalyst drivers from https://support.amd.com and apt-get install opencl-headers.
    • Mali GPU (e.g. Odroid XU4): (with root) mkdir -p /etc/OpenCL/vendors; echo /usr/lib/arm-linux-gnueabihf/mali-egl/libmali.so >/etc/OpenCL/vendors/mali.icd; apt-get install opencl-headers.
    • Verify: You can install clinfo to verify if you have correctly set up the OpenCL stack.
  • Install CUDA (optional, Nvidia only):
    • (Nvidia/Intel dual GPUs) After apt-get install cuda, use sudo prime-select intel to use Intel GPU for desktop.
    • (Other) Follow Nvidia website's instructions. You must install the samples package.
  • Install VAAPI (optional, Intel only)
    1. sudo apt-get install libva-dev libjpeg-dev
  • Install OpenNI2 (optional)
    1.  sudo apt-get install libopenni2-dev
  • Build (if you have run cd depends previously, cd .. back to the libfreenect2 root directory first.)
    mkdir build && cd build
    cmake .. -DCMAKE_INSTALL_PREFIX=$HOME/freenect2
    make
    make install
    
    You need to specify cmake -Dfreenect2_DIR=$HOME/freenect2/lib/cmake/freenect2 for CMake based third-party application to find libfreenect2.
  • Set up udev rules for device access: sudo cp ../platform/linux/udev/90-kinect2.rules /etc/udev/rules.d/, then replug the Kinect.
  • Run the test program: ./bin/Protonect
  • Run OpenNI2 test (optional): sudo apt-get install openni2-utils && sudo make install-openni2 && NiViewer2. Environment variable LIBFREENECT2_PIPELINE can be set to clcuda, etc to specify the pipeline.

2.1检测libfreenect

进入libfreenect2/build/bin可以看到编译好的Protonect,连接好kinect2

在命令行输入   ./Protonnect

可以得到kinect的实时画面了

基于libfreenect2 &ROS 下的iai_kinect2 实现kinectV2的图像获取和保存 【ubuntu18.04】

3.安装iai_kinect2

参考github官网

cd ~/catkin_ws/src/
git clone https://github.com/code-iai/iai_kinect2.git
cd iai_kinect2
rosdep install -r --from-paths .
cd ~/catkin_ws
catkin_make -DCMAKE_BUILD_TYPE="Release"

这里注意一下,我安装的ROS虽然自动装了catkin但是没有构建自己的catkin workspace

3.1 构建自己的catkin_ws,参考网址

$ mkdir ~/catkin/-ws && cd catkin_ws
$ mkdir src && cd src
$ catkin_init_workspace
$ cd ~/catkin_ws/
$ catkin_make
$ source ~/catkin_ws/devel/setup.bash
$ echo "source ~/catkin_ws/devel/setup.bash" >> ~/.bashrc
$ echo $ROS_PACKAGE_PATH

然后就可以按照iai_kinect2的安装说明一步一步来了

第一次走到catkin_make DCMAKE_BUILD_TYPE="Release"这一步,我出现一个错误

找不到Eigen/Geometry,根据报错的提醒找到自己这个库的位置,去相应的源文件里修改一下路径就好

我这里是在iai_kinect2/kinect2_registration/src/depth_registration_cpu.h中第一个include加上eigen3就好了,修改后为

#include <eigen3/Eigen/Geometry>

补充一下查看自己的eigen安装位置

dpkg -L libeigen3-dev

会出现这个所有所在路径,自己找到需要的复制即可

3.2 检测iai_kinect2是否安装成功

#使用roslaunch发起Kinect2的相关节点(记得连接好kinect)
roslaunch kinect2_bridge kinect2_bridge.launch
#使用rostopic list查看该节点发布出来的topic 
rostopic list
#开启一个窗口查看kinect2获得的图像,后面的sd和cloud是自己选择的参数
rosrun kinect2_viewer kinect2_viewer sd cloud 
#点击显示出来的窗口,按下“S”或者空格键就可以保存你之前选择好的参数对应的图像了,
#默认保存在catkin_ws下

保存四张图

cloud.pcd

depth.png

color.jpg

depth_colored.png

命名从0000开始,同一窗口不断按空格就0001,0002依次保存下去,重新run的话在新窗口又是从0000开始保存

在控制窗口可以使用ctrl+c 关闭节点

在iai_kinect2/kinect2_viewer/src/下打开viewer.cpp可以去查看对应的源码来了解对应参数的意义

//...省略部分
for(size_t i = 1; i < (size_t)argc; ++i)
  {
    std::string param(argv[i]);

    if(param == "-h" || param == "--help" || param == "-?" || param == "--?")
    {
      help(argv[0]);
      ros::shutdown();
      return 0;
    }
    else if(param == "qhd")
    {
      topicColor = K2_TOPIC_QHD K2_TOPIC_IMAGE_COLOR K2_TOPIC_IMAGE_RECT;
      topicDepth = K2_TOPIC_QHD K2_TOPIC_IMAGE_DEPTH K2_TOPIC_IMAGE_RECT;
    }
    else if(param == "hd")
    {
      topicColor = K2_TOPIC_HD K2_TOPIC_IMAGE_COLOR K2_TOPIC_IMAGE_RECT;
      topicDepth = K2_TOPIC_HD K2_TOPIC_IMAGE_DEPTH K2_TOPIC_IMAGE_RECT;
    }
    else if(param == "ir")
    {
      topicColor = K2_TOPIC_SD K2_TOPIC_IMAGE_IR K2_TOPIC_IMAGE_RECT;
      topicDepth = K2_TOPIC_SD K2_TOPIC_IMAGE_DEPTH K2_TOPIC_IMAGE_RECT;
    }
    else if(param == "sd")
    {
      topicColor = K2_TOPIC_SD K2_TOPIC_IMAGE_COLOR K2_TOPIC_IMAGE_RECT;
      topicDepth = K2_TOPIC_SD K2_TOPIC_IMAGE_DEPTH K2_TOPIC_IMAGE_RECT;
    }
    else if(param == "approx")
    {
      useExact = false;
    }

    else if(param == "compressed")
    {
      useCompressed = true;
    }
    else if(param == "image")
    {
      mode = Receiver::IMAGE;
    }
    else if(param == "cloud")
    {
      mode = Receiver::CLOUD;
    }
    else if(param == "both")
    {
      mode = Receiver::BOTH;
    }
    else
    {
      ns = param;
    }
  }

4.按照参考的博客实现kinect数据的储存(待补充)