Repository: opencv
Issue: AVMATRIX VC42 capture board: Pixel format of incoming image is unsupported by OpenCV
Labels: bug,category: videoio(camera)

### System Information

OpenCV 4.9.0
NVidia Jetson Orin
Operating System / Platform: Ubuntu 20.04
Camera model: capture board AVMATRIX VC42

### Detailed description

When I try to initialize OpenCV capture with V4L I got the following error:
```
initCapture VIDEOIO(V4L2:/dev/video0): Pixel format of incoming image is unsupported by OpenCV". 
```
I'm doing this from C# with [OpenCVSharp](https://github.com/shimat/opencvsharp) library.
Exactly the same error is happened when I reproducing it with the following Python code:
```
import cv2 as cv

cap = cv.VideoCapture(0, 200)

if not cap.isOpened():
    print("Cannot open camera")
    exit()

# Capture frame-by-frame
ret, frame = cap.read()
# if frame is read correctly ret is True
if not ret:
    print("Can't receive frame, exiting ...")
    exit()

cap.release()

data = frame.tobytes()
newFile = open("out.png", "wb")
newFile.write(data)
```

Here is the complete logs with all tracing options turning on:

```
nvidia@ubuntu:~/cap$ python cap.py
[DEBUG:0@0.038] global videoio_registry.cpp:218 VideoBackendRegistry VIDEOIO: Builtin backends(9): FFMPEG(1000); GSTREAMER(990); INTEL_MFX(980); V4L2(970); CV_IMAGES(960); CV_MJPEG(950); FIREWIRE(940); UEYE(930); OBSENSOR(920)
[DEBUG:0@0.042] global videoio_registry.cpp:242 VideoBackendRegistry VIDEOIO: Available backends(9): FFMPEG(1000); GSTREAMER(990); INTEL_MFX(980); V4L2(970); CV_IMAGES(960); CV_MJPEG(950); FIREWIRE(940); UEYE(930); OBSENSOR(920)
[ INFO:0@0.042] global videoio_registry.cpp:244 VideoBackendRegistry VIDEOIO: Enabled backends(9, sorted by priority): FFMPEG(1000); GSTREAMER(990); INTEL_MFX(980); V4L2(970); CV_IMAGES(960); CV_MJPEG(950); FIREWIRE(940); UEYE(930); OBSENSOR(920)
[ WARN:0@0.042] global cap.cpp:268 open VIDEOIO(V4L2): trying capture cameraNum=0 ...
[DEBUG:0@0.043] global cap_v4l.cpp:1005 open VIDEOIO(V4L2:/dev/video0): opening...
[DEBUG:0@0.043] global cap_v4l.cpp:1021 open VIDEOIO(V4L2:/dev/video0): deviceHandle=3
[DEBUG:0@0.043] global cap_v4l.cpp:1089 tryIoctl VIDEOIO(V4L2:/dev/video0): tryIoctl(3, VIDIOC_QUERYCAP(2154321408), failIfBusy=1)
[DEBUG:0@0.044] global cap_v4l.cpp:1097 tryIoctl VIDEOIO(V4L2:/dev/video0): call ioctl(3, VIDIOC_QUERYCAP(2154321408), ...) => 0    errno=0 (Success)
[DEBUG:0@0.044] global cap_v4l.cpp:1089 tryIoctl VIDEOIO(V4L2:/dev/video0): tryIoctl(3, VIDIOC_G_FMT(3234878980), failIfBusy=1)
[DEBUG:0@0.044] global cap_v4l.cpp:1097 tryIoctl VIDEOIO(V4L2:/dev/video0): call ioctl(3, VIDIOC_G_FMT(3234878980), ...) => 0    errno=0 (Success)
[DEBUG:0@0.044] global cap_v4l.cpp:1089 tryIoctl VIDEOIO(V4L2:/dev/video0): tryIoctl(3, VIDIOC_S_FMT(3234878981), failIfBusy=1)
[DEBUG:0@0.044] global cap_v4l.cpp:1097 tryIoctl VIDEOIO(V4L2:/dev/video0): call ioctl(3, VIDIOC_S_FMT(3234878981), ...) => -1    errno=22 (Invalid argument)
[DEBUG:0@0.044] global cap_v4l.cpp:1089 tryIoctl VIDEOIO(V4L2:/dev/video0): tryIoctl(3, VIDIOC_S_FMT(3234878981), failIfBusy=1)
[DEBUG:0@0.044] global cap_v4l.cpp:1097 tryIoctl VIDEOIO(V4L2:/dev/video0): call ioctl(3, VIDIOC_S_FMT(3234878981), ...) => -1    errno=22 (Invalid argument)
[DEBUG:0@0.045] global cap_v4l.cpp:1089 tryIoctl VIDEOIO(V4L2:/dev/video0): tryIoctl(3, VIDIOC_S_FMT(3234878981), failIfBusy=1)
[DEBUG:0@0.045] global cap_v4l.cpp:1097 tryIoctl VIDEOIO(V4L2:/dev/video0): call ioctl(3, VIDIOC_S_FMT(3234878981), ...) => -1    errno=22 (Invalid argument)
[DEBUG:0@0.045] global cap_v4l.cpp:1089 tryIoctl VIDEOIO(V4L2:/dev/video0): tryIoctl(3, VIDIOC_S_FMT(3234878981), failIfBusy=1)
[DEBUG:0@0.045] global cap_v4l.cpp:1097 tryIoctl VIDEOIO(V4L2:/dev/video0): call ioctl(3, VIDIOC_S_FMT(3234878981), ...) => -1    errno=22 (Invalid argument)
[DEBUG:0@0.045] global cap_v4l.cpp:1089 tryIoctl VIDEOIO(V4L2:/dev/video0): tryIoctl(3, VIDIOC_S_FMT(3234878981), failIfBusy=1)
[DEBUG:0@0.045] global cap_v4l.cpp:1097 tryIoctl VIDEOIO(V4L2:/dev/video0): call ioctl(3, VIDIOC_S_FMT(3234878981), ...) => -1    errno=22 (Invalid argument)
[DEBUG:0@0.046] global cap_v4l.cpp:1089 tryIoctl VIDEOIO(V4L2:/dev/video0): tryIoctl(3, VIDIOC_S_FMT(3234878981), failIfBusy=1)
[DEBUG:0@0.046] global cap_v4l.cpp:1097 tryIoctl VIDEOIO(V4L2:/dev/video0): call ioctl(3, VIDIOC_S_FMT(3234878981), ...) => -1    errno=22 (Invalid argument)
[DEBUG:0@0.046] global cap_v4l.cpp:1089 tryIoctl VIDEOIO(V4L2:/dev/video0): tryIoctl(3, VIDIOC_S_FMT(3234878981), failIfBusy=1)
[DEBUG:0@0.046] global cap_v4l.cpp:1097 tryIoctl VIDEOIO(V4L2:/dev/video0): call ioctl(3, VIDIOC_S_FMT(3234878981), ...) => -1    errno=22 (Invalid argument)
[DEBUG:0@0.046] global cap_v4l.cpp:1089 tryIoctl VIDEOIO(V4L2:/dev/video0): tryIoctl(3, VIDIOC_S_FMT(3234878981), failIfBusy=1)
[DEBUG:0@0.046] global cap_v4l.cpp:1097 tryIoctl VIDEOIO(V4L2:/dev/video0): call ioctl(3, VIDIOC_S_FMT(3234878981), ...) => -1    errno=22 (Invalid argument)
[DEBUG:0@0.046] global cap_v4l.cpp:1089 tryIoctl VIDEOIO(V4L2:/dev/video0): tryIoctl(3, VIDIOC_S_FMT(3234878981), failIfBusy=1)
[DEBUG:0@0.047] global cap_v4l.cpp:1097 tryIoctl VIDEOIO(V4L2:/dev/video0): call ioctl(3, VIDIOC_S_FMT(3234878981), ...) => -1    errno=22 (Invalid argument)
[DEBUG:0@0.047] global cap_v4l.cpp:1089 tryIoctl VIDEOIO(V4L2:/dev/video0): tryIoctl(3, VIDIOC_S_FMT(3234878981), failIfBusy=1)
[DEBUG:0@0.047] global cap_v4l.cpp:1097 tryIoctl VIDEOIO(V4L2:/dev/video0): call ioctl(3, VIDIOC_S_FMT(3234878981), ...) => -1    errno=22 (Invalid argument)
[DEBUG:0@0.047] global cap_v4l.cpp:1089 tryIoctl VIDEOIO(V4L2:/dev/video0): tryIoctl(3, VIDIOC_S_FMT(3234878981), failIfBusy=1)
[DEBUG:0@0.047] global cap_v4l.cpp:1097 tryIoctl VIDEOIO(V4L2:/dev/video0): call ioctl(3, VIDIOC_S_FMT(3234878981), ...) => -1    errno=22 (Invalid argument)
[DEBUG:0@0.047] global cap_v4l.cpp:1089 tryIoctl VIDEOIO(V4L2:/dev/video0): tryIoctl(3, VIDIOC_S_FMT(3234878981), failIfBusy=1)
[DEBUG:0@0.048] global cap_v4l.cpp:1097 tryIoctl VIDEOIO(V4L2:/dev/video0): call ioctl(3, VIDIOC_S_FMT(3234878981), ...) => -1    errno=22 (Invalid argument)
[DEBUG:0@0.048] global cap_v4l.cpp:1089 tryIoctl VIDEOIO(V4L2:/dev/video0): tryIoctl(3, VIDIOC_S_FMT(3234878981), failIfBusy=1)
[DEBUG:0@0.048] global cap_v4l.cpp:1097 tryIoctl VIDEOIO(V4L2:/dev/video0): call ioctl(3, VIDIOC_S_FMT(3234878981), ...) => -1    errno=22 (Invalid argument)
[DEBUG:0@0.048] global cap_v4l.cpp:1089 tryIoctl VIDEOIO(V4L2:/dev/video0): tryIoctl(3, VIDIOC_S_FMT(3234878981), failIfBusy=1)
[DEBUG:0@0.048] global cap_v4l.cpp:1097 tryIoctl VIDEOIO(V4L2:/dev/video0): call ioctl(3, VIDIOC_S_FMT(3234878981), ...) => -1    errno=22 (Invalid argument)
[DEBUG:0@0.048] global cap_v4l.cpp:1089 tryIoctl VIDEOIO(V4L2:/dev/video0): tryIoctl(3, VIDIOC_S_FMT(3234878981), failIfBusy=1)
[DEBUG:0@0.048] global cap_v4l.cpp:1097 tryIoctl VIDEOIO(V4L2:/dev/video0): call ioctl(3, VIDIOC_S_FMT(3234878981), ...) => -1    errno=22 (Invalid argument)
[DEBUG:0@0.049] global cap_v4l.cpp:1089 tryIoctl VIDEOIO(V4L2:/dev/video0): tryIoctl(3, VIDIOC_S_FMT(3234878981), failIfBusy=1)
[DEBUG:0@0.049] global cap_v4l.cpp:1097 tryIoctl VIDEOIO(V4L2:/dev/video0): call ioctl(3, VIDIOC_S_FMT(3234878981), ...) => -1    errno=22 (Invalid argument)
[DEBUG:0@0.049] global cap_v4l.cpp:1089 tryIoctl VIDEOIO(V4L2:/dev/video0): tryIoctl(3, VIDIOC_S_FMT(3234878981), failIfBusy=1)
[DEBUG:0@0.049] global cap_v4l.cpp:1097 tryIoctl VIDEOIO(V4L2:/dev/video0): call ioctl(3, VIDIOC_S_FMT(3234878981), ...) => -1    errno=22 (Invalid argument)
[DEBUG:0@0.049] global cap_v4l.cpp:1089 tryIoctl VIDEOIO(V4L2:/dev/video0): tryIoctl(3, VIDIOC_S_FMT(3234878981), failIfBusy=1)
[DEBUG:0@0.049] global cap_v4l.cpp:1097 tryIoctl VIDEOIO(V4L2:/dev/video0): call ioctl(3, VIDIOC_S_FMT(3234878981), ...) => -1    errno=22 (Invalid argument)
[DEBUG:0@0.050] global cap_v4l.cpp:1089 tryIoctl VIDEOIO(V4L2:/dev/video0): tryIoctl(3, VIDIOC_S_FMT(3234878981), failIfBusy=1)
[DEBUG:0@0.050] global cap_v4l.cpp:1097 tryIoctl VIDEOIO(V4L2:/dev/video0): call ioctl(3, VIDIOC_S_FMT(3234878981), ...) => -1    errno=22 (Invalid argument)
[DEBUG:0@0.050] global cap_v4l.cpp:1089 tryIoctl VIDEOIO(V4L2:/dev/video0): tryIoctl(3, VIDIOC_S_FMT(3234878981), failIfBusy=1)
[DEBUG:0@0.050] global cap_v4l.cpp:1097 tryIoctl VIDEOIO(V4L2:/dev/video0): call ioctl(3, VIDIOC_S_FMT(3234878981), ...) => -1    errno=22 (Invalid argument)
[DEBUG:0@0.050] global cap_v4l.cpp:1089 tryIoctl VIDEOIO(V4L2:/dev/video0): tryIoctl(3, VIDIOC_S_FMT(3234878981), failIfBusy=1)
[DEBUG:0@0.050] global cap_v4l.cpp:1097 tryIoctl VIDEOIO(V4L2:/dev/video0): call ioctl(3, VIDIOC_S_FMT(3234878981), ...) => -1    errno=22 (Invalid argument)
[ INFO:0@0.050] global cap_v4l.cpp:802 initCapture VIDEOIO(V4L2:/dev/video0): Pixel format of incoming image is unsupported by OpenCV
[ WARN:0@0.051] global cap_v4l.cpp:997 open VIDEOIO(V4L2:/dev/video0): can't open camera by index
[DEBUG:0@0.051] global cap_v4l.cpp:491 closeDevice VIDEOIO(V4L2:/dev/video0): close(3)
[ WARN:0@0.051] global cap.cpp:291 open VIDEOIO(V4L2): can't create capture
[ WARN:0@0.051] global cap.cpp:342 open VIDEOIO(V4L2): backend is generally available but can't be used to capture by index
[DEBUG:0@0.051] global cap.cpp:361 open VIDEOIO: choosen backend does not work or wrong.Please make sure that your computer support chosen backend and OpenCV built with right flags.
Cannot open camera
```

After investigation I found the cause of the issue and raising the bug report only to propose a way to fix it.

The cause is that V4L capture is trying to initialize camera with fixed resolution 640x480 and set the real resolution only after the initialization. This works for most cameras. But the AVMATRIX VC42 board driver does not support this resolution so it is failed.
Proposed fix is to allow setting resolution for camera initialization beforehand. 
The only way how it could be implemented without affecting internal architecture is to use environment variables. I already did it in my local copy and it does work so I'd like to share the solution.


### Steps to reproduce

Run the python code from the description. 
Note that the issue is specific to the board and will not be reproduced on most cameras!

### Issue submission checklist

- [X] I report the issue, it's not a question
- [X] I checked the problem with documentation, FAQ, open issues, forum.opencv.org, Stack Overflow, etc and have not found any solution
- [X] I updated to the latest OpenCV version and the issue is still there
- [X] There is reproducer code and related data files (videos, images, onnx, etc)