Reputation: 31
I want to create a program to capture an image every five seconds. This is my source code:
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
Mat frame;
VideoCapture cap;
cap.open(0);
while(1)
{
cap>>frame;
imshow("frame",frame);
if(waitKey(10)=='c')
{
QString nama = QString("webcam_capture_%1.jpg") .arg(QString::number(x));
imwrite(nama.toStdString(),frame);
x++;
sleep(5);
nama = QString("webcam_capture_%1.jpg") .arg(QString::number(x));
imwrite(nama.toStdString(),frame);
x++;
}
else if(waitKey(10)=='x')
break;
}
cap.release();
return a.exec();
}
The program supposed to captured two images when I press 'c', when the sleep executed, the program freezes for five seconds and then continue. But the image result between the first capture and the second capture are same.
What is the correct function to delay the program? Why the image capture result is same between two photos?
I'm using Windows 7, Qtcreator 2.7.2 Qt5, openCV 2.4.6. I planned to run this program on Linux too.
EDIT:
SOLVED! After adding the first cap>>frame
as stated by Ove, I added one more cap>>frame
and the next image capture is really the image after 5 seconds. The number of cap>>frame
needed depends on your cpu speed. I tried it on mini PC and I needed five cap>>frame
Upvotes: 1
Views: 1122
Reputation: 6317
You are reading the frame once at the start of the loop and then you are writing the same image to two different files. That's why you get the same image.
After the call to sleep(5)
, you should add this line:
cap>>frame;
in order to capture a new image and write the second image to the second file.
Upvotes: 4