摄影圈子 hqmana.com

OpenCv调用摄像头拍照并保存图片(C++)

OpenCV调用摄像头拍照并保存图片

实现步骤:
1、打开0号摄像头
2、while循环拍照
3、拍照时顺便保存图片

#include <opencv2/opencv.hpp>
#include <iostream>
using namespace std;
using namespace cv;
//工程目录下的temp文件夹里,与.sln同级的temp文件
string writePath = "../temp/";
int main(int argc, char** argv){
    
 VideoCapture capture(0);
 string name;
 namedWindow("hello", CV_WINDOW_AUTOSIZE);
 int i=0;
 while (1) {
    
  Mat frame;
  capture >> frame;
  if (32 == waitKey(20)) {
    			//空格拍照
   name = writePath + to_string(i)+".jpg";
   imwrite(name, frame);
   cout << name << endl;
   i++;
  }
  if (97 == waitKey(10)) {
    			//'a'退出
   break;
  }
  imshow("hello",frame);
  
 }
 //waitKey(0);
}

版权声明
本文为[大头鑫]所创,转载请带上原文链接,感谢
https://blog.csdn.net/weixin_39147809/article/details/103209325

Scroll to Top