1 | initial version |
closing and re-opening a capture is usually a bad idea (slow/memory intensive), so i'd rather change the logic / code-flow in your program: have 1 button to take a picture, and another to quit.
VideoCapture cap;
if (!cap.open(0)) {
return 0;}
for (;;)
{
cap >> frame;
if (frame.empty()) break; // end of video stream
imshow("Smile! Press space to take a picture, esc to stop.", frame);
int k = waitKey(1);
if (k == ' ') // space
{
imwrite("../image.png", frame);
}
if (k == 27) // escape
{
break; // stop capturing by pressing ESC
}
}
// the camera will be closed automatically upon exit
2 | No.2 Revision |
closing and re-opening a capture is usually a bad idea (slow/memory intensive), so i'd rather change the logic / code-flow in your program: have 1 button to take a picture, and another to quit.
int imagesTaken=0;
VideoCapture cap;
if (!cap.open(0)) {
return 0;}
for (;;)
{
cap >> frame;
if (frame.empty()) break; // end of video stream
imshow("Smile! Press space to take a picture, esc to stop.", frame);
int k = waitKey(1);
if (k == ' ') // space
{
imwrite("../image.png", frame);
imwrite(format("../image%04d.png", imagesTaken), frame); // try unique name
imagesTaken++;
}
if (k == 27) // escape
{
break; // stop capturing by pressing ESC
}
}
// the camera will be closed automatically upon exit