-3

I have to save image form webcam with interval of 5 sec, i tried loop but it doesn't work properly.. What can i do??? Any Timers???

if(i==0)
{
    cvSaveImage("crop.jpg",tmp);
    cvShowImage( "crop", tmp);
    i++;
    //printf("%d",i);
}
else
{
    i++;
    if(i==1000)
    {
        cvSaveImage("crop1.jpg",tmp);
        cvShowImage("crop1",tmp);
        //printf("%d",i);
    }
    if(i==2000)
        i=0;
} 
Drew Dormann
  • 59,987
  • 13
  • 123
  • 180
Vimal R'j
  • 39
  • 2
  • 6
  • 2
    There are many ways, see e.g. [`Sleep`](http://msdn.microsoft.com/en-us/library/windows/desktop/ms686298%28v=vs.85%29.aspx), or [`SetTimer`](http://msdn.microsoft.com/en-us/library/windows/desktop/ms644906%28v=vs.85%29.aspx). – Some programmer dude Apr 03 '13 at 15:34
  • 1
    What do you mean by "it doesn't work properly"? – David G Apr 03 '13 at 15:34
  • 1
    Did you try searching for it? This question has already many answers on the website. – Étienne Apr 03 '13 at 15:38

3 Answers3

1

Use a proper sleep function instead of hacking something with a loop. The right function to use might depend on your operating system, but on Windows you can use Sleep, as suggested by Joachim Pileborg:

http://msdn.microsoft.com/en-us/library/windows/desktop/ms686298(v=vs.85).aspx

David Grayson
  • 84,103
  • 24
  • 152
  • 189
0

You could use clock. Here's an example:

http://www.cplusplus.com/forum/beginner/76147/#msg408731

And there's actually a Timer class on the .NET framework.

http://msdn.microsoft.com/en-us/library/system.timers.timer(v=vs.71).aspx

Raúl Roa
  • 12,061
  • 13
  • 49
  • 64
0

I would go at it the same way Raul would, use a clock to check the time since you last saved an image. There are manyways you could do this but i will try to illustrate an example of the concept here using clock:

timePassed = (clock() - lastTime) / CLOCKS_PER_SEC;
bool takeNewPicture = timePassed >= minTimeBetweenShots;
if(takeNewPicture)
{
    cvSaveImage("crop.jpg",tmp);
    cvShowImage( "crop", tmp);
    lastTime = clock();
}

//Continue do whatever you want

the benefit with this approach is that you can do what you want between when shots are taken and your program wont halt for whatever minTimeBetweenShots equals. Which is the case when using:

Sleep(timeToSleep)

Unless its in a seperate thread.

Sebastian
  • 21
  • 3