从摄像头批量截取图片

 1 // hand gesture sampler.cpp : Defines the entry point for the console application.
 2 
 3 //
 4 #include "stdafx.h"
 5 #include <string>
 6 #include <iostream>
 7 
 8 #include <opencv2\opencv.hpp>
 9 
10 using namespace cv;
11 using namespace std;
12 
13 Mat src, src2Save;
14 char cfileName[1024];
15 string sfileName = "";
16 Size outputSize = Size(320, 240);
17 
18 template <typename T> string NumberToString ( T Number );
19 void help();
20 
21 
22 int main()
23 {
24     help();
25 
26     namedWindow("monitor", 1);
27 
28     VideoCapture cap(0);
29 
30     if(cap.isOpened())
31     {
32         int c = 0;
33         int num = 0;
34         bool sampleState = false;
35 
36         //main loop
37         while( c != 27)
38         {
39             if(c == 98) sampleState = true;
40             if(c == 115) sampleState = false;
41 
42             //get webcam image
43             cap >> src;
44 
45             //resize image to 320*240
46             if(src.cols > 320 || src.rows > 240) resize(src, src2Save, outputSize, 0.5, 0.5, 1);
47 
48             //show image
49             imshow("monitor", src2Save);
50 
51             //save image            
52             sfileName = "f:/documents/Downloads/hand gesture pictures/" + NumberToString(num) + ".png";
53             if(sampleState) 
54             { 
55                 imwrite(sfileName, src2Save); 
56                 num ++;
57                 cout<<"\nCollected Sample Number - " << num << endl;
58             }            
59 
60             //exit loop
61             c = waitKey(10);
62         }
63     }
64     else
65         //end app
66         return -1;
67 
68     return 0;
69 }
70 
71 template <typename T>
72   string NumberToString ( T Number )
73   {
74      ostringstream ss;
75      ss << Number;
76      return ss.str();
77   }
78 
79 void help()
80 {
81     printf(
82         "========================\n"
83         "========================\n"
84         "Programme to collect a good number of hand gestures from video streams.\n"
85         "Usage:\n"
86         "(s)top - stop sampling\n"
87         "(b)egin - begin sampling\n"
88         "ESC - exit the programme\n"
89         "==========================\n"
90         "==========================\n"
91         );
92 }
原文地址:https://www.cnblogs.com/xiangtailiang/p/2490397.html