采集图片显示视频:
#include <iostream> #include <opencv2/opencv.hpp> using namespace std; using namespace cv; int main () { CvCapture *cap = cvCaptureFromCAM(0); //CvCapture* capture = cvCaptureFromAVI("infile.avi"); IplImage* img; cvNamedWindow("showImg"); while(1) { img = cvQueryFrame(cap); if (!img) break; cvShowImage("showImg", img); char key = cvWaitKey(40); if (key == 27) break; } cvDestroyWindow("showImg"); cvReleaseCapture(&cap); return 0; }
按s键将图片存储到目录e:/src/cam/下
#include <iostream> #include <opencv2/opencv.hpp> using namespace std; using namespace cv; char path[64]; int main () { char num[8] = {0}; int i = 0; CvCapture *cap = cvCaptureFromCAM(0); IplImage* img; cvNamedWindow("showImg"); while(1) { img = cvQueryFrame(cap); if (!img) break; cvShowImage("showImg", img); char key = cvWaitKey(40); if (key == 27) break; if (key == 's') { itoa(i++, num, 10); strcpy(path, "e:/src/cam/img_"); strcat(path,num); strcat(path, ".bmp"); cvSaveImage(path, img); cout<< path <<endl; } } cvDestroyWindow("showImg"); cvReleaseCapture(&cap); return 0; }