为了取得好的图片效果,我们需要设置摄像头的参数。
假如摄像流为 cap, 那么设置参数是cap.set(参数编号,参数)
获取参数值的函数是 cap.get(参数编号)
看一段摄像头参数设置读取的例子吧,代码里先设置3个参数,然后再读取这3个参数。
import cv2
cap = cv2.VideoCapture(0)
#先设置参数,然后读取参数
cap.set(3,1280) # width
cap.set(4,1024) # height
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
宽,高,设置有效,而曝光量设置返回无效。