read the USB camera and save by frame
Description
1. After running, press "s" in the "USB Video" window to start saving the image by frame, and press "Q" to finish.
2. The device number is set according to the actual situation, my Computer default camera is 0,USB camera is 3.
3. Since the USB camera initialization is slower than the computer default camera, the sleep () statement is added before the loop, otherwise the "USB Video" window may not be able to read the gray screen. Use the sleep () statement to include the header file windows.h.
void Videocap ()
{
videocapture usbcap (3);
if (!usbcap.isopened ())
{
cout << "Cannot open the camera" << Endl;
}
int i = 0;
Char imgname[100];
Namedwindow ("USB video", window_normal);
bool start = false;
bool stop = false;
Sleep (+);
while (!stop)
{
Mat frame;
Usbcap >> frame;
Imshow ("USB video", frame);
if ((waitkey () = = ' s ') | | start)
{
start = true;
sprintf (Imgname, "%s%d%s", "img", ++i, ". jpg");
Imwrite (imgname, frame);
if (waitkey () = = ' Q ')
stop = True;}}
}
read video and save by frame
void Videoread () {Videocapture capture ("1.avi");
if (!capture.isopened ()) cout << "Error reading" << Endl;
Char imgname [100];
int i = 0;
Mat frame;
Cvnamedwindow ("video", Cv_window_normal);
bool stop = FALSE;
while (!stop) {if (!capture.read (frame)) is break;
Imshow ("video", frame);
int key = Waitkey (30);
sprintf (Imgname, "%s%d%s", "img", ++i, ". jpg");
Imwrite (imgname, frame); }
}