Video camera formats: yuv411, yuv422

本文介绍了YUV444、YUV422及YUV411三种常见视频格式的数据布局,并提供了将这些格式转换为RGB格式的C语言示例代码。文章指出YUV格式因其对人眼敏感度特性的利用,在图像处理和分析中具有优势。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

转自: http://paulbourke.net/dataformats/yuv/

http://www.cnblogs.com/qinjunni/archive/2012/02/23/2364446.html

There are a number of video formats used by digital video cameras say, perhaps the most common at the time of writing are called yuv444, yuv422, and yuv411. The following briefly describes the layout of the data (luminance and two chrominance channels) and describes how to convert these to the more familiar RGB, noting of course that it may be more appropriate to do some image processing and analysis in YUV (also known as YCbCr space. The basic idea behind these formats is that the human visual system is less sensitive to high frequency colour information (compared to luminance) so the colour information can be encoded at a lower spatial resolution.

422

To reconstruct pixels in pairs, 4 bytes of data are required. The bytes are arranged as u, y1, v, y2. The total number of bytes in the image stream is 2 x width x height.

void Convert422(unsigned char *yuv,BITMAP4 *rgb1,BITMAP4 *rgb2)
{
   int y1,y2,u,v;

   // Extract yuv components
   u  = yuv[0];
   y1 = yuv[1];
   v  = yuv[2];
   y2 = yuv[3];

   // yuv to rgb
   *rgb1 = YUV_to_Bitmap(y1,u,v);
   *rgb2 = YUV_to_Bitmap(y2,u,v);
}

411

To reconstract pixels in sets of 4, 6 bytes of data are required. The bytes are arranged as u, y1, y2, v, y3, y4. The total number of bytes in the image stream is 6 x width x height / 4.

void Convert411(unsigned char *yuv,BITMAP4 *rgb1,BITMAP4 *rgb2,BITMAP4 *rgb3,BITMAP4 *rgb4)
{  
   int y1,y2,y3,y4,u,v;
   
   // Extract yuv components
   u  = yuv[0];
   y1 = yuv[1];
   y2 = yuv[2];
   v  = yuv[3];
   y3 = yuv[4];
   y4 = yuv[5];

   // yuv to rgb
   *rgb1 = YUV_to_Bitmap(y1,u,v);
   *rgb2 = YUV_to_Bitmap(y2,u,v);
   *rgb3 = YUV_to_Bitmap(y3,u,v);
   *rgb4 = YUV_to_Bitmap(y4,u,v);
}

Conversion to RGB

The plain C source provided here is written for clarify not efficiency, in general these conversions would be performed by the way of lookup tables.

BITMAP4 YUV_to_Bitmap(int y,int u,int v)
{
   int r,g,b;
   BITMAP4 bm = {0,0,0,0};
   
   // u and v are +-0.5
   u -= 128;
   v -= 128;

   // Conversion
   r = y + 1.370705 * v;
   g = y - 0.698001 * v - 0.337633 * u;
   b = y + 1.732446 * u;

/*
   r = y + 1.402 * v;
   g = y - 0.344 * u - 0.714 * v;
   b = y + 1.772 * u;
*/
/*
   y -= 16;
   r = 1.164 * y + 1.596 * v;
   g = 1.164 * y - 0.392 * u - 0.813 * v;
   b = 1.164 * y + 2.017 * u;
*/

   // Clamp to 0..1
   if (r < 0) r = 0;
   if (g < 0) g = 0;
   if (b < 0) b = 0;
   if (r > 255) r = 255;
   if (g > 255) g = 255;
   if (b > 255) b = 255;

   bm.r = r;
   bm.g = g;
   bm.b = b;
   bm.a = 0;

   return(bm);
}

 

[ 95%] Built target find_object_2d [ 96%] Building CXX object abot_bringup/CMakeFiles/abot_driver.dir/src/base_driver_config.cpp.o [ 97%] Building CXX object abot_bringup/CMakeFiles/abot_driver.dir/src/base_driver.cpp.o [ 97%] Linking CXX executable /home/robot/catkin_ws/devel/lib/abot_bringup/abot_driver [100%] Built target abot_driver robot@robot-NUC7CJYH:~/catkin_ws$ roslaunch usb_cam usb_cam-test.launch ... logging to /home/robot/.ros/log/e0639e48-fd95-11ef-bbb8-982cbceead36/roslaunch-robot-NUC7CJYH-1079.log Checking log directory for disk usage. This may take a while. Press Ctrl-C to interrupt Done checking log file disk usage. Usage is <1GB. started roslaunch server http://robot-NUC7CJYH:41197/ SUMMARY ======== PARAMETERS * /image_view/autosize: True * /rosdistro: melodic * /rosversion: 1.14.13 * /usb_cam/camera_frame_id: usb_cam * /usb_cam/color_format: yuv422p * /usb_cam/image_height: 480 * /usb_cam/image_width: 640 * /usb_cam/io_method: mmap * /usb_cam/pixel_format: yuyv * /usb_cam/video_device: /dev/video0 NODES / image_view (image_view/image_view) usb_cam (usb_cam/usb_cam_node) auto-starting new master process[master]: started with pid [1104] ROS_MASTER_URI=http://localhost:11311 setting /run_id to e0639e48-fd95-11ef-bbb8-982cbceead36 process[rosout-1]: started with pid [1117] started core service [/rosout] process[usb_cam-2]: started with pid [1120] process[image_view-3]: started with pid [1125] [ INFO] [1741600564.510688056]: Initializing nodelet with 2 worker threads. [ INFO] [1741600564.731082433]: Using transport "raw" [ INFO] [1741600564.762924580]: using default calibration URL [ INFO] [1741600564.764182432]: camera calibration URL: file:///home/robot/.ros/camera_info/head_camera.yaml [ INFO] [1741600564.764397842]: Unable to open camera calibration file [/home/robot/.ros/camera_info/head_camera.yaml] [ WARN] [1741600564.764545731]: Camera calibration file /home/robot/.ros/camera_info/head_camera.yaml not found. [ INFO] [1741600564.764692670]: Starting 'head_camera' (/dev/video0) at 640x480 via mmap (yuyv) at 30 FPS [ WARN] [1741600565.765906413]: unknown control 'focus_auto' 如何修改摄像头的图片大小
03-11
<think>嗯,用户问的是如何修改摄像头的图片大小。首先,我需要看看用户提供的日志信息,里面可能有线索。在用户提供的roslaunch输出中,有参数设置的部分,比如/usb_cam/image_height和/usb_cam/image_width,当前是480和640。这说明用户在使用usb_cam包来驱动摄像头,并且当前的分辨率是640x480。用户可能想调整这个分辨率,比如提高或降低。 接下来,我需要回忆一下ROS中usb_cam包的配置方法。通常,这些参数是在launch文件中设置的。用户运行的命令是roslaunch usb_cam usb_cam-test.launch,所以应该检查这个launch文件的参数。用户可能需要编辑这个launch文件,修改image_width和image_height的值。 然后,我要考虑摄像头是否支持用户想要设置的分辨率。如果用户设置的尺寸不被摄像头支持,可能会出错或者无法生效。这时候可能需要使用v4l2-ctl工具来查询摄像头支持的分辨率,比如运行v4l2-ctl --list-formats-ext。但用户可能不知道这个步骤,所以在回答中需要提到这一点,确保用户检查摄像头的能力。 另外,用户提供的日志中有一个警告:unknown control 'focus_auto'。这可能和摄像头驱动或参数有关,但暂时不影响分辨率的问题,可以忽略,或者提醒用户注意其他潜在问题,但主要解决分辨率调整。 然后,步骤应该是:1. 找到并编辑launch文件,修改width和height参数;2. 保存后重新运行launch文件;3. 验证是否生效,可能需要查看发布的图像话题的信息,或者image_view中的图像尺寸变化。 但用户可能对ROS的launch文件结构不太熟悉,所以需要给出具体的修改示例,比如在launch文件中找到对应的参数行,并修改数值。例如,在usb_cam-test.launch中,可能已经有类似<param name="image_width" value="640" />这样的行,改成所需的数值。 此外,需要提醒用户可能的权限问题,比如摄像头设备/dev/video0的访问权限,但根据用户提供的日志,似乎已经能够访问,所以可能不需要处理。但若修改后无效,可能需要检查摄像头是否支持该分辨率。 总结回答的结构应该是:首先说明通过修改launch文件中的参数,然后给出具体步骤,包括查找支持的分辨率,编辑文件,验证结果。可能还需要提到动态重新配置的方法,如果有的话,比如使用rqt_reconfigure,但usb_cam是否支持这个可能需要确认。根据我的知识,usb_cam_node可能支持动态参数调整,可以通过rosrun rqt_reconfigure rqt_reconfigure来调整参数,但可能还是需要先确保launch文件中的参数正确。 另外,用户可能遇到修改后没有变化的情况,这时候需要建议他们检查摄像头支持的分辨率,或者是否有其他节点覆盖了参数。例如,如果launch文件中参数设置正确,但摄像头不支持,那么usb_cam_node可能会保持默认值或者报错。 最后,确保回答清晰,步骤明确,帮助用户逐步解决问题,同时符合之前提到的格式要求,比如使用中文,行内公式用$...$,虽然这个问题可能用不到数学公式,但如果有提到分辨率比例的话可能需要,不过这里可能不需要。所以主要按照用户的要求,结构清晰,步骤正确。</think>根据您提供的ROS日志信息,修改摄像头图片大小需要通过usb_cam功能包的参数配置来实现。以下是分步解决方案: **1. 定位参数位置** 在`usb_cam-test.launch`文件中明确设置了: ```xml <param name="image_width" value="640" /> <param name="image_height" value="480" /> ``` **2. 验证摄像头支持的分辨率(关键步骤)** 在终端执行: ```bash v4l2-ctl -d /dev/video0 --list-formats-ext ``` 这将列出摄像头支持的所有分辨率(如$1280 \times 720$, $1920 \times 1080$等),必须选择设备支持的分辨率组合 **3. 修改launch文件** 以修改为$1280 \times 720$为例: ```xml <param name="image_width" value="1280" /> <param name="image_height" value="720" /> ``` **4. 配置参数验证** 重启节点后查看日志输出: ``` [ INFO] [时间戳]: Starting 'head_camera' (/dev/video0) at 1280x720 via mmap... ``` 确认显示的目标分辨率是否与设置一致 **5. 动态调整方法(可选)** 对于支持动态重配置的驱动: ```bash rosrun rqt_reconfigure rqt_reconfigure ``` 在GUI中找到`usb_cam`节点直接修改参数 **常见问题处理:** - 若出现`VIDIOC_S_FMT error`,表示摄像头不支持该分辨率 - 若出现`image truncated`警告,尝试修改`io_method`参数 - 分辨率改变后需要同步调整相机标定文件`head_camera.yaml`中的参数 **建议测试序列:** 1. 优先测试$640 \times 480$ (VGA) 2. 尝试$1280 \times 720$ (HD) 3. 最后测试$1920 \times 1080$ (FHD) 注意:高分辨率会增加计算负载,需根据实际硬件性能选择。修改后建议使用`rostopic hz /usb_cam/image_raw`监控帧率变化。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值