How to export image and video data from a bag file

Description: This tutorial explains how to export image messages from a bag file into a series of jpeg images and then goes on to show how to encode them into an OGG Theora video.

Contents

Tutorial setup
Exporting jpegs from bag file
Converting jpegs into an OGG Theora Video

Tutorial setup
This tutorial requires that you have previously recorded a bag file which contains image data you would like to export as jpeg images or video. Additionally, this tutorial requires that the image_view package has been built and a few video utilities are installed:

Toggle line numbers

   1 roscd image_view
   2 rosmake image_view --rosdep-install
   3 sudo aptitude install mjpegtools***

For newer versions :
Toggle line numbers

   1 roscd image_view
   2 rosmake image_view
   3 sudo apt-get install mjpegtools

This will install the necessary tools to complete the tutorial. The rest of this tutorial will assume that you have a .bag file previously created that is named test.bag and that this bag file is stored in the image_view package directory.

Exporting jpegs from bag file
To export jpeg images from a bag file first you will need to create a launch file which will dump the data. This example uses /camera/image_raw as the topic for the desired image data. This can be replaced as needed.

Toggle line numbers

 1 <launch>
   2   <node pkg="rosbag" type="play" name="rosbag" required="true" args="$(find image_view)/test.bag"/>
   3   <node name="extract" pkg="image_view" type="extract_images" respawn="false" required="true" output="screen" cwd="ROS_HOME">
   4     <remap from="image" to="/camera/image_raw"/>
   5   </node>
   6 </launch>

$(find image_view)/test.bag" 其中image_view表示package名字,此处改为你的.bag文件所处的package的名字即可,同时test改为对应的名字。

此处表示需将你记录的topic名字覆盖掉/image 这个原始的topic。

The launch file can be started by running

Toggle line numbers

   1 roslaunch export.launch

This will dump the images name frame%04d.jpg into the folder “.ros” in your home directory.

The images files can be easily to moved to where ever is convenient.

Toggle line numbers

   1 cd ~
   2 mkdir test
   3 mv ~/.ros/frame*.jpg test/

If your bag file contains compressed images for example on /camera/image_raw/compressed it will be necessary for you to decompress the images before exporting.

Toggle line numbers
1 rosrun image_transport republish compressed in:=camera/image_raw raw out:=camera_out/image
Now your decompressed images will be the topic /camera_out/image, so you should change line 4 in the launch file to:

Toggle line numbers

   1 <remap from="image" to="/camera_out/image"/>

Converting jpegs into an OGG Theora Video
These instructions are based on the information found here, and have been tested and shown to work.

If your camera was running at 15 frames per second then you would execute the following in a shell for reasonable results.

Toggle line numbers

 1 cd ~/test
   2 jpeg2yuv -I p -f 15 -j frame%04d.jpg -b 1 > tmp.yuv
   3 ffmpeg2theora --optimize --videoquality 10 --videobitrate 16778 -o output.ogv tmp.yuv

or generate MPEG video directly from jpegs.

Toggle line numbers

   1 cd ~/test
   2 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

Also, to generate an mp4 video directly from jpg on the command line, you can use -

Toggle line numbers

   1 cd ~/test
   2 ffmpeg -framerate 25 -i frame%04d.jpg -c:v libx264 -profile:v high -crf 20 -pix_fmt yuv420p output.mp4 

命令行参数注释
framerate 25(帧率25)
-r代表帧率
-i代表输入
frame%04d.jpg( 输出格式是4位,不够4位前面补零 )
ffmpg控制码率有3种选择,-minrate -b:v -maxrate
-b:v主要是控制平均码率。比如一个视频源的码率太高了,有10Mbps,文件太大,想把文件弄小一点,但是又不破坏分辨率。ffmpeg -i input.mp4 -b:v 2000k output.mp4上面把码率从原码率转成2Mbps码率,这样其实也间接让文件变小了。目测接近一半。不过,ffmpeg官方wiki比较建议,设置b:v时,同时加上 -bufsize 用于设置码率控制缓冲器的大小,设置的好处是,让整体的码率更趋近于希望的值,减少波动。(简单来说,比如1 2的平均值是1.5, 1.49 1.51 也是1.5, 当然是第二种比较好)
ffmpeg -i input.mp4 -b:v 2000k -bufsize 2000k output.mp4
-minrate -maxrate就简单了,在线视频有时候,希望码率波动,不要超过一个阈值,可以设置maxrate。ffmpeg -i input.mp4 -b:v 2000k -bufsize 2000k -maxrate 2500k output.mp4
libx264(编码接口函数),x264是一个 H.264/MPEG4 AVC 编码器
-profile:v high 它可以将你的输出限制到一个特定的 H.264 profile,该选项可以被忽略除非你的播放设备只支持某种profile。当的所有profile 的档次包括:baseline,main.high,high10,high422,high444 。注意使用–profile选项和无损编码是不兼容的。如果你想让你的视频最大化的和目标播放设备兼容(比如老版本的的ios或者所有的android 设备),那么你可以用:-profile:v baseline 这将会关闭很多高级特性,但是它会提供很好的兼容性。也许你可能不需要这些设置,因为一旦你用了这些设置,在同样的视频质量下与更高的编码档次相比会使比特率稍有增加。
-crf 20(在优先保证画面质量(也不太在乎转码时间)的情况下,使用**-crf参数来控制转码质量(码率)是比较适宜的**。这个参数的取值范围为051,其中0为无损模式,数值越大,画质越差,生成的文件却越小。从主观上讲,**1828是一个合理的范围。18被认为是视觉无损的(从技术角度上看当然还是有损的),它的输出视频质量和输入视频相当。我们的策略是,在保证可接受视频画质的前提下,选择一个最大的crf值——如果输出视频质量很好,那就尝试一个更大的值;如果看起来很糟,那就尝试一个小一点的值。)
-pix_fmt yuv420p(图片的
颜色空间**是yuv420p,YUV420p的像素颜色范围是[16,235],16表示黑色,235表示白色)
output.mp4(输出mp4格式)

参考资料:

MPG又称MPEG(MovingPicturesExpertsGroup)即动态图像专家组,由国际标准化组织ISO(InternationalStandardsOrganization)与IEC(InternationalElectronicCommittee)于1988年联合成立,专门致力于**运动图像(MPEG视频)及其伴音编码(MPEG音频)**标准化工作。MPEG标准主要有以下五个。

1.在Ubuntu上安装了mpegtools包,以便用jpeg2yuv 这是一个把JPG图像文件转换成YUV原始数据格式的程序,就三个函数,输出为YUV420的。-this is a static library for decompressing the jpg file and converting its colorspace from yuv422 to yuv420, but RGB24. enjoy your self!

2.ffmpeg2theora这是一个把ffmpeg格式转换成theora格式的工具。(This is a tool for converting ffmpeg format into Theora format.)

3.mencoder 是一款命令行方式的视频处理软件,是Mplayer自带的编码工具(Mplayer是Linux下的播放器,开源,支持几乎所有视频格式的播放,有windows和Mac版本)。

1.2.3中的黑体子读需要用sudo apt-get install name安装的tools

4.YUVJ420P的字面含义是“使用了JPEG颜色范围的YUV420P,像素使用表示颜色的数值范围发生了变化。
YUV420p的像素颜色范围是[16,235],16表示黑色,235表示白色
YUVJ420P的像素颜色范围是[0,255]。0表示黑色,255表示白色

从这里来看,貌似是YUVJ420P表示的更准确一下。

区别的缘由
YUV420p对应的是电视。
YUVJ420P对应的是显示器。

灰阶
何谓灰阶?通常来说,液晶屏幕上人们肉眼所见的一个点,即一个像素,它是由红、绿、蓝(RGB)三个子像素组成的。每一个子像素,其背后的光源都可以显现出不同的亮度级别。而灰阶代表了由最暗到最亮之间不同亮度的层次级别。这中间层级越多,所能够呈现的画面效果也就越细腻。以8bit面板为例,能表现2的8次方,等于256个亮度层次,我们就称之为256灰阶。LCD屏幕上每个像素,均由不同亮度层次的红、绿、蓝组合起来,最终形成不同的色彩点。也就是说,屏幕上每一个点的色彩变化,其实都是由构成这个点的三个RGB子像素的灰阶变化所带来的。

电脑如果想显示准确的色彩,那么必须要0-255灰阶全部齐全。灰阶的损失,就意味着色彩的损失。 对于PC主机,灰阶的输出是0-255。灰阶输出为0的情况下,显示全黑画面;灰阶为255的情况下,显示全白画面。液晶显示器为了适应PC主机,所可接受的灰阶信号范围同样是0-255。

256个灰阶输出图,如果PC接电视,必然会有13%的灰阶损失 而液晶电视是不同的,它的灰阶范围是16-235,当接收到信号16时,液晶电视将其还原成白色;当接收到信号235时,显示全白画面。电视的输入设备,比如说有线电视信号、DVD影碟机等输出的也是16-235的灰阶信号。 液晶电视如果接收到了低于16的信号,会被认为是干挠,将不被显示;如果接收到了高于235的信号,大部分液晶电视只能做削波处理,即仍当做235信号来处理。简单点说,就是0-15全部显示纯黑,236-255全部显示纯白,中间的过渡全部损失。

原文链接:https://blog.csdn.net/samsung12345678/article/details/102383708
5.视频编解码—x264用于编码,ffmpeg用于解码 https://blog.csdn.net/scalerzhangjie/article/details/8273410

参考文章:
http://wiki.ros.org/rosbag/Tutorials/Exporting%20image%20and%20video%20data
https://blog.csdn.net/scalerzhangjie/article/details/8273410
https://blog.csdn.net/vblittleboy/article/details/8982857
https://blog.csdn.net/lsw8569013/article/details/53005116
https://www.cnblogs.com/frost-yen/p/5848781.html(这篇文章讲的比较全包括:1. 视频转换2. 提取音频3. 提取视频4. 视频剪切5. 码率控制6. 视频编码格式转换7. 只提取视频ES数据8. 过滤器的使用8.1 将输入的1920x1080缩小到960x540输出:8.2 为视频添加logo8.3 去掉视频的logo9. 截取视频图像10. 序列帧与视频的相互转换)
https://blog.csdn.net/matrix_laboratory/article/details/72764621

发布了23 篇原创文章 · 获赞 3 · 访问量 618

猜你喜欢

转载自blog.csdn.net/weixin_43042467/article/details/103843960