一开始不懂ROS,但是使用深度相机时产生了.bag需要用到ROS解析提取图像,没办法只能自己学一下了!
一、ROS 安装
电脑环境Ubuntu 16.04,对应的ROS版本应该是kinetic
ROS的安装总会遇到依赖问题,其实就是源的问题,当然若果按照官网可以直接安装的话可以忽略我的安装过程,问题类似于以下
Some packages could not be installed. This may mean that you have requested an impossible situation or if you are using the unstable distribution that some required packages have not yet been created or been moved out of Incoming. The following information may help to resolve the situation:
The following packages have unmet dependencies: ros-kinetic-desktop-full :
Depends: ros-kinetic-desktop but it is not going to be installed
Depends: ros-kinetic-perception but it is not going to be installed Depends: ros-kinetic-simulators but it is not going to be installed
Depends: ros-kinetic-urdf-tutorial but it is not going to be installed
E: Unable to correct problems, you have held broken packages.
删除ros-latest.list
sudo rm -rf /etc/apt/sources.list.d/ros-latest.list
修改源
sudo vi /etc/apt/sources.list
deb http://mirrors.ustc.edu.cn/ubuntu/ xenial main restricted universe multiverse
deb http://mirrors.ustc.edu.cn/ubuntu/ xenial-security main restricted universe multiverse
deb http://mirrors.ustc.edu.cn/ubuntu/ xenial-updates main restricted universe multiverse
deb http://mirrors.ustc.edu.cn/ubuntu/ xenial-proposed main restricted universe multiverse
deb http://mirrors.ustc.edu.cn/ubuntu/ xenial-backports main restricted universe multiverse
deb-src http://mirrors.ustc.edu.cn/ubuntu/ xenial main restricted universe multiverse
deb-src http://mirrors.ustc.edu.cn/ubuntu/ xenial-security main restricted universe multiverse
deb-src http://mirrors.ustc.edu.cn/ubuntu/ xenial-updates main restricted universe multiverse
deb-src http://mirrors.ustc.edu.cn/ubuntu/ xenial-proposed main restricted universe multiverse
deb-src http://mirrors.ustc.edu.cn/ubuntu/ xenial-backports main restricted univer
sudo sh -c 'echo "deb http://packages.ros.org/ros/ubuntu $(lsb_release -sc) main" > /etc/apt/sources.list.d/ros-latest.list'
sudo apt-key adv --keyserver hkp://ha.pool.sks-keyservers.net:80 --recv-key 0xB01FA116
sudo apt-get update
sudo apt-get upgrade
sudo apt-get install ros-kinetic-desktop-full
初始化
sudo rosdep init
rosdep update
添加环境变量
echo "source /opt/ros/kinetic/setup.bash" >> ~/.bashrc
source ~/.bashrc
二、创建工作空间
打开终端
创建ROS工作空间
mkdir -p ~/catkin_ws/src
cd ~/catkin_ws/src
catkin_init_workspace
cd ~/catkin_ws/
catkin_make
source devel/setup.bash
echo $ROS_PACKAGE_PATH /home/youruser/catkin_ws/src:/opt/ros/kinetic/share:/opt/ros/kinetic/stacks
youruser是用户目录
三、构建catkin包(用于发布ROS消息)
cd ~/catkin_ws/src
catkin_create_kpg run_py(我自己的包名称,你可以随便写你自己的) std_msgs rospy roscpp(后面三个都是依赖项)
四、catkin包完善
定义msg消息
1.在run_py包中新建msg消息目录,新建Num.msg文件
roscd run_py
mkdir msg
cd msg
touch Num.msg
rosed run_py Num.msg
2.Num.msg文件,手工输入代码:
int64 num (测试用)
3.打开文件rosed run_py package.xml,增加依赖项
上面两行的位置就放在文件最后几行对应的地方
4.打开文件rosed run_py CMakeLists.txt,增加依赖项
find_package(catkin REQUIRED COMPONENTS
roscpp
rospy
std_msgs
message_generation
)
5.CMakeLists.txt文件,增加消息文件,取消#,并修改为
add_message_files(
FILES
Num.msg
)
6.在CMakeLists.txt文件,增减消息生成包,取消#,并修改为
generation_messages(
DEPENDENCIES
std_msgs
)
7.在CMakeLists.txt文件,增加消息生成包,取消CATKIN_DEPENDS的#,并修改为
catkin_package(
CATKIN_DEPENDS roscpp rospy std_msgs messge_runtime
)
8.编译代码
cd ~/catkin_ws
catkin_make
9.检查服务
rosmsg show run_py/Num
输出:
int64 Num
五、编写发布器(即利用Python解析bag文件)
roscd run_py
1.建立Python脚本目录
mkdir scripts
cd scripts
2.创建解析文件bag_deal.py(名字随意)
touch bag_deal.py
chmod +x bag_deal.py
rosed run_py bag_deal.py
输入内用如下:
#!/usr/bin/python
# -*- coding: UTF-8 -*-
import roslib; #roslib.load_manifest(PKG)
import rosbag
import rospy
import cv2
from sensor_msgs.msg import Image
from cv_bridge import CvBridge
from cv_bridge import CvBridgeError
class ImageCreator():
def __init__(self):
self.bridge = CvBridge()
with rosbag.Bag('/home/saners/catkin_ws/src/run_py/scripts/abc.bag', 'r') as bag: #要读取的bag文件;
for topic,msg,t in bag.read_messages():
if topic == "/device_0/sensor_0/Depth_0/image/data":
try:
cv_image = self.bridge.imgmsg_to_cv2(msg)
timestr = "%.6f" % msg.header.stamp.to_sec()
#%.6f表示小数点后带有6位,可根据精确度需要修改;
image_name = '/home/saners/realtimedata/depth/'+timestr+ ".jpg" #图像命名:时间戳.jpg
cv2.imwrite(image_name, cv_image)
except CvBridgeError as e:
print e
elif topic == "/device_0/sensor_0/Infrared_1/image/data":
try:
cv_image1 = self.bridge.imgmsg_to_cv2(msg)
timestr = "%.6f" % msg.header.stamp.to_sec()
image_name1 = '/home/saners/realtimedata/rgb/'+timestr+ ".jpg"
cv2.imwrite(image_name1, cv_image1)
except CvBridgeError as e:
# pass
print e
if __name__ == '__main__':
try:
image_creator = ImageCreator()
except rospy.ROSInterruptException:
pass
关于内容的几点说明:
1)这个文件无法单独拿出去运行,需要ROS发布后使用
2)topic 的获取需要 rosbag info XXX.bag (你的bag文件) 这里给出一个例子:rosbag info abc.bag
path: abc.bag
version: 2.0
duration: 9.3s
start: Jan 01 1970 08:00:00.00 (0.00)
end: Jan 01 1970 08:00:09.27 (9.27)
size: 103.8 MB
messages: 2267
compression: none [114/114 chunks]
types: diagnostic_msgs/KeyValue [cf57fdc6617a881a88c16e768132149c]
geometry_msgs/Transform [ac9eff44abf714214112b05d54a3cf9b]
realsense_msgs/StreamInfo [311d7e24eac31bb87271d041bf70ff7d]
sensor_msgs/CameraInfo [c9a58c1b0b154e0e6da7578cb991d214]
sensor_msgs/Image [060021388200f6f0f447d0fcd9c64743]
std_msgs/Float32 [73fcbf46b49191e672908e50842a83d4]
std_msgs/String [992ce8a1687cec8c8bd883ec73ca41d1]
std_msgs/UInt32 [304a39449588c7f8ce2df6e8001c5fce]
topics: /device_0/info 7 msgs : diagnostic_msgs/KeyValue
/device_0/sensor_0/Depth_0/image/data 279 msgs : sensor_msgs/Image (这是图像)
/device_0/sensor_0/Depth_0/image/metadata 837 msgs : diagnostic_msgs/KeyValue
/device_0/sensor_0/Depth_0/info 1 msg : realsense_msgs/StreamInfo
/device_0/sensor_0/Depth_0/info/camera_info 1 msg : sensor_msgs/CameraInfo
/device_0/sensor_0/Depth_0/tf/0 1 msg : geometry_msgs/Transform
/device_0/sensor_0/Infrared_1/image/data 279 msgs : sensor_msgs/Image (这也是图像)
/device_0/sensor_0/Infrared_1/image/metadata 837 msgs : diagnostic_msgs/KeyValue
/device_0/sensor_0/Infrared_1/info 1 msg : realsense_msgs/StreamInfo
/device_0/sensor_0/Infrared_1/info/camera_info 1 msg : sensor_msgs/CameraInfo
/device_0/sensor_0/Infrared_1/tf/0 1 msg : geometry_msgs/Transform
/device_0/sensor_0/info 1 msg : diagnostic_msgs/KeyValue
/device_0/sensor_0/option/Asic Temperature/description 1 msg : std_msgs/String
/device_0/sensor_0/option/Asic Temperature/value 1 msg : std_msgs/Float32
/device_0/sensor_0/option/Depth Units/description 1 msg : std_msgs/String
/device_0/sensor_0/option/Depth Units/value 1 msg : std_msgs/Float32
/device_0/sensor_0/option/Enable Auto Exposure/description 1 msg : std_msgs/String
/device_0/sensor_0/option/Enable Auto Exposure/value 1 msg : std_msgs/Float32
/device_0/sensor_0/option/Error Polling Enabled/description 1 msg : std_msgs/String
/device_0/sensor_0/option/Error Polling Enabled/value 1 msg : std_msgs/Float32
/device_0/sensor_0/option/Exposure/description 1 msg : std_msgs/String
/device_0/sensor_0/option/Exposure/value 1 msg : std_msgs/Float32
/device_0/sensor_0/option/Frames Queue Size/description 1 msg : std_msgs/String
/device_0/sensor_0/option/Frames Queue Size/value 1 msg : std_msgs/Float32
/device_0/sensor_0/option/Gain/description 1 msg : std_msgs/String
/device_0/sensor_0/option/Gain/value 1 msg : std_msgs/Float32
/device_0/sensor_0/option/Output Trigger Enabled/description 1 msg : std_msgs/String
/device_0/sensor_0/option/Output Trigger Enabled/value 1 msg : std_msgs/Float32
/device_0/sensor_0/option/Stereo Baseline/description 1 msg : std_msgs/String
/device_0/sensor_0/option/Stereo Baseline/value 1 msg : std_msgs/Float32
/device_0/sensor_0/option/Visual Preset/description 1 msg : std_msgs/String
/device_0/sensor_0/option/Visual Preset/value 1 msg : std_msgs/Float32
/file_version 1 msg : std_msgs/UInt32
3)关于cv_bridge模块
http://wiki.ros.org/cv_bridge/Tutorials/ConvertingBetweenROSImagesAndOpenCVImagesPython
4)文件中的路径修改成自己电脑读取和存储的路径
3.编译代码
cd ~/catkin_ws
catkin_make
4.运行代码
打开新终端,启动roscore
roscore
打开另一个终端,启动bag_deal.py
rosrun run_py bag_deal.py
六、将bag文件产生的图像转换成视频
cd xxx(图像存储的文件夹)
mencoder "mf://*.jpg" -mf type=jpg:fps=15 -o output.mpg -speed 1 -ofps 30 -ovc lavc -lavcopts vcodec=mpeg2video:vbitrate=2500 -oac copy -of mpeg
注:mencoder 需要安装sudo apt install mencoder
到此就完成了bag文件的解析,其中有些步骤是多余的,由于本人不太了解ROS,请见谅!