docker_ROS的usb_cam使用与标定

目录

准备

准备标定板

新建容器

新建usb_cam话题的ROS功能包

编写代码

编译

运行功能包

标定

安装camera_calibration标定功能包

启动发布usb_cam话题的功能包

启动camera_calibration标定功能包


准备

usb相机

标定板

一个带有ROS的docker镜像。

准备标定板

图片链接:

棋盘格图片

打印粘贴。或者可以使用手机平板等屏幕展示这张棋盘照片,测量棋盘小格尺寸,并保证标定过程中,图片不被缩放。

新建容器

使用--privileged参数建立镜像。基础的代码:

sudo docker run -it --privileged --name=lab1 your_image_name  /bin/bash

修改 your_image_name为你的docker镜像名。--privileged参数使容器与宿主机共用device设备,有相同的/dev文件。但是,在进入容器之前,先连接相机,再进入容器,防止进入容器后刷新不出相机。

docker run的其他参数根据需要自行设置。

连接相机

进入容器

新建usb_cam话题的ROS功能包

新建ROS功能包,用来发布usb_cam的话题。

编写代码

使用C++语言编写,示例如下,

新建功能包,而后写入核心代码:

img_publisher.cpp文件


#include 
#include 
#include 
#include 
#include 
#include 

int main(int argc, char** argv) {
    ros::init(argc, argv, "img_publisher");
    ros::NodeHandle nh;
    image_transport::ImageTransport it(nh);
    image_transport::Publisher pub = it.advertise("camera/image", 1);

    cv::VideoCapture cap;
    cv::Mat frame;
    int deviceID = 0;
    if (argc > 1)
        deviceID = argv[1][0] - '0';
    int apiID = cv::CAP_ANY;
    cap.open(deviceID + apiID);
    if (!cap.isOpened()) {
        std::cerr << "ERROR! Unable to open camera" << std::endl;
        return -1;
    }

    ros::Rate loop_rate(30);
    while (nh.ok()) {
        cap.read(frame);
        if (!frame.empty()) {
            sensor_msgs::ImagePtr msg = cv_bridge::CvImage(std_msgs::Header(), "bgr8", frame).toImageMsg();
            pub.publish(msg);
        }
        ros::spinOnce();
        loop_rate.sleep();
    }
    return 0;
}

CMakeLists.txt文件

cmake_minimum_required(VERSION 3.0.2)
project(usb_cam)

## Compile as C++11, supported in ROS Kinetic and newer
# add_compile_options(-std=c++11)

## Find catkin macros and libraries
## if COMPONENTS list like find_package(catkin REQUIRED COMPONENTS xyz)
## is used, also find other catkin packages
find_package(catkin REQUIRED COMPONENTS
  cv_bridge
  image_transport
  roscpp
  sensor_msgs
  std_msgs
)
find_package(OpenCV REQUIRED)
message(${OpenCV_VERSION})


###################################
## catkin specific configuration ##
###################################
## The catkin_package macro generates cmake config files for your package
## Declare things to be passed to dependent projects
## INCLUDE_DIRS: uncomment this if your package contains header files
## LIBRARIES: libraries you create in this project that dependent projects also need
## CATKIN_DEPENDS: catkin_packages dependent projects also need
## DEPENDS: system dependencies of this project that dependent projects also need
catkin_package(
#  INCLUDE_DIRS include
#  LIBRARIES usb_cam
#  CATKIN_DEPENDS cv_bridge image_transport roscpp sensor_msgs std_msgs
#  DEPENDS system_lib
)

###########
## Build ##
###########

## Specify additional locations of header files
## Your package locations should be listed before other locations
include_directories(
# include
  ${catkin_INCLUDE_DIRS}
  ${OpenCV_INCLUDE_DIRS}
)


add_executable(img_publisher src/img_publisher.cpp)
add_executable(img_viewer src/img_viewer.cpp)


target_link_libraries(img_publisher ${catkin_LIBRARIES} ${OpenCV_LIBS})
target_link_libraries(img_viewer ${catkin_LIBRARIES} ${OpenCV_LIBS})

编译

打开终端:

catkin_make

在~/.bashrc加入环境变量。

打开终端:

gedit ~/.bashrc

文末写入

source ~/catkin_ws/devel/setup.bash

保存文件,关闭。

运行功能包

打开终端:

rosrun usb_cam img_publisher

会发布一个话题

查看话题

rostopic list

正常接入相机,功能包才正常运行,此时会出现包括/camera/image话题等一系列话题。

标定

安装camera_calibration标定功能包

查看ROS的功能包:

rospack list

查看是否有“camera_calibration”功能包,没有则需要安装:

rosdep install camera_calibration

启动发布usb_cam话题的功能包

打开终端:

rosrun usb_cam img_publisher

查看话题 

rostopic list

正常接入相机,功能包才正常运行,此时会出现包括/camera/image话题等一系列话题。 

启动camera_calibration标定功能包

打开终端:

rosrun camera_calibration cameracalibrator.py --size 8x6 --square 0.20 image:=/camera/image

其中, --size 8x6是棋盘交点的数量,9*7的格子就会有8*6个交点

--square 0.20是一个小棋盘格大小,单位:米。

image:=/camera/image,image:=相机话题名。

正常启动后,弹出标定窗口,间断地调整相机位置,直到右侧"calibrate"按钮亮起,

Moving the Checkerboard

In order to get a good calibration you will need to move the checkerboard around in the camera frame such that:

    checkerboard on the camera's left, right, top and bottom of field of view
        X bar - left/right in field of view
        Y bar - top/bottom in field of view
        Size bar - toward/away and tilt from the camera
    checkerboard filling the whole field of view
    checkerboard tilted to the left, right, top and bottom (Skew)

At each step, hold the checkerboard still until the image is highlighted in the calibration window.

移动棋盘
为了获得良好的校准,您需要在相机框架中移动棋盘,以便:
相机视野左、右、上、下的棋盘
X条-视野中的左/右
Y条-视野中的顶部/底部
尺寸条-朝向/远离相机并倾斜
棋盘填充整个视野
向左、右、上、下倾斜的棋盘(倾斜)
在每一步中,保持棋盘静止,直到图像在校准窗口中高亮显示。

点击,开始计算。计算结束后"save"按钮亮起,结果点"save"按钮保存

If you are satisfied with the calibration, click COMMIT to send the calibration parameters to the camera for permanent storage. The GUI exits and you should see "writing calibration data to ..." in the console.

如果您对校准感到满意,请单击COMMIT将校准参数发送到相机以进行永久存储。GUI退出,您应该在控制台中看到“正在将校准数据写入…”。

参考

标定过程及参数:

相机内参标定究竟标了什么?相机内参外参保姆级教程 - 知乎

camera_calibration标定功能包介绍:

camera_calibration/Tutorials/MonocularCalibration - ROS Wiki

你可能感兴趣的:(docker,人工智能,c++,计算机视觉)