Python opencv camera parameters

ShellCollector 2020-11-13 09:58:07
python opencv camera parameters


In order to get a good picture effect , We need to set the camera parameters .

If the video stream is cap, Then the setting parameter is cap.set( Parameter number , Parameters )

The function that gets the parameter value is   cap.get( Parameter number )

Let's take a look at an example of camera parameter setting reading , Set the code first 3 Parameters , And then read this 3 Parameters .

import cv2
cap = cv2.VideoCapture(0)
# Set the parameters first , And then read the parameters
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)
# Press ESC Key to exit
key = cv2.waitKey(10)
if key == 27:
break
cv2.destroyAllWindows()
cv2.VideoCapture(0).release()
  1.  

  My program runs and the result is :

width=1280.0
height=720.0
exposure=-1.0

wide , high , The settings are valid , The exposure setting is invalid .

What parameters can be set for the camera ? How about the corresponding relationship of parameter numbers , Look at the list below .

  1. 0. CV_CAP_PROP_POS_MSEC Current position of the video file in milliseconds.

  2. 1. CV_CAP_PROP_POS_FRAMES 0-based index of the frame to be decoded/captured next.

  3. 2. CV_CAP_PROP_POS_AVI_RATIO Relative position of the video file

  4. 3. CV_CAP_PROP_FRAME_WIDTH Width of the frames in the video stream.

  5. 4. CV_CAP_PROP_FRAME_HEIGHT Height of the frames in the video stream.

  6. 5. CV_CAP_PROP_FPS Frame rate.

  7. 6. CV_CAP_PROP_FOURCC 4-character code of codec.

  8. 7. CV_CAP_PROP_FRAME_COUNT Number of frames in the video file.

  9. 8. CV_CAP_PROP_FORMAT Format of the Mat objects returned by retrieve() .

  10. 9. CV_CAP_PROP_MODE Backend-specific value indicating the current capture mode.

  11. 10. CV_CAP_PROP_BRIGHTNESS Brightness of the image (only for cameras).

  12. 11. CV_CAP_PROP_CONTRAST Contrast of the image (only for cameras).

  13. 12. CV_CAP_PROP_SATURATION Saturation of the image (only for cameras).

  14. 13. CV_CAP_PROP_HUE Hue of the image (only for cameras).

  15. 14. CV_CAP_PROP_GAIN Gain of the image (only for cameras).

  16. 15. CV_CAP_PROP_EXPOSURE Exposure (only for cameras).

  17. 16. CV_CAP_PROP_CONVERT_RGB Boolean flags indicating whether images should be converted to RGB.

  18. 17. CV_CAP_PROP_WHITE_BALANCE Currently unsupported

  19. 18. CV_CAP_PROP_RECTIFICATION Rectification flag for stereo cameras (note: only supported by DC1394 v 2.x backend currently)

Here are the parameters that can be set , The cameras are different , Some parameters may not be set . 

General read returns -1 It's ineffective , Now let's take a look at a code that detects valid and invalid data , It's the code above

 import cv2
# Select the camera number , Generally from 0 Start
cap = cv2.VideoCapture(0)
# Set the parameters first , And then read the parameters
for i in range(47):
print("No.={} parameter={}".format(i,cap.get(i)))
while True:
ret, img = cap.read()
cv2.imshow("input", img)
# Press ESC Key to exit
key = cv2.waitKey(10)
if key == 27:
break
cv2.destroyAllWindows()
cv2.VideoCapture(0).release()

The result of the operation is :

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

You can see which parameters of my camera can be set , Not for -1 Of .

It's easy to get the numbers wrong in the program , You can look better in the following ways , It's not easy to make a mistake .

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

The above table is incomplete , But let's look at the basics opencv What's in it :

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

 
  1. enum VideoCaptureProperties {

  2. CAP_PROP_POS_MSEC =0, //!< Current position of the video file in milliseconds.

  3. CAP_PROP_POS_FRAMES =1, //!< 0-based index of the frame to be decoded/captured next.

  4. CAP_PROP_POS_AVI_RATIO =2, //!< Relative position of the video file: 0=start of the film, 1=end of the film.

  5. CAP_PROP_FRAME_WIDTH =3, //!< Width of the frames in the video stream.

  6. CAP_PROP_FRAME_HEIGHT =4, //!< Height of the frames in the video stream.

  7. CAP_PROP_FPS =5, //!< Frame rate.

  8. CAP_PROP_FOURCC =6, //!< 4-character code of codec. see VideoWriter::fourcc .

  9. CAP_PROP_FRAME_COUNT =7, //!< Number of frames in the video file.

  10. CAP_PROP_FORMAT =8, //!< Format of the %Mat objects returned by VideoCapture::retrieve().

  11. CAP_PROP_MODE =9, //!< Backend-specific value indicating the current capture mode.

  12. CAP_PROP_BRIGHTNESS =10, //!< Brightness of the image (only for those cameras that support).

  13. CAP_PROP_CONTRAST =11, //!< Contrast of the image (only for cameras).

  14. CAP_PROP_SATURATION =12, //!< Saturation of the image (only for cameras).

  15. CAP_PROP_HUE =13, //!< Hue of the image (only for cameras).

  16. CAP_PROP_GAIN =14, //!< Gain of the image (only for those cameras that support).

  17. CAP_PROP_EXPOSURE =15, //!< Exposure (only for those cameras that support).

  18. CAP_PROP_CONVERT_RGB =16, //!< Boolean flags indicating whether images should be converted to RGB.

  19. CAP_PROP_WHITE_BALANCE_BLUE_U =17, //!< Currently unsupported.

  20. CAP_PROP_RECTIFICATION =18, //!< Rectification flag for stereo cameras (note: only supported by DC1394 v 2.x backend currently).

  21. CAP_PROP_MONOCHROME =19,

  22. CAP_PROP_SHARPNESS =20,

  23. CAP_PROP_AUTO_EXPOSURE =21, //!< DC1394: exposure control done by camera, user can adjust reference level using this feature.

  24. CAP_PROP_GAMMA =22,

  25. CAP_PROP_TEMPERATURE =23,

  26. CAP_PROP_TRIGGER =24,

  27. CAP_PROP_TRIGGER_DELAY =25,

  28. CAP_PROP_WHITE_BALANCE_RED_V =26,

  29. CAP_PROP_ZOOM =27,

  30. CAP_PROP_FOCUS =28,

  31. CAP_PROP_GUID =29,

  32. CAP_PROP_ISO_SPEED =30,

  33. CAP_PROP_BACKLIGHT =32,

  34. CAP_PROP_PAN =33,

  35. CAP_PROP_TILT =34,

  36. CAP_PROP_ROLL =35,

  37. CAP_PROP_IRIS =36,

  38. CAP_PROP_SETTINGS =37, //!< Pop up video/camera filter dialog (note: only supported by DSHOW backend currently. The property value is ignored)

  39. CAP_PROP_BUFFERSIZE =38,

  40. CAP_PROP_AUTOFOCUS =39,

  41. CAP_PROP_SAR_NUM =40, //!< Sample aspect ratio: num/den (num)

  42. CAP_PROP_SAR_DEN =41, //!< Sample aspect ratio: num/den (den)

  43. CAP_PROP_BACKEND =42, //!< Current backend (enum VideoCaptureAPIs). Read-only property

  44. CAP_PROP_CHANNEL =43, //!< Video input or Channel Number (only for those cameras that support)

  45. CAP_PROP_AUTO_WB =44, //!< enable/ disable auto white-balance

  46. CAP_PROP_WB_TEMPERATURE=45, //!< white-balance color temperature

  47. #ifndef CV_DOXYGEN

  48. CV__CAP_PROP_LATEST

  49. #endif

  50. };

In fact, the parameter range is very important , Looks like I'm still looking for information . Here's an example of someone else .

capture.set(CV_CAP_PROP_FPS, 30);// Frame rate frame / second

capture.set(CV_CAP_PROP_BRIGHTNESS, 1);// brightness  

capture.set(CV_CAP_PROP_CONTRAST,40);// Contrast 40

capture.set(CV_CAP_PROP_SATURATION, 50);// saturation 50

capture.set(CV_CAP_PROP_HUE, 50);// tonal 50

capture.set(CV_CAP_PROP_EXPOSURE, 50);// Exposure 50 Get camera parameters

To verify the parameter range , We use a ready-made Software , such as windows The camera , perhaps AMcap,linux Under the guvcview,cheese.

After setting the parameters , our python The program takes their parameters , It's a better way .

And then use python Run to get all parameters :

 
  1. for i in range(49):

  2. if abs(cap.get(i)+1)>0.001:

  3. print("{}.parameter={}".format(i,cap.get(i)))

My running results are as follows :

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

It is blind to adjust parameters without knowing the range of parameters .

版权声明
本文为[ShellCollector]所创,转载请带上原文链接,感谢

  1. 利用Python爬虫获取招聘网站职位信息
  2. Using Python crawler to obtain job information of recruitment website
  3. Several highly rated Python libraries arrow, jsonpath, psutil and tenacity are recommended
  4. Python装饰器
  5. Python实现LDAP认证
  6. Python decorator
  7. Implementing LDAP authentication with Python
  8. Vscode configures Python development environment!
  9. In Python, how dare you say you can't log module? ️
  10. 我收藏的有关Python的电子书和资料
  11. python 中 lambda的一些tips
  12. python中字典的一些tips
  13. python 用生成器生成斐波那契数列
  14. python脚本转pyc踩了个坑。。。
  15. My collection of e-books and materials about Python
  16. Some tips of lambda in Python
  17. Some tips of dictionary in Python
  18. Using Python generator to generate Fibonacci sequence
  19. The conversion of Python script to PyC stepped on a pit...
  20. Python游戏开发,pygame模块,Python实现扫雷小游戏
  21. Python game development, pyGame module, python implementation of minesweeping games
  22. Python实用工具,email模块,Python实现邮件远程控制自己电脑
  23. Python utility, email module, python realizes mail remote control of its own computer
  24. 毫无头绪的自学Python,你可能连门槛都摸不到!【最佳学习路线】
  25. Python读取二进制文件代码方法解析
  26. Python字典的实现原理
  27. Without a clue, you may not even touch the threshold【 Best learning route]
  28. Parsing method of Python reading binary file code
  29. Implementation principle of Python dictionary
  30. You must know the function of pandas to parse JSON data - JSON_ normalize()
  31. Python实用案例,私人定制,Python自动化生成爱豆专属2021日历
  32. Python practical case, private customization, python automatic generation of Adu exclusive 2021 calendar
  33. 《Python实例》震惊了,用Python这么简单实现了聊天系统的脏话,广告检测
  34. "Python instance" was shocked and realized the dirty words and advertisement detection of the chat system in Python
  35. Convolutional neural network processing sequence for Python deep learning
  36. Python data structure and algorithm (1) -- enum type enum
  37. 超全大厂算法岗百问百答(推荐系统/机器学习/深度学习/C++/Spark/python)
  38. 【Python进阶】你真的明白NumPy中的ndarray吗?
  39. All questions and answers for algorithm posts of super large factories (recommended system / machine learning / deep learning / C + + / spark / Python)
  40. [advanced Python] do you really understand ndarray in numpy?
  41. 【Python进阶】Python进阶专栏栏主自述:不忘初心,砥砺前行
  42. [advanced Python] Python advanced column main readme: never forget the original intention and forge ahead
  43. python垃圾回收和缓存管理
  44. java调用Python程序
  45. java调用Python程序
  46. Python常用函数有哪些?Python基础入门课程
  47. Python garbage collection and cache management
  48. Java calling Python program
  49. Java calling Python program
  50. What functions are commonly used in Python? Introduction to Python Basics
  51. Python basic knowledge
  52. Anaconda5.2 安装 Python 库(MySQLdb)的方法
  53. Python实现对脑电数据情绪分析
  54. Anaconda 5.2 method of installing Python Library (mysqldb)
  55. Python implements emotion analysis of EEG data
  56. Master some advanced usage of Python in 30 seconds, which makes others envy it
  57. python爬取百度图片并对图片做一系列处理
  58. Python crawls Baidu pictures and does a series of processing on them
  59. python链接mysql数据库
  60. Python link MySQL database