OpenCV 显示图像不正确

OpenCV displaying images incorrectly

我在 OpenCV 中遇到问题。在显示任何图像时,我收到的多张图像比其他图像延迟更多。我怀疑这是因为相机频道。

当我 运行 命令 v4l2-ctl --list-formats 我得到:

ioctl: VIDIOC_ENUM_FMT
Type: Video Capture

[0]: 'YUYV' (YUYV 4:2:2)
[1]: 'MJPG' (Motion-JPEG, compressed)

对于 v4l2-ctl --list-formats-ext 我得到:

ioctl: VIDIOC_ENUM_FMT
Type: Video Capture

[0]: 'YUYV' (YUYV 4:2:2)
    Size: Discrete 640x480
        Interval: Discrete 0.033s (30.000 fps)
        Interval: Discrete 0.067s (15.000 fps)
    Size: Discrete 160x120
        Interval: Discrete 0.033s (30.000 fps)
        Interval: Discrete 0.067s (15.000 fps)
    Size: Discrete 176x144
        Interval: Discrete 0.033s (30.000 fps)
        Interval: Discrete 0.067s (15.000 fps)
    Size: Discrete 320x240
        Interval: Discrete 0.033s (30.000 fps)
        Interval: Discrete 0.067s (15.000 fps)
    Size: Discrete 1280x720
        Interval: Discrete 0.100s (10.000 fps)
        Interval: Discrete 0.200s (5.000 fps)
    Size: Discrete 1280x800
        Interval: Discrete 0.100s (10.000 fps)
        Interval: Discrete 0.200s (5.000 fps)
    Size: Discrete 1280x1024
        Interval: Discrete 0.200s (5.000 fps)
    Size: Discrete 1920x1080
        Interval: Discrete 0.200s (5.000 fps)
[1]: 'MJPG' (Motion-JPEG, compressed)
    Size: Discrete 640x480
        Interval: Discrete 0.033s (30.000 fps)
        Interval: Discrete 0.067s (15.000 fps)
    Size: Discrete 160x120
        Interval: Discrete 0.033s (30.000 fps)
        Interval: Discrete 0.067s (15.000 fps)
    Size: Discrete 176x144
        Interval: Discrete 0.033s (30.000 fps)
        Interval: Discrete 0.067s (15.000 fps)
    Size: Discrete 320x240
        Interval: Discrete 0.033s (30.000 fps)
        Interval: Discrete 0.067s (15.000 fps)
    Size: Discrete 1280x720
        Interval: Discrete 0.033s (30.000 fps)
        Interval: Discrete 0.067s (15.000 fps)
    Size: Discrete 1280x800
        Interval: Discrete 0.033s (30.000 fps)
        Interval: Discrete 0.067s (15.000 fps)
    Size: Discrete 1280x1024
        Interval: Discrete 0.033s (30.000 fps)
        Interval: Discrete 0.067s (15.000 fps)
    Size: Discrete 1920x1080
        Interval: Discrete 0.033s (30.000 fps)
        Interval: Discrete 0.067s (15.000 fps)

目前 OpenCV 代码只是打开我的网络摄像头的简单代码:

import cv2
    
cap=cv2.VideoCapture(-1)   
videoCapture.set(CV_CAP_PROP_CONVERT_RGB, false) 
 
while True:
    r,im=cap.read()
    cv2.imshow('dd',im[:, :, 0])
    k=cv2.waitKey(30) & 0xff
    if k==27:
         break
    
cap.release()
cv2.destroyAllWindows()

在网上查了几个小时后解决了。我遵循了这个论坛中的步骤:https://forums.raspberrypi.com/viewtopic.php?t=35689

简而言之,我做了:

> lsmod 
> sudo rmmod uvcvideo 
> sudo modprobe uvcvideo nodrop=1 timeout=5000 quirks=0x90

然后添加到我的代码中:

cap.set(cv2.CAP_PROP_FOURCC,cv2.VideoWriter_fourcc('M','J','P','G'))

所以我以这段代码结束:

import cv2
    
# cap = cap = cv2.VideoCapture(f'v4l2src device=/dev/video0 io-mode=2 ! image/jpeg, width=(int)176, height=(int)144 !  nvjpegdec ! video/x-raw, format=I420 ! appsink', cv2.CAP_GSTREAMER)
cap = cv2.VideoCapture(0, cv2.CAP_V4L2)
# cap.set(cv2.CAP_PROP_CONVERT_RGB, 0);          # Request raw camera data
cap.set(cv2.CAP_PROP_FOURCC,cv2.VideoWriter_fourcc('M','J','P','G'))
while True:
    r,im=cap.read()
    cv2.imshow('dd',im)
    k=cv2.waitKey(30) & 0xff
    if k==27:
         break
    
cap.release()
cv2.destroyAllWindows()

只显示相机。

P.S:我想我必须在每个脚本中设置那段代码。