我试图让我的相机使用 YUY2 格式而不是它当前使用的 MJPEG 格式来捕捉视频。我怎样才能在 OpenCV 中做到这一点?
因此,我查看了 OpenCV 中提供的fourcc 代码,并尝试了camera.set(cv2.CAP_PROP_FOURCC,844715353.0)。这个数字是 YUY2 的代码。但是,每当我运行程序时,它总是会返回 NV12 格式。我也尝试使用 VideoWriter,但仍然出现同样的问题。它从不切换。
import cv2
import sys
import numpy as np
import time
fourcc_names = ['CAP_PROP_POS_MSEC', 'CAP_PROP_POS_FRAMES', 'CAP_PROP_POS_AVI_RATIO', 'CAP_PROP_FRAME_WIDTH', 'CAP_PROP_FRAME_HEIGHT', 'CAP_PROP_FPS', 'CAP_PROP_FOURCC', 'CAP_PROP_FRAME_COUNT', 'CAP_PROP_FORMAT', 'CAP_PROP_MODE', 'CAP_PROP_BRIGHTNESS', 'CAP_PROP_CONTRAST', 'CAP_PROP_SATURATION', 'CAP_PROP_HUE', 'CAP_PROP_GAIN', 'CAP_PROP_EXPOSURE', 'CAP_PROP_CONVERT_RGB', 'CAP_PROP_WHITE_BALANCE_BLUE_U', 'CAP_PROP_RECTIFICATION', 'CAP_PROP_MONOCHROME', 'CAP_PROP_SHARPNESS', 'CAP_PROP_AUTO_EXPOSURE', 'CAP_PROP_GAMMA', 'CAP_PROP_TEMPERATURE', 'CAP_PROP_TRIGGER', 'CAP_PROP_TRIGGER_DELAY', 'CAP_PROP_WHITE_BALANCE_RED_V', 'CAP_PROP_ZOOM', 'CAP_PROP_FOCUS', 'CAP_PROP_GUID', 'CAP_PROP_ISO_SPEED', 'CAP_PROP_BACKLIGHT', 'CAP_PROP_PAN', 'CAP_PROP_TILT', 'CAP_PROP_ROLL', 'CAP_PROP_IRIS', 'CAP_PROP_SETTINGS', 'CAP_PROP_BUFFERSIZE', 'CAP_PROP_AUTOFOCUS']
def decode_fourcc(v):
v = int(v)
return "".join([chr((v >> 8 * i) & 0xFF) for i in range(4)])
print(cv2.__version__)
camera = cv2.VideoCapture()
camera.set(cv2.CAP_PROP_FOURCC,844715353.0)
#codec = 844715353.0 # YUY2
# print 'fourcc:', decode_fourcc(codec)
fourcc = cv2.VideoWriter_fourcc(*'YUY2')
#camera.set(cv2.CAP_PROP_FOURCC, codec)
camera.open(1)
#print Camera open:', camera.isOpened()
# print camera.get(cv2.CAP_PROP_FOURCC)
# C = camera.get(cv2.CAP_PROP_FOURCC)
# print 'fourcc original:', decode_fourcc(C)
# codec = 0x47504A4D # MJPG
# codec = 844715353.0 # YUY2
camera.set(cv2.CAP_PROP_FRAME_WIDTH, 3264)
camera.set(cv2.CAP_PROP_FRAME_HEIGHT, 2448)
camera.set(cv2.CAP_PROP_FPS, 15.0)
print(camera.get(cv2.CAP_PROP_FRAME_WIDTH))
print (camera.get(cv2.CAP_PROP_FRAME_HEIGHT))
print (camera.get(cv2.CAP_PROP_FPS))
C = camera.get(cv2.CAP_PROP_FOURCC)
print('fourcc:', decode_fourcc(C))
print
for i in range(38):
print(i, fourcc_names[i], camera.get(i))
while(1):
camera.grab()
retval, im = camera.retrieve(0)
cv2.imshow("image", im)
k = cv2.waitKey(1) & 0xff
if k == 27:
break
camera.release()
cv2.destroyAllWindows()
这是代码的输出。如您所见,fourcc 保持 NV12 并且在我设置时不会切换。我如何让它听?
4.1.0
3264.0
2448.0
15.0
fourcc: NV12
0 CAP_PROP_POS_MSEC 0.0
1 CAP_PROP_POS_FRAMES 0.0
2 CAP_PROP_POS_AVI_RATIO -1.0
3 CAP_PROP_FRAME_WIDTH 3264.0
4 CAP_PROP_FRAME_HEIGHT 2448.0
5 CAP_PROP_FPS 15.0
6 CAP_PROP_FOURCC 842094158.0
7 CAP_PROP_FRAME_COUNT -1.0
8 CAP_PROP_FORMAT -1.0
9 CAP_PROP_MODE 0.0
10 CAP_PROP_BRIGHTNESS 0.0
11 CAP_PROP_CONTRAST 32.0
12 CAP_PROP_SATURATION 105.0
13 CAP_PROP_HUE 0.0
14 CAP_PROP_GAIN 0.0
15 CAP_PROP_EXPOSURE -7.0
16 CAP_PROP_CONVERT_RGB 1.0
17 CAP_PROP_WHITE_BALANCE_BLUE_U -1.0
18 CAP_PROP_RECTIFICATION -1.0
19 CAP_PROP_MONOCHROME -1.0
20 CAP_PROP_SHARPNESS 3.0
21 CAP_PROP_AUTO_EXPOSURE 1.0
22 CAP_PROP_GAMMA 100.0
23 CAP_PROP_TEMPERATURE 4600.0
24 CAP_PROP_TRIGGER -1.0
25 CAP_PROP_TRIGGER_DELAY -1.0
26 CAP_PROP_WHITE_BALANCE_RED_V -1.0
27 CAP_PROP_ZOOM -1.0
28 CAP_PROP_FOCUS -1.0
29 CAP_PROP_GUID -1.0
30 CAP_PROP_ISO_SPEED -1.0
31 CAP_PROP_BACKLIGHT -1.0
32 CAP_PROP_PAN 1.0
33 CAP_PROP_TILT -1.0
34 CAP_PROP_ROLL -1.0
35 CAP_PROP_IRIS -1.0
36 CAP_PROP_SETTINGS -1.0
37 CAP_PROP_BUFFERSIZE -1.0