python opencv 摄像头亮度_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 应用: 摄像头参数设置相关推荐
- Python中Button按钮组件常用的属性及参数设置
Python中Button按钮组件常用的属性及参数设置 本篇文章中小编给大家介绍Button按钮组件的相关常用的属性以及参数的设置. 一. 常用属性使用语法 变量=Button(父容器(根窗口),参数 ...
- python表情识别程序_Python+Dlib+Opencv实现人脸采集并表情判别功能的代码
一.dlib以及opencv-python库安装 介于我使用的是jupyter notebook,所以在安装dlib和opencv-python时是在 这个命令行安装的 dlib安装方法: 1.若可以 ...
- python调整图片亮度_python 调整图片亮度的示例
实现效果 实现代码 import matplotlib.pyplot as plt from skimage import io file_name='D:/2020121173119242.png' ...
- python调整图片亮度_python调整图片亮度的示例
这篇文章我们来讲一下在网站建设中,python调整图片亮度的示例.本文对大家进行网站开发设计工作或者学习都有一定帮助,下面让我们进入正文. 实现效果 实现代码 import matplotlib.py ...
- python pdb调试快捷键_python pdb调试以及sublime3快捷键设置
python pdb调试以及sublime3快捷键设置 pdb调试 如果对gdb比较熟悉的话,pdb就很容易上手.以一个demo快速了解常用的调试命令. def test(a): while True ...
- python储存与读取图片_Python下opencv使用笔记(一)(图像简单读取、显示与储存)...
写在之前 从去年開始关注python这个软件,途中间间断断看与学过一些关于python的东西.感觉python确实是一个简单优美.easy上手的脚本编程语言,众多的第三方库使得python异常的强大. ...
- python调用opencv处理视频_Python调用OpenCV读写视频
最近因为经常对视频进行操作,所以记录下Python用opencv来读写视频的方法. 一.opencv读视频 python调用opencv来读视频比较简单,可以直接调用cv2.VideoCapture来 ...
- python 视频人脸替换_Python基于OpenCV实现视频的人脸检测
本文实例为大家分享了基于OpenCV实现视频的人脸检测具体代码,供大家参考,具体内容如下 前提条件 1.摄像头 2.已安装Python和OpenCV3 代码 import cv2 import sys ...
- python视频人脸检测_Python基于OpenCV实现视频的人脸检测
本文实例为大家分享了基于OpenCV实现视频的人脸检测具体代码,供大家参考,具体内容如下 前提条件 1.摄像头 2.已安装Python和OpenCV3 代码 import cv2 import sys ...
最新文章
- LINUX创建www的用户组和用户,并且不允许登录权限:
- (转载)浅析Hadoop文件格式
- 程序员晒工资单,还是大厂香!据说大多数3年,35K还少了?
- TCP/IP / 网关和路由器的区别
- look look C#7
- 即将全部下架!支付宝等平台不许再卖互联网存款产品
- ### Error building SqlSession. ### Cause: org.apache.ibatis.builder.BuilderException: Error creating
- UITableView单元格选择颜色?
- 学习总结 java 创建及其练习
- linux把 root文件夹删除文件,在Linux下删除顽固文件和目录
- cogs908. 校园网
- Django DTL 与verbatim
- 《剑指0ffer》刷题笔记 -01 二维数组中的查找
- 2021年什么邮箱最好用,这款电子邮箱让你的工作效率翻倍!
- 哪一种编程语言适合人工智能
- 计算机图形学孔令德基础知识,欢迎访问 孔令德计算机图形学精品资源共享课课程网站...
- 红米k40和红米k40pro的区别 哪个好
- 利用IDEA将项目打包(两种方法)
- syn flood 攻击 c 语言源代码,以太网模拟syn flood攻击
- 【Qzone】向来缘浅 奈何情深 第三步 编译Android内核源代码
热门文章
- P - Reduced ID Numbers
- include详解 shell_thinkphp诸多限制条件下如何getshell详解
- 传奇4,掉线重连,无法连接服务器等情况解决方案
- ft232电路ttl_FT232R 的串行 UART 接口解析
- 【iOS】—— 内存的五大分区
- layui分页limit不显示_layui表格分页不生效怎么解决?
- 数据处理:16进制补码 转换为 10进制数值
- 手机如何查看电脑上的html,如何在电脑上查看手机wps内的文档
- unity异步从外部文件加载音频和图片
- Grafana + Prometheus + Exporter (一)