当前位置:   article > 正文

OpenCV设置和获取摄像头参数_opencv 设置摄像头 对比度

opencv 设置摄像头 对比度

适用于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);

 

获取视频参数


capture.get(CV_CAP_PROP_FRAME_COUNT);//视频帧数

 

源代码

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

打印结果:

 

声明:本文内容由网友自发贡献,转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号