我是靠谱客的博主 自觉万宝路,最近开发中收集的这篇文章主要介绍Python 下opencv 应用: 摄像头参数设置,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

为了取得好的图片效果,我们需要设置摄像头的参数。

假如摄像流为 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_SATURATION Saturation of the image (only for cameras).
13. CV_CAP_PROP_HUE Hue of the image (only for cameras).
14. CV_CAP_PROP_GAIN Gain of the image (only for cameras).
15. CV_CAP_PROP_EXPOSURE Exposure (only for cameras).
16. CV_CAP_PROP_CONVERT_RGB Boolean flags indicating whether images should be converted to RGB.
17. CV_CAP_PROP_WHITE_BALANCE Currently unsupported
18. CV_CAP_PROP_RECTIFICATION Rectification flag for stereo cameras (note: only supported by DC1394 v 2.x backend currently)

这里是可能可以设置的参数,摄像头不一样,有些参数可能是不能设置的。 

一般读取返回-1 的就是无效,下面再看一段检测有效无效的代码,就是上面代码改的

import cv2

#选择摄像头号,一般从 0 开始
cap = cv2.VideoCapture(0)

#先设置参数,然后读取参数
for i in range(47):
    print("No.={} parameter={}".format(i,cap.get(i)))

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

运行结果是:

0=0.0
1=0.0
2=-1.0
3=1280.0
4=720.0
5=30.0
6=842094158.0
7=-1.0
8=-1.0
9=0.0
10=0.0
11=0.0
12=64.0
13=0.0
14=0.0
15=-1.0
16=1.0
17=-1.0
18=-1.0
19=-1.0
20=2.0
21=-1.0
22=100.0
23=4600.0
24=-1.0
25=-1.0
26=-1.0
27=-1.0
28=-1.0
29=-1.0
30=-1.0
31=-1.0
32=3.0
33=-1.0
34=-1.0
35=-1.0
36=-1.0
37=-1.0
38=-1.0
39=-1.0
40=1.0
41=1.0
42=1400.0
43=-1.0
44=-1.0
45=-1.0
46=-1.0

可以看到我的摄像头哪些参数可以设置,不为-1的。

在程序中编号很容易弄错,可以用如下方式比较好看一点,也不容易出错。

cap.set(cv2.CAP_PROP_FRAME_WIDTH,1280)
cap.set(cv2.CAP_PROP_FRAME_HEIGHT,1024)
cap.set(cv2.CAP_PROP_EXPOSURE, 0.1)

上面表不完整,但基本都包括了我们看看现在opencv 里面怎么说的:

https://github.com/opencv/opencv/blob/master/modules/videoio/include/opencv2/videoio.hpp

enum VideoCaptureProperties {
       CAP_PROP_POS_MSEC       =0, //!< Current position of the video file in milliseconds.
       CAP_PROP_POS_FRAMES     =1, //!< 0-based index of the frame to be decoded/captured next.
       CAP_PROP_POS_AVI_RATIO  =2, //!< Relative position of the video file: 0=start of the film, 1=end of the film.
       CAP_PROP_FRAME_WIDTH    =3, //!< Width of the frames in the video stream.
       CAP_PROP_FRAME_HEIGHT   =4, //!< Height of the frames in the video stream.
       CAP_PROP_FPS            =5, //!< Frame rate.
       CAP_PROP_FOURCC         =6, //!< 4-character code of codec. see VideoWriter::fourcc .
       CAP_PROP_FRAME_COUNT    =7, //!< Number of frames in the video file.
       CAP_PROP_FORMAT         =8, //!< Format of the %Mat objects returned by VideoCapture::retrieve().
       CAP_PROP_MODE           =9, //!< Backend-specific value indicating the current capture mode.
       CAP_PROP_BRIGHTNESS    =10, //!< Brightness of the image (only for those cameras that support).
       CAP_PROP_CONTRAST      =11, //!< Contrast of the image (only for cameras).
       CAP_PROP_SATURATION    =12, //!< Saturation of the image (only for cameras).
       CAP_PROP_HUE           =13, //!< Hue of the image (only for cameras).
       CAP_PROP_GAIN          =14, //!< Gain of the image (only for those cameras that support).
       CAP_PROP_EXPOSURE      =15, //!< Exposure (only for those cameras that support).
       CAP_PROP_CONVERT_RGB   =16, //!< Boolean flags indicating whether images should be converted to RGB.
       CAP_PROP_WHITE_BALANCE_BLUE_U =17, //!< Currently unsupported.
       CAP_PROP_RECTIFICATION =18, //!< Rectification flag for stereo cameras (note: only supported by DC1394 v 2.x backend currently).
       CAP_PROP_MONOCHROME    =19,
       CAP_PROP_SHARPNESS     =20,
       CAP_PROP_AUTO_EXPOSURE =21, //!< DC1394: exposure control done by camera, user can adjust reference level using this feature.
       CAP_PROP_GAMMA         =22,
       CAP_PROP_TEMPERATURE   =23,
       CAP_PROP_TRIGGER       =24,
       CAP_PROP_TRIGGER_DELAY =25,
       CAP_PROP_WHITE_BALANCE_RED_V =26,
       CAP_PROP_ZOOM          =27,
       CAP_PROP_FOCUS         =28,
       CAP_PROP_GUID          =29,
       CAP_PROP_ISO_SPEED     =30,
       CAP_PROP_BACKLIGHT     =32,
       CAP_PROP_PAN           =33,
       CAP_PROP_TILT          =34,
       CAP_PROP_ROLL          =35,
       CAP_PROP_IRIS          =36,
       CAP_PROP_SETTINGS      =37, //!< Pop up video/camera filter dialog (note: only supported by DSHOW backend currently. The property value is ignored)
       CAP_PROP_BUFFERSIZE    =38,
       CAP_PROP_AUTOFOCUS     =39,
       CAP_PROP_SAR_NUM       =40, //!< Sample aspect ratio: num/den (num)
       CAP_PROP_SAR_DEN       =41, //!< Sample aspect ratio: num/den (den)
       CAP_PROP_BACKEND       =42, //!< Current backend (enum VideoCaptureAPIs). Read-only property
       CAP_PROP_CHANNEL       =43, //!< Video input or Channel Number (only for those cameras that support)
       CAP_PROP_AUTO_WB       =44, //!< enable/ disable auto white-balance
       CAP_PROP_WB_TEMPERATURE=45, //!< white-balance color temperature
#ifndef CV_DOXYGEN
       CV__CAP_PROP_LATEST
#endif
     };

其实参数取值范围很重要,看来我还是要查找资料。下面是别人的一个例子。

capture.set(CV_CAP_PROP_FPS, 30);//帧率 帧/秒

capture.set(CV_CAP_PROP_BRIGHTNESS, 1);//亮度 

capture.set(CV_CAP_PROP_CONTRAST,40);//对比度 40

capture.set(CV_CAP_PROP_SATURATION, 50);//饱和度 50

capture.set(CV_CAP_PROP_HUE, 50);//色调 50

capture.set(CV_CAP_PROP_EXPOSURE, 50);//曝光 50 获取摄像头参数

要核实参数范围,我们用一个现成的软件,比如windows 的相机,或者AMcap,linux下的guvcview,cheese。

在设置好参数后,我们的python 程序获取他们的参数,是个比较好的办法。

然后用python运行获取所有参数:

for i in range(49):
    if abs(cap.get(i)+1)>0.001:
        print("{}.parameter={}".format(i,cap.get(i)))

我的运行结果如下:

ipdb> 0.parameter=0.0
1.parameter=0.0
3.parameter=1280.0
4.parameter=720.0
5.parameter=30.0
6.parameter=842094158.0
9.parameter=0.0
10.parameter=0.0
11.parameter=0.0
12.parameter=64.0
13.parameter=0.0
14.parameter=0.0
16.parameter=1.0
20.parameter=2.0
22.parameter=100.0
23.parameter=4600.0
32.parameter=3.0
40.parameter=1.0
41.parameter=1.0
42.parameter=1400.0

在不知道参数范围的情况下调整参数比较盲目。

下面看我用Amp 的参数调整对话框,大致可以看出参数调整的范围。

最后

以上就是自觉万宝路为你收集整理的Python 下opencv 应用: 摄像头参数设置的全部内容,希望文章能够帮你解决Python 下opencv 应用: 摄像头参数设置所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(50)

评论列表共有 0 条评论

立即
投稿
返回
顶部