Tuesday, April 12, 2022

[SOLVED] OpenCV displaying images incorrectly

Issue

I am facing an issue in OpenCV. At the time of displaying any image I'm receiving multiple images ones more delayed than the others. I have the suspect that it is because of the camera channels. Camera issue

when I run the command v4l2-ctl --list-formats I get:

ioctl: VIDIOC_ENUM_FMT
Type: Video Capture

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

and for v4l2-ctl --list-formats-ext I get:

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)

Currently the OpenCV code is just a simple one to open my webcam:

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()

Solution

Solve it after a couple of hours looking on the web. I followed the steps in this forum: https://forums.raspberrypi.com/viewtopic.php?t=35689

In a nutshell I did:

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

and then added to my code:

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

so that I ended with this code:

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()

which just displays the camera.

P.S: I guess I'll have to set that piece of code in every single script.



Answered By - Angelo Espinoza
Answer Checked By - Willingham (WPSolving Volunteer)