当前位置:   article > 正文

ROS:使用usb_cam调用UVC摄像头遇到的坑_deprecated pixel format used, make sure you did se

deprecated pixel format used, make sure you did set range correctly

目录

 

坑一:装包后直接启用,图像是花屏

坑二:deprecated pixel format used, make sure you did set range correctly


坑一:装包后直接启用,图像是花屏

直接使用如下命令安装usb_cam功能包:

sudo apt-get install ros-melodic-usb-cam

然后插上自己的UVC摄像头,运行如下指令:

roslaunch usb_cam usb_cam-test.launch

此时可以检测到摄像头,但出来的图像是花的,如下:

 在查看了摄像头的数据手册之后,发现摄像头的默认像素格式是mjpeg格式的:

 于是乎查看了usb_cam-test.launch,发现原始的launch文件中定义的像素格式是yuyv格式的(第六行):

  1. <launch>
  2. <node name="usb_cam" pkg="usb_cam" type="usb_cam_node" output="screen" >
  3. <param name="video_device" value="/dev/video0" />
  4. <param name="image_width" value="640" />
  5. <param name="image_height" value="480" />
  6. <param name="pixel_format" value="yuyv" />
  7. <param name="camera_frame_id" value="usb_cam" />
  8. <param name="io_method" value="mmap"/>
  9. </node>
  10. <node name="image_view" pkg="image_view" type="image_view" respawn="false" output="screen">
  11. <remap from="image" to="/usb_cam/image_raw"/>
  12. <param name="autosize" value="true" />
  13. </node>
  14. </launch>

所以,将上述"yuyv"改成"mjpeg" ,在运行相应的launch文件,就可以的到正常的图片了:

坑二:deprecated pixel format used, make sure you did set range correctly

但是此时终端中会出现如下图所示的提示 :

虽然看着并没有什么影响,但是对于我等强迫症患者来说简直等同于error(T_T) ,在网上找了下以下解决方法:

usb_cam源码从GitHub上下载到工作空间中,然后在usb_cam源码src文件中找到usb_cam.cpp文件,大约在第430+行找到如下代码段:

  1. if (pic_size != avframe_camera_size_)
  2. {
  3. ROS_ERROR("outbuf size mismatch. pic_size: %d bufsize: %d", pic_size, avframe_camera_size_);
  4. return;
  5. }
  6. video_sws_ = sws_getContext(xsize, ysize, avcodec_context_->pix_fmt, xsize, ysize, AV_PIX_FMT_RGB24, SWS_BILINEAR, NULL,
  7. NULL, NULL);
  8. sws_scale(video_sws_, avframe_camera_->data, avframe_camera_->linesize, 0, ysize, avframe_rgb_->data,
  9. avframe_rgb_->linesize);
  10. sws_freeContext(video_sws_);

在代码段:video_sws_ = sws_get...  位置上面一行插入以下代码:

  1. {
  2. AVPixelFormat pixFormat;
  3. switch (avcodec_context_->pix_fmt) {
  4. case AV_PIX_FMT_YUVJ420P :
  5. pixFormat = AV_PIX_FMT_YUV420P;
  6. break;
  7. case AV_PIX_FMT_YUVJ422P :
  8. pixFormat = AV_PIX_FMT_YUV422P;
  9. break;
  10. case AV_PIX_FMT_YUVJ444P :
  11. pixFormat = AV_PIX_FMT_YUV444P;
  12. break;
  13. case AV_PIX_FMT_YUVJ440P :
  14. pixFormat = AV_PIX_FMT_YUV440P;
  15. break;
  16. default:
  17. pixFormat = avcodec_context_->pix_fmt;
  18. break;
  19. }
  20. avcodec_context_->pix_fmt = pixFormat;
  21. }

然后保存、重新编译、source devel/setup.bash、运行launch文件、完美解决问题。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/2023面试高手/article/detail/263123
推荐阅读
相关标签
  

闽ICP备14008679号