Ask Your Question
0

Close camera after two seconds

asked 2016-08-02 02:12:46 -0600

@boom gravatar image

updated 2016-08-03 00:51:59 -0600

Hello there i am newbie in opencv. i am doing with camera capturing nd got the code from some google source. i understood the code.

#include "opencv2/opencv.hpp"

using namespace cv;

int main(int, char**)
{
    VideoCapture cap(0); // open the default camera
    if(!cap.isOpened())  // check if we succeeded
        return -1;

    for(;;)
    {
        Mat frame;
        cap >> frame; // get a new frame from camera
        // do any processing
        imwrite("/home/sachin/Desktop/a.jpg", frame);
        if(waitKey(1) >= 0) break;   // you can increase delay to 2 seconds here

cap.release(); } // the camera will be deinitialized automatically in VideoCapture destructor return 0; }

here camera capture the image while I press ctrl+c. but i want to do it as automatically close the camera in 2 second. i don't how to close camera . please help me anticipation of your help.

edit retag flag offensive close merge delete

Comments

Just destroy the VideoCapture-Device or call cap.release.

FooBar gravatar imageFooBar ( 2016-08-02 02:46:31 -0600 )edit

1 answer

Sort by ยป oldest newest most voted
1

answered 2016-08-02 03:04:01 -0600

berak gravatar image

you could try to set a timer:

int main(int, char**)
{
    VideoCapture cap(0); // open the default camera
    if(!cap.isOpened())  // check if we succeeded
        return -1;

    Mat frame; // keep it out of the loop, so we can save latest img.
    int64 t0 = getTickCount(); // start time
    for(;;)
    {
        cap >> frame; // get a new frame from camera
        int64 t1 = getTickCount(); // time now
        double timeRunning = double(t1-t0)/getTickFrequency();
        if (timeRunning >= 2.0) // 2 seconds
              break;
    }
    // write last frame to  disk:
    imwrite("/home/sachin/Desktop/a.jpg", frame);
    // the camera will be deinitialized automatically in VideoCapture destructor
    return 0;
}

sidenote: waitKey() won't work without a gui window

edit flag offensive delete link more

Comments

1

Thank you sir it works can u please tell me the cap.release i googled the syntax but not found

@boom gravatar image@boom ( 2016-08-02 06:25:04 -0600 )edit

i don't think, you'll have to do this explicitly, but it's just cap.release()

berak gravatar imageberak ( 2016-08-02 06:28:26 -0600 )edit

Thank sir I will oblige you. One more thing if i want to capture 2 image at single code mean 1st image capture in 2second and again 2nd capture and save as a1.jpg and a2.jpg i dojn't think it will save in array.

@boom gravatar image@boom ( 2016-08-02 06:34:49 -0600 )edit

that's slightly different from your original question, right ? or is it only about the filename ? that would be:

int framenumber = 0;
...
imwrite(format("/home/sachin/Desktop/a%d.jpg", framenumber), frame);
framenumber++;
berak gravatar imageberak ( 2016-08-02 06:39:46 -0600 )edit

yes sir u r right it's different from original. Thank you sir actually i want to make a process on image to detection of crack in biscuit. This is my M.tech and i am from Electric engg. and small Knowledge of language

@boom gravatar image@boom ( 2016-08-02 06:57:11 -0600 )edit

sir this code iis ok with ubuntu but while i run it on raspberry pi it gives error like videoc_dqbuf: no such devices

@boom gravatar image@boom ( 2016-08-16 02:16:23 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2016-08-02 02:12:46 -0600

Seen: 3,132 times

Last updated: Aug 03 '16