Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

How to use a camera to get single frames?

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.

How can I do this?

I normally use my camera like this:

#include "opencv2/opencv.hpp"

using namespace cv;

int main(int, char**)
{
    VideoCapture cap(0);
    if(!cap.isOpened()) 
        return -1;


    namedWindow("video",1);
    for(;;)
    {
        Mat frame;
        cap >> frame; 
        imshow("video", frame);
        if(waitKey(30) >= 0) break;
    }
    return 0;
}

I have tried to delete the for loop and then to use "cap>>fame" whenever I need it. But if I try this, my programm freezes even if I wait a second before trying to get a new frame.

What can I do? Thank you very much :-)

How to use a camera to get single frames?

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.

How can I do this?

I normally use This is my camera like this:try:

#include "opencv2/opencv.hpp"

using namespace cv;

int main(int, char**)
void TakeOneFrame()
{
    VideoCapture cap(0);
    if(!cap.isOpened()) 
        return -1;


    namedWindow("video",1);
    for(;;)
    {
    namedWindow("Video");
    namedWindow("Picture");
    Mat frame;
     Mat picture;

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


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

I have tried to delete 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 loop and then to use "cap>>fame" whenever I need it. But if I try this, my programm freezes even if I wait a second before trying to get a new frame.

What can I do? problem?

Thank you very much :-)

How to use a camera to get single frames?

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