1. OpenCV:VideoCapture同时打开两个摄像头
#include <opencv2/opencv.hpp>
#include<iostream>
using namespace cv;
using namespace std;
int main()
{
//initialize and allocate memory to load the video stream from camera
VideoCapture camera0(1);
camera0.set(CAP_PROP_FRAME_WIDTH,320);
camera0.set(CAP_PROP_FRAME_HEIGHT,240);
VideoCapture camera1(0);
camera1.set(CAP_PROP_FRAME_WIDTH,320);
camera1.set(CAP_PROP_FRAME_HEIGHT,240);
if( !camera0.isOpened() ) return 1;
if( !camera1.isOpened() ) return 1;
while(true) {
//grab and retrieve each frames of the video sequentially
Mat3b frame0;
camera0 >> frame0;
Mat3b frame1;
camera1 >> frame1;
imshow("Video0", frame0);
imshow("Video1", frame1);
//std::cout << frame1.rows() << std::endl;
//wait for 40 milliseconds
int c = waitKey(20);
//exit the loop if user press "Esc" key (ASCII value of "Esc" is 27)
if(27 == char(c)) break;
}
return 0;
}
[注]:
1. 分辨率为320*240,能够正常显示,分辨率太高,只有一个摄像头能正常显示,另一个显示灰度图。原因是USB的带宽有限。
2. 摄像头的序号在opencv中是采用降序的形式,才能正确读取多个摄像头,否则就不能。
3. VideoCapture类有两种用法,一种是VideoCapture(const string& filename)用来打开视频文件,一种是VideoCapture(int device)用来打开设备。
2. OpenCV VideoCapture 设置和获取摄像头参数
适用于VideoCapture打开的摄像头
VideoCapture capture(0);
设置摄像头参数 不要随意修改
capture.set(CV_CAP_PROP_FRAME_WIDTH, 1080);//宽度
capture.set(CV_CAP_PROP_FRAME_HEIGHT, 960);//高度
capture.set(CV_CAP_PROP_FPS, 30);//帧率 帧/秒
capture.set(CV_CAP_PROP_BRIGHTNESS, 1);//亮度 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
获取摄像头参数
capture.get(CV_CAP_PROP_FRAME_WIDTH);
capture.get(CV_CAP_PROP_FRAME_HEIGHT);
capture.get(CV_CAP_PROP_FPS);
capture.get(CV_CAP_PROP_BRIGHTNESS);
capture.get(CV_CAP_PROP_CONTRAST);
capture.get(CV_CAP_PROP_SATURATION);
capture.get(CV_CAP_PROP_HUE);
capture.get(CV_CAP_PROP_EXPOSURE);
源代码
/**
@brief OpenCV摄像头和视频属性操作
@author
@copyright -
@version 1.0
@data
@note -
*/
#include <iostream>
#include "opencv2/opencv.hpp"
using namespace std;
using namespace cv;
const string window_name = "用户界面";
#define USE_CAMERA
//#define USE_VIDEO
int main()
{
Mat frame;
double brightness = 0; //亮度
double contrast = 0; //对比度
double saturation = 0; //饱和度
double hue = 0; //色调
double gain = 0; //增益
double exposure = 0; //曝光
double white_balance = 0; //白平衡
double pos_msec = 0; //当前视频位置(ms)
double pos_frame = 0; //从0开始下一帧的索引
double pos_avi_ratio = 0; //视频中的相对位置(范围为0.0到1.0)
double frame_width = 0; //视频帧的像素宽度
double frame_height = 0; //视频帧的像素高度
double fps = 0; //帧速率
double frame_count = 0; //视频总帧数
double video_duration = 0.00; //视频时长
double format = 0; //格式
#ifdef USE_VIDEO
const string file_name = "201910915314.avi";
VideoCapture capture(file_name);
frame_width = capture.get(cv::CAP_PROP_FRAME_WIDTH);
frame_height = capture.get(cv::CAP_PROP_FRAME_HEIGHT);
fps = capture.get(cv::CAP_PROP_FPS);
frame_count = capture.get(cv::CAP_PROP_FRAME_COUNT);
format = capture.get(cv::CAP_PROP_FORMAT);
pos_avi_ratio = capture.get(cv::CAP_PROP_POS_AVI_RATIO);
video_duration = frame_count / fps;
cout << "---------------------------------------------" << endl;
cout << "视频中的相对位置(范围为0.0到1.0):" << pos_avi_ratio << endl;
cout << "视频帧的像素宽度:" << frame_width << endl;
cout << "视频帧的像素高度:" << frame_height << endl;
cout << "录制视频的帧速率(帧/秒):" << fps << endl;
cout << "视频文件总帧数:" << frame_count << endl;
cout << "图像的格式:" << format << endl;
cout << "视频时长:" << video_duration << endl;
cout << "---------------------------------------------" << endl;
#endif // USE_VIDEO
#ifdef USE_CAMERA
VideoCapture capture(0);
brightness = capture.get(cv::CAP_PROP_BRIGHTNESS);
contrast= capture.get(cv::CAP_PROP_CONTRAST);
saturation = capture.get(cv::CAP_PROP_SATURATION);
hue = capture.get(cv::CAP_PROP_HUE);
gain = capture.get(cv::CAP_PROP_GAIN);
exposure = capture.get(cv::CAP_PROP_EXPOSURE);
white_balance = capture.get(cv::CAP_PROP_WHITE_BALANCE_BLUE_U);
std::cout << "---------------------------------------------" << endl;
std::cout << "摄像头亮度:" << brightness << endl;
std::cout << "摄像头对比度:" << contrast << endl;
std::cout << "摄像头饱和度:" << saturation << endl;
std::cout << "摄像头色调:" << hue << endl;
std::cout << "摄像头增益:" << gain << endl;
std::cout << "摄像头曝光度:" << exposure << endl;
std::cout << "摄像头白平衡:" << white_balance << endl;
std::cout << "---------------------------------------------" << endl;
#endif // USE_CAMERA
namedWindow(window_name,WINDOW_AUTOSIZE);
while (capture.isOpened())
{
capture >> frame;
#ifdef USE_VIDEO
pos_msec = capture.get(cv::CAP_PROP_POS_MSEC);
pos_frame = capture.get(cv::CAP_PROP_POS_FRAMES);
pos_avi_ratio = capture.get(cv::CAP_PROP_POS_AVI_RATIO);
cout << "---------------------------------------------" << endl;
cout << "视频文件中当前位置(ms):" << pos_msec << endl;
cout << "从0开始下一帧的索引:" << pos_frame << endl;
cout << "视频中的相对位置(范围为0.0到1.0):" << pos_avi_ratio << endl;
cout << "---------------------------------------------" << endl;
#endif // USE_VIDEO
imshow(window_name, frame);
if (waitKey(60)==27)
{
break;
}
}
capture.release();
destroyAllWindows();
return 0;
}
3. opencv 出现 “VIDIOC_STREAMON: No space left on device” 错误 解决方法
现象:
今天在用opencv python 打开双摄像头时报一个错误: VIDIOC_STREAMON: No space left on device,设备上没有空间
1. 之前用opencv-3.2.0 版本没有报这个问题,现在使用新版本opencv-3.4.2 就出现这个问题。
2. 把摄像头的分辨率改小到320*240后可以正常使用双摄像头。
原因是:单个摄像头占用了USB的全部带宽。
解决方法:
在打开摄像头时添加一个语句:
capture1.set(cv2.CAP_PROP_FOURCC, cv2.VideoWriter_fourcc(*"MJPG"))
代码:
capture1 = cv2.VideoCapture(2)
capture2 = cv2.VideoCapture(1)
capture1.set(cv2.CAP_PROP_FOURCC, cv2.VideoWriter_fourcc(*"MJPG"))
capture2.set(cv2.CAP_PROP_FOURCC, cv2.VideoWriter_fourcc(*"MJPG"))
# setting resolution
capture1.set(cv2.CAP_PROP_FRAME_WIDTH, 640)
capture1.set(cv2.CAP_PROP_FRAME_HEIGHT, 480)
capture2.set(cv2.CAP_PROP_FRAME_WIDTH, 640)
capture2.set(cv2.CAP_PROP_FRAME_HEIGHT, 480)
if (capture.isOpened())
capture.release(); //decide whether capture is already opened; if so,close it
capture.open(0); //open the default camera
capture.set(CAP_PROP_FOURCC, VideoWriter::fourcc('M', 'J', 'P', 'G')) ;
capture.set(CAP_PROP_FPS, 60);
capture.set(CAP_PROP_FRAME_WIDTH, ImgWidth);
capture.set(CAP_PROP_FRAME_HEIGHT, ImgHeight);