您的位置:首页 > 其它

在ROS 使用摄像头 WebCam 完成图像处理(1) -- 获取图像数据

2015-08-30 16:58 1111 查看

在ROS 使用摄像头 WebCam 完成图像处理(1) -- 获取图像数据

我们要使用摄像头,先要能看到摄像头图像:(见附录)

然后我们这里是参考《ros_by_example》( volume1 hydro )书中的方法。

这里要注意的是:虚拟机里的Ubuntu使用cheese, guvcview这样的工具是可以获取图像的,但ros里提供的摄像头驱动,比如uvc_camera, 就不能很好的工作了。

1. 环境搭建的准备工作

ROS组织发布的基础包

开始之前,安装Prerequisites:(是一些ROS组织发布的基础包)

-----

$ sudo apt-get install ros-hydro-turtlebot-* \

ros-hydro-openni-camera ros-hydro-openni-launch \

ros-hydro-uvc-camera \

ros-hydro-openni-tracker ros-hydro-laser-* \

ros-hydro-audio-common ros-hydro-joystick-drivers \

ros-hydro-orocos-kdl ros-hydro-python-orocos-kdl \

ros-hydro-dynamixel-motor-* ros-hydro-pocketsphinx \

gstreamer0.10-pocketsphinx python-setuptools python-rosinstall \

ros-hydro-opencv2 ros-hydro-vision-opencv \

ros-hydro-depthimage-to-laserscan ros-hydro-arbotix-* \

git subversion mercurial

-----

这里有点洒拦河网的感觉,其实可以只安装这几个:

$ sudo apt-get install ros-hydro-openni-camera \

ros-hydro-openni-launch \

ros-hydro-uvc-camera \

python-setuptools \

python-rosinstall \

ros-hydro-opencv2 \

ros-hydro-vision-opencv \

git subversion mercurial

1. Installing Webcam Drivers(OPTIONAL)

To use a typical USB webcam, there are a number of possible driver packages available.

The one we will use here is from Eric Perko's repository. Type the following commands

to move into your personal ROS directory, retrieve the source code, then make the

package:

$ sudo apt-get install git-core
  $ cd ~/catkin_ws/src
  $ git clone https://github.com/ericperko/uvc_cam.git   $ cd ~/catkin_ws
  $ catkin_make

Tips:如果你是ROS Electric, Fuerte, or Groovy, 自己看《ros_by_example》( volume1 hydro )书吧。

如果你是Hydro,要用Git 工具下载 rbx1 代码再生成ROS Packages就像former教程写的一样.

2. 用Git 工具下载 rbx1 代码,生成rbx ros Package

Commands:

$ cd ~/catkin_ws/src && git clone
--depth=1
https://github.com/pirobot/rbx1.git
$ cd rbx1 && git checkout hydro-devel

$ cd ~/catkin_ws && catkin_make

$ source ~/catkin_ws/devel/setup.bash

-----


git clone 太慢怎么办 ? 所以我这里使用了 --depth=l 进行浅复制

以上这几行命令,对于你应该很熟悉了,你是有基础的人。这里我们就用了'ROS By Example' 书提供的包了,All of the 'ROS By Example' packages begin with the letters rbx1。

===========

To list the packages,

move into the parent of the rbx1 meta-package and use the Linux ls command:

$ roscd rbx1
    $ cd ..
    $ ls -F

which should result in the following listing:

rbx1/ rbx1_bringup/ rbx1_dynamixels/ rbx1_nav/ rbx1_vision/

rbx1_apps/ rbx1_description/ rbx1_experimental/ rbx1_speech/ README.md

For example, to move into the rbx1_speech package, you would use the command:

$ roscd rbx1_speech

Note that you can run this command from any directory and ROS will find the package.

===========

2. 然后我们就测试一下rbx1_vision 下的uvc_can.launch文件:

$ ls /dev/video*

看看是有video几,假设是 /dev/video0 , linux就通过这个文件来管理你的WebCam。运行rbx1提供的节点看看:

$ roslaunch rbx1_vision uvc_cam.launch device:=/dev/video0

you should see a stream of diagnostic messages describing various camera settings.

Don't worry if some of these messages indicate that a parameter could not be set.

但是我在这里遇到一个问题:

ERROR: cannot launch node of type [uvc_cam/uvc_cam_node]: can't locate node [uvc_cam_node] in package [uvc_cam]

我查了一下,http://wiki.ros.org/uvc_cam 说,已经不支持了。所以 ROS_by_Example 在这里有过期了。。。。也就是说,rbx_vision 用了过期的api.

但 uvc_camera 这个Package是比较新的,所以我们使用 uvc_camera:

3. 安装测试 uvc_camera:

我们要把 ROS by Example 作为参考,独立使用 WebCam 及 ROS 提供的包来完成工作。
任务: 用ROS提供的驱动包,从摄像头获取图像,然后把图像数据发布在 sensor_msgs/Image 主题上。

从调研的情况看, uvc_cam is out of date. theusb_cam;uvc_camera;libuvc_camera
看起来都还可以用。

经过尝试,最后我们推荐使用
uvc_camera : (实际上,开篇的 sudo apt-get install ros-hydro-uvc-camera 就已经替代下面5行命令)

(读者可跳过下面5行命令)

$ cd ~/catkin_ws/src
  $ git clone https://github.com/ktossell/camera_umd   $ cd ~/catkin_ws
  $ catkin_make
  $ source ~/catkin_ws/devel/setup.bash

尝试运行:

一个窗口: $ roscore,

另一个窗口:

$ rosrun uvc_camera uvc_camera_node device:=/dev/video0
or: $ rosrun uvc_camera uvc_camera_node width:=640 height:=480 frame:=camera device:=/dev/video0

再开一个Terminal 窗口:
$ rostopic echo /image_raw

出来的数据应该就是图像数据了。 :) , 我们可以再开一个Terminal来查看图像:

$ rosrun image_view image_view image:=/image_raw




如果没有图像,是下面这样:(可能因为用了虚拟机,ros node对虚拟机兼容还不太好。)




至此,我们要让rbx1_vision动起来,还要做个主题映射把/image_raw -> /camera/rgb/image_color

还要修改 rbx1_vision/launch/uvc_cam.launch 文件中调用的 driver node : pkg=uvc_cam; node name=uvc_cam_node.

为:

name="uvc_camera_node" pkg="uvc_camera" type="uvc_camera_node"

结束前面正在运行的nodes, 新建一个 .launch 文件:

$ sudo gedit ~/catkin_ws/src/rbx1/rbx1_vision/launch/usb_cam.launch

内容:

 <launch>
    <arg name="device" default="/dev/video0" />

    <node name="uvc_camera_node" pkg="uvc_camera" type="uvc_camera_node" output="screen">
    	<remap from="image_raw" to="camera/rgb/image_color" />
        <param name="device" value="$(arg device)" />
        <param name="width" value="640" />
        <param name="height" value="480" />
        <param name="frame_rate" value="30" />
        <param name="exposure" value="0" />
        <param name="gain" value="100" /> 
    </node>
  </launch>


这里我们在 launch 文件里指定了运行 uvc_camera 包下的
uvc_camera_node 节点作为 WebCam 的驱动节点,并且把 image_raw 这个主题映射到 camera/rgb/image_color这个topic 下,这是习惯,更直观一点。试试运行这个launch 文件,并用 image_view 这个工具查看 camera/rgb/image_color 上的图像数据:

4. 最终测试:

试试运行这个launch 文件,并用 image_view 这个工具查看 camera/rgb/image_color 上的图像数据:

$ roslaunch rbx1_vision usb_cam.launch device:=/dev/video0

$ rosrun image_view image_view image:=/camera/rgb/image_color

我得到正常的图像数据,并在image_view工具里显示图像:




========================================

--------- 调试手记 ---------

我们要用ROS提供的驱动包,从摄像头获取图像,然后把图像数据发布在 sensor_msgs/Image 主题上。

从调研的情况看, uvc_cam is out of date. theusb_cam; uvc_camera; libuvc_camera 看起来都还可以用。不过 usb_cam 被维护的力度看起来最大,我们就来玩玩它先。

$ cd ~/catkin_ws/src
  $ git clone https://github.com/bosch-ros-pkg/usb_cam   $ cd ~/catkin_ws
  $ catkin_make

尝试运行:
一个窗口: $ roscore, 另一个窗口:

$ rosrun usb_cam usb_cam_node

但是我没看到 sensor_msgs/Image 这个主题, 不过这里的 /image_raw 应该就是图像数据。但我得到的全是零。

再试试 uvc_camera 这个Package,但是它的git 页面上说,libuvc_camera 更新,所以我们使用libuvc_camera, 在catkin_make这一步时出错。

最后还是使用 uvc_camera :

$ cd ~/catkin_ws/src
  $ git clone https://github.com/ktossell/camera_umd   $ cd ~/catkin_ws
  $ catkin_make

尝试运行:
一个窗口: $ roscore, 另一个窗口:

$ rosrun uvc_camera uvc_camera_node

再开一个Terminal 窗口:
$ rostopic echo /image_raw

出来的数据应该就是图像数据了。 :)

参考:ROS 教程之 vision
: 用各种摄像头获取图像

由于UVC图像采集卡出来的视频格式是MJPEG,这里,ROS支持的摄像头开发包有很多,这里有一个列表进行了罗列,还有这里有一篇文章进行了各个驱动的简单对比。在查看usb_cam
的wiki说明的时候,发现pixel_format 一栏默认的是mjpeg,大喜!

启用这个package:usb_cam.

$ cd ~/catkin_ws/src
  $ git clone https://github.com/bosch-ros-pkg/usb_cam   $ cd ~/catkin_ws
  $ catkin_make
$ source ~/catkin_ws/devel/setup.bash

尝试运行:
一个窗口: $ roscore, 另一个窗口:

$ rosrun usb_cam usb_cam_node

得到错误:[ERROR] [1440988422.600910989]: outbuf size mismatch. pic_size: 460800 bufsize: 614400

但是我没看到 sensor_msgs/Image 这个主题, 不过这里的 /image_raw 应该就是图像数据。但我得到的全是零。

$ rosrun image_view image_view image:=/image_raw

得到的是黑色窗口。

------------------------------------

参考:https://github.com/bosch-ros-pkg/usb_cam/issues/35

$ sudo gedit ~/catkin_ws/src/rbx1/rbx1_vision/launch/usb_cam.launch

<span style="color:#999999;">    <launch>  
        <arg name="device" default="/dev/video0" />  
        <node name="usb_cam_node" pkg="usb_cam" type="usb_cam_node" output="screen">  
        <remap from="usb_cam_node/image_raw" to="camera/rgb/image_color" />  
            <param name="video_device" value="$(arg device)" />  
            <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"/>
            <param name="framerate" value="30" />  
            <param name="brightness" value="128" />  
            <param name="contrast" value="128" />  
            <param name="saturation " value="70" />  
        </node>  
    </launch>  </span>

$ roslaunch rbx1_vision usb_cam.launch device:=/dev/video0

$ rosrun image_view image_view image:=/camera/rgb/image_color

结果,没有图像,是下面这样:




===================================================================================================

附录:虚拟机VirtualBox上运行 Ubuntu 14.04 LTS 使用WebCam摄像头

写在前面:

'我的宿主机是Win 7系统。虚拟机工具VirtualBox 4.3.28. 虚拟系统:Ubuntu 14.04 LTS
'VirtualBox上运行的Ubuntu系统,要使用摄像头,需打通VirtualBox对USB设备的支持,要安装对应的Oracle VM virtualBox Extension Pack(Google 搜索)
'关于Ubuntu/Linux系统中cheese工具支持的摄像头类型,有一个叫做UVC的组织,提供了一套Linux UVC driver支持大部分型号的WebCam

我的WebCam型号: Logitech HD Webcam C525

获取图像:

参考:ubuntu12.04+virtualbox+winxp的关于摄像头无法使用,声音出不来的问题

安装cheese以测试:

$ sudo apt-get install cheese

或者通过 ubuntu 图形界面 软件app中心 搜索 cheese 进行 Install。

测试运行cheese:

$ cheese

应该能看到图像了~~

如果没有,可以这样排查问题,第一步看摄像头采集卡有没有问题:在软件中心,查找 guvcview ,安装这个软件,然后在video&Files中device一栏选择为你的外部摄像头,看有没有图像。如果没有图像,说明你的视频采集卡有问题或者CCD摄像头有问题。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: