本人测试环境:ubuntu18.04。
这里有3中方法,方法1为ROS驱动版本,方法3为自己写的,推荐自己手写方式。
方法1:使用usb_cam驱动读取
1.1 安装编译usb_cam驱动
在终端运行以下命令:
cd ~/catkin_ws/src
git clone https://github.com/bosch-ros-pkg/usb_cam.git
cd ..
catkin_make
1.2 测试usb_cam驱动
- 打开摄像头。
source devel/setup.bash
rosrun usb_cam usb_cam_node
默认打开的是/dev/video0摄像头,如果想要打开自己的usb摄像头,可以改为/dev/video1(查看自己的usb接口对应名字)。运行以上程序并不能看到图像,因为只是打开摄像头。
- 运行显示图像
rosrun image_view image_view image:=/usb_cam/image_raw
1.3 使用launch打开
也可以使用launch文件对以上操作进行统一编写,如下:
<launch>
<node name="usb_cam" pkg="usb_cam" type="usb_cam_node" output="screen" >
<param name="video_device" value="/dev/video0" />
<param name="image_width" value="640" />
<param name="image_height" value="480" />
<param name="pixel_format" value="yuyv" />
<param name="camera_frame_id" value="usb_cam" />
<param name="io_method" value="mmap"/>
</node>
<node name="image_view" pkg="image_view" type="image_view" respawn="false" output="screen">
<remap from="image" to="/usb_cam/image_raw"/>
<param name="autosize" value="true" />
</node>
</launch>
同样,可以对相应参数进行修改,推荐使用这种方法。
方法2:video-stream-opencv
推荐使用方式1进行读取,方法2只是用来进行另一种选择。
2.1 安装video-stream-opencv
在终端运行以下命令进行安装:
cd ~/catkin_ws/src/
git clone https://github.com/ros-drivers/video_stream_opencv.git
cd ..
catkin_make
2.2 测试
source devel/setup.bash
rosrun video_stream_opencv test_video_resource.py 0 #后面的0是摄像头编号,也就是笔记本自带摄像头
方法3:自己手写
推荐这个方法,这种方式需要配置CMakeLists.txt文件,大致改动如下:
# 主要配置opencv
find_package(catkin REQUIRED COMPONENTS
cv_bridge
roscpp
sensor_msgs
std_msgs
)
find_package(OpenCV REQUIRED)
...
target_link_libraries(node_fisheye ${catkin_LIBRARIES} ${OpenCV_LIBRARIES})
#include "ros/ros.h"
#include <sensor_msgs/Image.h>
#include <sensor_msgs/image_encodings.h>
#include <cv_bridge/cv_bridge.h>
#include <iostream>
#include <sstream>
#include <vector>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/opencv.hpp>
using namespace std;
using namespace cv;
int main(int argc, char **argv)
{
ros::init(argc, argv, "node_fisheye");
ros::NodeHandle n;
ros::Publisher image_pub_ = n.advertise<sensor_msgs::Image>("fisheye", 1000);
VideoCapture camera(0);//设备驱动号
if (!camera.isOpened())
{
printf("Failed to open camera!");
return -1;
}
camera.set(CV_CAP_PROP_FRAME_WIDTH, 1024);
camera.set(CV_CAP_PROP_FRAME_HEIGHT, 768 );
camera.set(CV_CAP_PROP_FPS, 30);
camera.set(CV_CAP_PROP_EXPOSURE, 1.0);
camera.set(CV_CAP_PROP_FOURCC, CV_FOURCC('M','J','P','G'));
auto fps = camera.get(CAP_PROP_FPS);
//vector<int> compression_params;
//compression_params.push_back(CV_IMWRITE_PNG_COMPRESSION);
//compression_params.push_back(0);
ros::Rate loop_rate(1000);
while (ros::ok())
{
Mat frame;
camera >> frame;
cv_bridge::CvImage img_bridge;
sensor_msgs::Image img_msg; // >> message to be sent
std_msgs::Header header; // empty header
header.stamp = ros::Time::now(); // time
img_bridge = cv_bridge::CvImage(header, sensor_msgs::image_encodings::BGR8, frame);
img_bridge.toImageMsg(img_msg);
image_pub_.publish(img_msg);
/*
sensor_msgs::ImagePtr msg = cv_bridge::CvImage(std_msgs::Header(), "bgr8", frame).toImageMsg();
// 发布图像
image_pub_.publish(*msg);*/
imshow("frame", frame);
if (waitKey(30) > 0)
{
cout << "frame size: " << frame.rows << " x " <<frame.cols << endl;
camera.release();
frame.release();
break;
}
//imu_pub.publish(imu_msg); //开始发布
ros::spinOnce();
loop_rate.sleep();
}
return 0;
}