计算机视觉(ROS Viso2)

一、安装USB摄像头驱动

1. 安装usb_cam

使用apt-get命令安装usb_cam包:

sudo apt-get install ros-<rosdistro>-usb-cam

其中<rosdistro>替换为对应的ROS版本,例如noeticmelodic

2. 源码编译(可选)

如果需要从源码编译,可以按照以下步骤操作:

cd /path/to/colcon_ws/src
git clone https://github.com/ros-drivers/usb_cam.git
cd /path/to/colcon_ws
rosdep install --from-paths src --ignore-src -y
colcon build
source /path/to/colcon_ws/install/setup.bash

对于ROS 1,使用catkin_make代替colcon build

二、启动摄像头

1. 启动usb_cam节点

使用roslaunchrosrun命令启动usb_cam节点:

roslaunch usb_cam usb_cam-test.launch

或者使用参数文件启动:

ros2 run usb_cam usb_cam_node_exe --ros-args --params-file /path/to/usb_cam/config/params.yaml

对于ROS 1,使用rosrun命令:

rosrun usb_cam usb_cam_node

或者使用roslaunch文件。

2. 检查话题发布

使用rostopic listros2 topic list命令查看发布的图像话题:

rostopic list

图像数据一般会以/image_raw相关的名称发布。

ROS与OpenCV库

一、安装OpenCV

1. 安装OpenCV

使用apt-get命令安装OpenCV:

sudo apt-get install libopencv-dev

2. 安装Python绑定(可选)

安装Python绑定:

sudo apt-get install python3-opencv

二、使用OpenCV读取图像

import cv2
cap = cv2.VideoCapture(0)  # 使用第一个摄像头
while True:
    ret, frame = cap.read()
    if not ret:
        break
    cv2.imshow('frame', frame)
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break
cap.release()
cv2.destroyAllWindows()

使用ROS标定摄像头

1. 安装camera_calibration

使用apt-get命令安装camera_calibration包:

sudo apt-get install ros-<rosdistro>-camera-calibration

2. 启动标定程序

使用rosrun命令启动标定程序:

rosrun camera_calibration cameracalibrator.py --size 6x7 --square 0.108 image:=/usb_cam/image_raw camera:=/usb_cam

其中--size 6x7表示棋盘格的内部角点个数,--square 0.108表示棋盘格的边长。

3. 标定过程

标定过程中,需要将棋盘格放在摄像头视野的不同位置,包括左边、右边、上边和下边,确保标定板既有倾斜的,也有水平的。

视觉里程计

一、使用viso2

1. 安装viso2

使用apt-get命令安装viso2包:

sudo apt-get install ros-<rosdistro>-viso2

2. 启动viso2节点

使用roslaunch命令启动viso2节点:

roslaunch viso2_ros stereo_euroc.launch

或者根据具体需求修改launch文件。

二、使用libviso2

1. 下载和编译libviso2

下载libviso2源码并编译:

git clone https://github.com/ethz-asl/libviso2.git
cd libviso2
mkdir build
cd build
cmake ..
make
sudo make install

2. 使用libviso2进行视觉里程计

使用libviso2进行视觉里程计的示例代码:

#include <viso2/visual_odometry.h>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>

int main() {
    viso2::VisualOdometryMono vo;
    vo.set_calib(640, 480, 525.0, 525.0, 320.0, 240.0);

    cv::VideoCapture cap(0);
    cv::Mat frame;

    while (true) {
        cap >> frame;
        if (frame.empty()) break;

        viso2::Image img(frame.data, frame.cols, frame.rows, viso2::MONO);
        vo.process(img);

        if (vo.has_result()) {
            const viso2::Matrix& T = vo.get_result();
            std::cout << "Translation: " << T.t() << std::endl;
            std::cout << "Rotation: " << T.R() << std::endl;
        }

        cv::imshow("frame", frame);
        if (cv::waitKey(30) >= 0) break;
    }

    return 0;
}

视频讲解

BiliBili: 视睿网络-哔哩哔哩视频 (bilibili.com)