Ask Your Question
1

How to use a camera to get single frames?

asked 2014-11-05 16:52:36 -0500

Brixus gravatar image

updated 2015-09-25 14:35:38 -0500

Hi,

I would like to use my webcam as a photo camera.

I would like to have a function like "Mat GetOneFrame();" which I can call in short distances to deliver just one up to date frame.

This is my try:

void TakeOneFrame()
{
    namedWindow("Video");
    namedWindow("Picture");
    Mat frame;
    Mat picture;

    VideoCapture cap (0);
    for(int i=0; i<=2;i++)
       {
        if(char(waitKey(1)) != 'q' &&cap.isOpened())
            {cap.operator >>(frame);}
        frame.copyTo(picture);
        imshow("Video", frame);
       }
    cap.release();


   while(1)
    {
        imshow("Picture",picture);
    }
}

I found out, that I need to grab a few frames until there is one sharp one. The Video window produces the desired frame, but when the window "Picture" opens up, there is no image showing. Even if I chance the position of frame.copyTo(picture); this makes no change.

Why is that and is there a better solution for my problem?

Thank you very much :-)

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
1

answered 2014-11-06 04:22:45 -0500

berak gravatar image

updated 2014-11-06 04:23:51 -0500

the 'waitKey(some_millis)' has to go after the imshow(), else your window won't get updated:

VideoCapture cap(0);
for(int i=0; (i<=2 && cap.isOpened()); i++)
{
    cap >> frame;
    imshow("Video", frame);
    frame.copyTo(picture);
    if ( char(waitKey(1)) == 'q' ) 
        break;
}
edit flag offensive delete link more

Comments

1

Thank you :-)

Brixus gravatar imageBrixus ( 2014-11-06 06:08:38 -0500 )edit
Login/Signup to Answer

Question Tools

Stats

Asked: 2014-11-05 16:52:36 -0500

Seen: 2,022 times

Last updated: Nov 06 '14