适用于VideoCapture打开的摄像头
VideoCapture capture(0);
设置摄像头参数 不要随意修改
1 capture.set(CV_CAP_PROP_FRAME_WIDTH, 1080);//宽度
2 capture.set(CV_CAP_PROP_FRAME_HEIGHT, 960);//高度
3 capture.set(CV_CAP_PROP_FPS, 30);//帧率 帧/秒
4 capture.set(CV_CAP_PROP_BRIGHTNESS, 1);//亮度 1
5 capture.set(CV_CAP_PROP_CONTRAST,40);//对比度 40
6 capture.set(CV_CAP_PROP_SATURATION, 50);//饱和度 50
7 capture.set(CV_CAP_PROP_HUE, 50);//色调 50
8 capture.set(CV_CAP_PROP_EXPOSURE, 50);//曝光 50
获取摄像头参数
1 capture.get(CV_CAP_PROP_FRAME_WIDTH);
2 capture.get(CV_CAP_PROP_FRAME_HEIGHT);
3 capture.get(CV_CAP_PROP_FPS);
4 capture.get(CV_CAP_PROP_BRIGHTNESS);
5 capture.get(CV_CAP_PROP_CONTRAST);
6 capture.get(CV_CAP_PROP_SATURATION);
7 capture.get(CV_CAP_PROP_HUE);
8 capture.get(CV_CAP_PROP_EXPOSURE);
源代码
1 /**
2 @brief OpenCV摄像头和视频属性操作
3 @author
4 @copyright -
5 @version 1.0
6 @data
7 @note -
8 */
9 #include <iostream>
10 #include "opencv2/opencv.hpp"
11
12 using namespace std;
13 using namespace cv;
14
15 const string window_name = "用户界面";
16
17 #define USE_CAMERA
18 //#define USE_VIDEO
19
20 int main()
21 {
22 Mat frame;
23
24 double brightness = 0; //亮度
25 double contrast = 0; //对比度
26 double saturation = 0; //饱和度
27 double hue = 0; //色调
28 double gain = 0; //增益
29 double exposure = 0; //曝光
30 double white_balance = 0; //白平衡
31
32 double pos_msec = 0; //当前视频位置(ms)
33 double pos_frame = 0; //从0开始下一帧的索引
34 double pos_avi_ratio = 0; //视频中的相对位置(范围为0.0到1.0)
35 double frame_width = 0; //视频帧的像素宽度
36 double frame_height = 0; //视频帧的像素高度
37 double fps = 0; //帧速率
38 double frame_count = 0; //视频总帧数
39 double video_duration = 0.00; //视频时长
40 double format = 0; //格式
41
42 #ifdef USE_VIDEO
43 const string file_name = "201910915314.avi";
44 VideoCapture capture(file_name);
45
46 frame_width = capture.get(cv::CAP_PROP_FRAME_WIDTH);
47 frame_height = capture.get(cv::CAP_PROP_FRAME_HEIGHT);
48 fps = capture.get(cv::CAP_PROP_FPS);
49 frame_count = capture.get(cv::CAP_PROP_FRAME_COUNT);
50 format = capture.get(cv::CAP_PROP_FORMAT);
51 pos_avi_ratio = capture.get(cv::CAP_PROP_POS_AVI_RATIO);
52 video_duration = frame_count / fps;
53
54 cout << "---------------------------------------------" << endl;
55 cout << "视频中的相对位置(范围为0.0到1.0):" << pos_avi_ratio << endl;
56 cout << "视频帧的像素宽度:" << frame_width << endl;
57 cout << "视频帧的像素高度:" << frame_height << endl;
58 cout << "录制视频的帧速率(帧/秒):" << fps << endl;
59 cout << "视频文件总帧数:" << frame_count << endl;
60 cout << "图像的格式:" << format << endl;
61 cout << "视频时长:" << video_duration << endl;
62 cout << "---------------------------------------------" << endl;
63 #endif // USE_VIDEO
64
65 #ifdef USE_CAMERA
66 VideoCapture capture(0);
67 brightness = capture.get(cv::CAP_PROP_BRIGHTNESS);
68 contrast= capture.get(cv::CAP_PROP_CONTRAST);
69 saturation = capture.get(cv::CAP_PROP_SATURATION);
70 hue = capture.get(cv::CAP_PROP_HUE);
71 gain = capture.get(cv::CAP_PROP_GAIN);
72 exposure = capture.get(cv::CAP_PROP_EXPOSURE);
73 white_balance = capture.get(cv::CAP_PROP_WHITE_BALANCE_BLUE_U);
74
75 std::cout << "---------------------------------------------" << endl;
76 std::cout << "摄像头亮度:" << brightness << endl;
77 std::cout << "摄像头对比度:" << contrast << endl;
78 std::cout << "摄像头饱和度:" << saturation << endl;
79 std::cout << "摄像头色调:" << hue << endl;
80 std::cout << "摄像头增益:" << gain << endl;
81 std::cout << "摄像头曝光度:" << exposure << endl;
82 std::cout << "摄像头白平衡:" << white_balance << endl;
83 std::cout << "---------------------------------------------" << endl;
84 #endif // USE_CAMERA
85
86 namedWindow(window_name,WINDOW_AUTOSIZE);
87 while (capture.isOpened())
88 {
89 capture >> frame;
90
91 #ifdef USE_VIDEO
92 pos_msec = capture.get(cv::CAP_PROP_POS_MSEC);
93 pos_frame = capture.get(cv::CAP_PROP_POS_FRAMES);
94 pos_avi_ratio = capture.get(cv::CAP_PROP_POS_AVI_RATIO);
95 cout << "---------------------------------------------" << endl;
96 cout << "视频文件中当前位置(ms):" << pos_msec << endl;
97 cout << "从0开始下一帧的索引:" << pos_frame << endl;
98 cout << "视频中的相对位置(范围为0.0到1.0):" << pos_avi_ratio << endl;
99 cout << "---------------------------------------------" << endl;
100 #endif // USE_VIDEO
101
102 imshow(window_name, frame);
103 if (waitKey(60)==27)
104 {
105 break;
106 }
107 }
108 capture.release();
109 destroyAllWindows();
110 return 0;
111 }
打印结果: