勇哥的测试平台是vs2019+opencv3.1,下面的代码可用。
功能是打开usb摄像头,并保存每帧图片。
#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; }
本文出自勇哥的网站《少有人走的路》wwww.skcircle.com,转载请注明出处!讨论可扫码加群:


