Ask Your Question
1

How to use a camera to get single frames?

asked Nov 5 '14

Brixus gravatar image

updated Sep 25 '15

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 :-)

Preview: (hide)

1 answer

Sort by » oldest newest most voted
1

answered Nov 6 '14

berak gravatar image

updated Nov 6 '14

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;
}
Preview: (hide)

Comments

1

Thank you :-)

Brixus gravatar imageBrixus (Nov 6 '14)edit

Question Tools

Stats

Asked: Nov 5 '14

Seen: 3,094 times

Last updated: Nov 06 '14