为了取得好的图片效果,我们需要设置摄像头的参数。
假如摄像流为 cap, 那么设置参数是cap.set(参数编号,参数)
获取参数值的函数是 cap.get(参数编号)
看一段摄像头参数设置读取的例子吧,代码里先设置3个参数,然后再读取这3个参数。
import cv2
#选择摄像头号,一般从 0 开始
cap = cv2.VideoCapture(0)
#先设置参数,然后读取参数
cap.set(3,1280)
cap.set(4,1024)
cap.set(15, 0.1)
print("width={}".format(cap.get(3)))
print("height={}".format(cap.get(4)))
print("exposure={}".format(cap.get(15)))
while True:
ret, img = cap.read()
cv2.imshow("input", img)
# 按 ESC 键退出
key = cv2.waitKey(10)
if key == 27:
break
cv2.destroyAllWindows()
cv2.VideoCapture(0).release()
我的程序运行结果是:
width=1280.0
height=720.0
exposure=-1.0
宽,高,设置有效,而曝光量设置返回无效。
摄像头有哪些参数可以设置呢?参数编号的对应关系怎么样,请看下面列表。
0. CV_CAP_PROP_POS_MSEC Current position of the video file in milliseconds.
1. CV_CAP_PROP_POS_FRAMES 0-based index of the frame to be decoded/captured next.
2. CV_CAP_PROP_POS_AVI_RATIO Relative position of the video file
3. CV_CAP_PROP_FRAME_WIDTH Width of the frames in the video stream.
4. CV_CAP_PROP_FRAME_HEIGHT Height of the frames in the video stream.
5. CV_CAP_PROP_FPS Frame rate.
6. CV_CAP_PROP_FOURCC 4-character code of codec.
7. CV_CAP_PROP_FRAME_COUNT Number of frames in the video file.
8. CV_CAP_PROP_FORMAT Format of the Mat objects returned by retrieve() .
9. CV_CAP_PROP_MODE Backend-specific value indicating the current capture mode.
10. CV_CAP_PROP_BRIGHTNESS Brightness of the image (only for cameras).
11. CV_CAP_PROP_CONTRAST Contrast of the image (only for cameras).
12. CV_CAP_PROP_SATURATIO