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()
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 .
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)
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
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
};
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 :
for i in range(49):
if abs(cap.get(i)+1)>0.001:
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 .