OpenCV 如何打开摄像头获取图像数据?
代码运行环境:
Qt 5.9.1 msvc2015 32bit
OpenCV 3.3.0
#include "include/opencv2/opencv.hpp"
#include <iostream>
using namespace std;
using namespace cv;
int main(int argc, char *argv[])
{
#define 第一个摄像头 0
#define 第二个摄像头 1
#define 第三个摄像头 2
#define Esc 27
//#define 轮廓检测
//#define 灰度化
#define 旋转90度
cv::VideoCapture capture;
capture.open(第一个摄像头);
if(!capture.isOpened())
{
return;
}
Mat frame;
#ifdef 轮廓检测
Mat cannyDst;
#endif
// capture.set(CV_CAP_PROP_FRAME_WIDTH, 1280);
// capture.set(CV_CAP_PROP_FRAME_HEIGHT, 720);
// capture.set(CV_CAP_PROP_FRAME_WIDTH, 640);
// capture.set(CV_CAP_PROP_FRAME_HEIGHT, 480);
// capture.set(CAP_PROP_FRAME_WIDTH,320);
// capture.set(CAP_PROP_FRAME_HEIGHT,240);
// capture.set(CAP_PROP_EXPOSURE,10);//设置曝光率
// qDebug()<<capture.get(CV_CAP_PROP_FRAME_WIDTH)<<"x"<<capture.get(CV_CAP_PROP_FRAME_HEIGHT);
double FPS, T;
while(true)
{
T = (double)cv::getTickCount();
capture >> frame;
if(frame.empty())
break;
T = ((double)cv::getTickCount() - T) / cv::getTickFrequency();
FPS = 1.0 / T;
putText(frame, "FPS:"+std::to_string(FPS) , Point(30, 80), FONT_HERSHEY_TRIPLEX ,2, cvScalar(0, 0, 255, 0));//int(frame.rows*0.9)
// qDebug()<<frame.cols<<"x"<<frame.rows;
#ifdef 旋转90度
cv::transpose(frame,frame);
flip(frame, frame, 1);
#endif
#ifdef 灰度化
cv::cvtColor(frame, frame, CV_BGR2GRAY);
#endif
#ifdef 轮廓检测
cv::Canny(frame, cannyDst, 0, 50, 3);
cv::imshow("摄像头",cannyDst);
#else
cv::imshow("摄像头",frame);
#endif
//退出循环
if (waitKey(1) == Esc)
{
break;
}
}
}