opencv runtime_error ai_generated partial

VIDEOIO 错误:V4L2:流 0 的像素格式不受 OpenCV 支持

VIDEOIO ERROR: V4L2: Pixel format of stream 0 is not supported by OpenCV

ID: opencv/videoio-camera-format-unsupported

其他格式: JSON · Markdown 中文 · English
82%修复率
83%置信度
1证据数
2023-06-20首次发现

版本兼容性

版本状态引入弃用备注
4.5.5 active
4.6.0 active
4.7.0 active
4.8.1 active
4.9.0 active

根因分析

摄像头提供的像素格式(例如 MJPG、YUYV、NV12)OpenCV 的 V4L2 后端无法直接解码,需要转换。

English

The camera provides a pixel format (e.g., MJPG, YUYV, NV12) that OpenCV's V4L2 backend cannot decode directly without conversion.

generic

官方文档

https://docs.opencv.org/4.x/dd/d43/tutorial_py_video_display.html

解决方案

  1. Before opening the camera, set the desired pixel format using CAP_PROP_FOURCC: cap = cv2.VideoCapture(0); cap.set(cv2.CAP_PROP_FOURCC, cv2.VideoWriter_fourcc('M','J','P','G'))
  2. Use the GStreamer backend instead: cap = cv2.VideoCapture('v4l2src ! videoconvert ! appsink', cv2.CAP_GSTREAMER)

无效尝试

常见但无效的做法:

  1. Installing v4l-utils and running v4l2-ctl to change format without restarting the app 70% 失败

    The format change is not picked up by an already-opened VideoCapture; need to reopen.

  2. Setting CAP_PROP_FOURCC to 'MJPG' after opening camera 85% 失败

    The property must be set before opening the camera or on a closed capture.

  3. Recompiling OpenCV with different V4L2 flags but not rebuilding the app 90% 失败

    The app links against the old library; a full rebuild is needed.