opencv启动USB摄像头并保存图片

来源:互联网 发布:那个卡盟主站域名好用 编辑:程序博客网 时间:2024/04/29 21:02

网上有许多打开摄像头的程序,但是大都是针对旧版本的opencv,并且并不带有截取图片的功能,结合大牛的博客和书本教材,写了个采集摄像头样本代码。功能不是很完善,有瑕疵,大牛勿喷!

#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/core/core.hpp>
#include<iostream>
#include <string>
#include <sstream>
using namespace cv;
using namespace std;
int main()
{
VideoCapture capture(0);
//设置摄像头
capture.set( CV_CAP_PROP_FRAME_WIDTH,1280);
capture.set( CV_CAP_PROP_FRAME_HEIGHT,720);
//确认是否成功打开摄像头
if(!capture.isOpened())
{
cout<<"打开摄像头失败,退出"<<endl;
exit(-1);
}
int i=0;
std::string b="yangben";//保存图片名称
std::string ext=".jpg";
Mat frame(Size(1280,720),CV_8UC3);
while (1)
{

       if (!capture.read(frame))
    break;
capture>>frame;
if (!frame.empty())   //很重要,确保摄像头已启动

 {
  imshow("window", frame);
std::string name(b);
std::ostringstream ss; 
ss << i; 
name+= ss.str();
i++;
name+=ext;
std::cout << name <<std::endl;
cv::imwrite(name,frame);
}
if( waitKey(30)>=0 ) break;//延时时间可调
}
return 0;
}

0 1
原创粉丝点击