ROS2 摄像头校准

3790阅读 0评论2020-08-20 iibull
分类:其他平台

1. ROS1 用的是 USB_CAM, ROS2用的是 v4l2_camera

点击(此处)折叠或打开

  1. sudo apt install ros-rolling-v4l2-camera ros-rolling-vision-opencv ros-rolling-image-transport-plugins ros-rolling-image-transport ros-rolling-image-common
  2. sudo apt install python3-polled-camera polled-camera-tool libpolled-camera0d
  3. sudo apt install libtheora-dev libogg-dev libboost-python-dev
  4. sudo apt-get install -y libssl-dev libusb-1.0-0-dev pkg-config libgtk-3-dev
    sudo apt-get install -y libglfw3-dev libgl1-mesa-dev libglu1-mesa-dev
  5. 也可考虑源码安装, 这样可以保证更新
  6. git clone https://github.com/ros-perception/image_pipeline.git
  7. cd image_pipeline;
  8. git checkout ros2
  9. colcon build --symlink-install
  10. source 安装目录/setup.bash
  11. ## Wiki 参考  https://index.ros.org/r/v4l2_camera/#foxy 

  12. ros2 run v4l2_camera v4l2_camera_node --ros-args -p video_device:="/dev/video2" -p image_size=[1280,720]

  13. 出现
  14. [INFO] [1597918407.198064148] [v4l2_camera]: Starting camera
  15. [INFO] [1597918407.839159913] [v4l2_camera]: using default calibration URL
  16. [INFO] [1597918407.839354370] [v4l2_camera]: camera calibration URL: file:///home/iibull/.ros/camera_info/sunplusit_inc:_pc_camera.yaml
  17. [ERROR] [1597918407.839576699] [camera_calibration_parsers]: Unable to open camera calibration file [/home/iibull/.ros/camera_info/sunplusit_inc:_pc_camera.yaml]
  18. [WARN] [1597918407.839643757] [v4l2_camera]: Camera calibration file /home/iibull/.ros/camera_info/sunplusit_inc:_pc_camera.yaml not found

点击(此处)折叠或打开

  1. 发布话题
  2. iibull@BIG:/$ ros2 topic list
  3. /camera_info
  4. /image_raw
  5. /image_raw/compressed
  6. /image_raw/compressedDepth
  7. /image_raw/theora
  8. /parameter_events
  9. /rosout
/image_raw/compressed 是不能被  Rviz2 or showimage 看的, 所以 image_transport 的 republish 节点可以做此转换。 

ros2 run rqt_image_view rqt_image_view  使用 /image_raw 以及 /image_raw/compressed 话题可以看到图像.

点击(此处)折叠或打开

  1. ros2 run image_transport republish compressed --ros-args --remap in/compressed:=image_raw/compressed --ros-args --remap out:=image_raw/uncompressed
    • compressed - the transport to use for input, in this case 'compressed'. Alternative: raw, to republish the raw /image_raw topic
    • in/compressed:=image_raw/compressed - by default, republish uses the topics in and out, or in/compressed for example if the input transport is 'compressed'. This parameter is a ROS remapping rule to map those names to the actual topic to use.
    • raw - the transport to use for output. If omitted, all available transports are provided.
    • out:=image_raw/uncompressed - remapping of the output topic.
  2. 打开 rviz2. fixed frame 改成 camera, 然后添加 /image_raw/uncompressed 的topic 显示。 则可以看到图像了。

//现在进行校准。
参考  

点击(此处)折叠或打开

  1. 执行 ros2 run camera_calibration cameracalibrator
  2. 出现错误
  3. RuntimeError: FATAL: module compiled as little endian, but detected different endianness at runtime
  4. ...
  5. ...
  6. SystemError: initialization of cv_bridge_boost raised unreported exception
  7. cv_bridge是OpenCV与ROS之间的格式转换桥梁。 higer 版本编译器中应当移除 boost endian APIs, 目前官网github上有改好的,ubuntu仓库还没有更新,需要手动编译。 (害得我查了很久)

    解决方法: 参考 

  1. 下载地址:https://github.com/ros-perception/vision_opencv

  2. (这个仓库中包含多个包,下载之后只需要把cv_bridge拷贝到指定目录即可)

  3. OK, 那么在自己的工作空间内。 git clone https://github.com/ros-perception/vision_opencv.git
  4. cd vision_opencv/
  5. git checkout ros2
  6. colcon build --symlink-install

  7. sudo apt install python3-numpy
  8. pip3 install opencv-python
  9. source 自己的安装目录/install/setup.bash
  10. 再次执行 cameracalibration 就没有问题了。

ros2 run camera_calibration cameracalibrator --size 7x9 --square 0.02 image:=/image_raw camera:=/custom_camera 命令成功后就会出现一个摄像头窗口

点击(此处)折叠或打开

  1. 摄像头参数
  2. Camera Name:
  3. -c, --camera_name
  4.         name of the camera to appear in the calibration file ##校准文件中,摄像头的名字

  5. 棋盘参数
  6. Chessboard Options:
  7. You must specify one or more chessboards as pairs of --size and--square options.

  8.   -p PATTERN, --pattern=PATTERN
  9.                     calibration pattern to detect - 'chessboard','circles', 'acircles','charuco'
  10.   -s SIZE, --size=SIZE
  11.                     chessboard size as NxM, counting interior corners (e.g. a standard chessboard is 7x7)
  12.   -q SQUARE, --square=SQUARE
  13.                     chessboard square size in meters

  14. ROS 选项
  15. ROS Communication Options:
  16.  --approximate=APPROXIMATE
  17.                     allow specified slop (in seconds) when pairing images from unsynchronized stereo cameras
  18.  --no-service-check
  19.                     disable check for set_camera_info services at startup

  20. Calibration Optimizer Options:

  21.  --fix-principal-point
  22.                     fix the principal point at the image center
  23.  --fix-aspect-ratio
  24.                     enforce focal lengths (fx, fy) are equal
  25.  --zero-tangent-dist
  26.                     set tangential distortion coefficients (p1, p2) to
  27.                     zero
  28.  -k NUM_COEFFS, --k-coefficients=NUM_COEFFS
  29.                     number of radial distortion coefficients to use (up to
  30.                     6, default 2)
  31.  --disable_calib_cb_fast_check
  32.                     uses the CALIB_CB_FAST_CHECK flag for findChessboardCorners
去  生成自己的 校准图片打印, 7x9要做8行10列的图calib.pdf。直接放在屏幕上, 100%显示也是可以
前后左右上下移动, 同时各个方向上倾斜, 最终得到calibrate按钮可用并点击(1分钟计算时间), 然后点击保存, 获得  “/tmp/calibrationdata.tar.gz”
tar -xvf calibration.tar.gz

点击(此处)折叠或打开

  1. iibull@BIG:/tmp$ tar -xvf calibrationdata.tar.gz
  2. left-0000.png
  3. left-0001.png
  4. left-0002.png
  5. left-0003.png
  6. left-0004.png
  7. left-0005.png
  8. left-0006.png
  9. left-0007.png
  10. left-0008.png
  11. left-0009.png
  12. left-0010.png
  13. left-0011.png
  14. left-0012.png
  15. left-0013.png
  16. left-0014.png
  17. left-0015.png
  18. left-0016.png
  19. left-0017.png
  20. left-0018.png
  21. left-0019.png
  22. left-0020.png
  23. left-0021.png
  24. left-0022.png
  25. left-0023.png
  26. left-0024.png
  27. left-0025.png
  28. left-0026.png
  29. left-0027.png
  30. left-0028.png
  31. left-0029.png
  32. left-0030.png
  33. left-0031.png
  34. left-0032.png
  35. left-0033.png
  36. left-0034.png
  37. left-0035.png
  38. left-0036.png
  39. left-0037.png
  40. left-0038.png
  41. left-0039.png
  42. left-0040.png
  43. left-0041.png
  44. ost.yaml
  45. ost.txt
ost.yaml 就是我们最终想要的校准文件。

修改 ost.yaml 文件中 的内存  camera_name: sunplusit_inc:_pc_camera
cp ost.yaml /home/iibull/.ros/camera_info/sunplusit_inc:_pc_camera.yaml

然后重启 v4l2
ros2 run v4l2_camera v4l2_camera_node --ros-args -p video_device:="/dev/video2" -r image_raw:=/my_camera/image_raw -r camera_info:=/my_camera/camera_info -p camera_info_url:=file:///home/iibull/.ros/camera_info/mycamera.yaml
这里mycamera.yaml是我自己的配置文件。

通过 ros2 topic echo /my_camera/camera_info 看到输出的内容和 mycamera.yaml 内容是一样的。

点击(此处)折叠或打开

  1. 目前官方的代码有些问题, 不支持 camera_info_url的导入。 只能自己修改一下。
  2. git clone https://gitlab.com/boldhearts/ros2_v4l2_camera.git


  3. vi src/ros2_v4l2_camera/src/v4l2_camera.cpp
  4. 添加
  5. void V4L2Camera::createParameters()
  6. {
  7.   // Node parameters
  8.   output_encoding_ = declare_parameter("output_encoding", std::string{"rgb8"});
  9.   declare_parameter("camera_info_url");
  10.   ...
  11.   ...
  12.   ...


  13. colcon build --symlink-install
  14. source install/setup.bash

  15. //删除旧的
  16. sudo apt remove ros-rolling-v4l2-camera




上一篇:STM32 overrun 问题
下一篇:TurtleBot3 的 ROS2 运行框架梳理 1 - 底盘和键盘手动控制部分