4

I'm trying to create a function that gives the frame at the moment the function is called. So when i call the function, it should give the picture of the object that is in front of the camera at the moment the function is called.

I have been trying for hours, but i can't succeed. Anyone?

main file:

#include "camera.h"
#include <iostream>
#include <unistd.h>
int main(int argc, const char *argv[])
{
    Camera cam;

    cam.setVideoSource(0);

    cv::Mat image;
    cv::Mat image2;

    cam.openCamera();

    cam.grabFrame(image);  // grap first frame
    sleep(5);              // wait 5 seconds
    cam.grabFrame(image2); // capture seconds frame

    cv::namedWindow("1",CV_WINDOW_KEEPRATIO);
    cv::imshow("1",image);

    cv::namedWindow("2",CV_WINDOW_KEEPRATIO);
    cv::imshow("2",image2);

    cv::waitKey();
    return 0;
}

camera.h file

#include <iostream>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/core/core.hpp>

class Camera{
  private:
    int videoSource;    //video source
    cv::VideoCapture cap;   //capture of camera


  public:
    //constructor default videoSourceNumber 
    Camera() : videoSource(0) {};

    //Setter: videoSourceNumber
    void setVideoSource(int sourceNumber){
        videoSource = sourceNumber;
    }


    //function OPEN CAMERA
    //opens the video capture
    //returns true if successfull
    bool openCamera() {
        cap.open(videoSource);

        if (!cap.isOpened()){
            std::cout << "---- Error ----" << std::endl;
            return false;
        }

        return true;
    }


    //function GRAB FRAME
    //grabs the frame of the video capture
    //returns true if successfull
    bool grabFrame(cv::Mat& cameraFrame){
        cap >> cameraFrame;

        if (cameraFrame.empty()){
            std::cout << "---- Error ----" << std::endl;
            return false;
        }   

        return true;
    }
};
user2812874
  • 99
  • 1
  • 2
  • 4
  • Playing with this now. It seems to capture the images properly if I slowly step through (in Debug Mode). However, I'm noticing the OpenCV windows are getting weird names. I think memory is getting corrupted somehow. – escapecharacter Sep 24 '13 at 22:32

1 Answers1

0

A somewhat unsatisfying solution:

//function GRAB FRAME
//grabs the frame of the video capture
//returns true if successfull
bool grabFrame(cv::Mat& cameraFrame){
    int attempts = 0;
    do {
        cap >> cameraFrame;
        attempts++;
    } while (cameraFrame.empty() && attempts < 10);

    if (cameraFrame.empty()){
        std::cout << "---- Error ----" << std::endl;
        return false;
    }   

    return true;
}

I also played with this for a while and could not find a solution. My instinct was to give the camera more "warm-up" time before asking for the first frame. A sleep of 10 seconds seems to do so reliably, but that is not acceptable. If I don't give it any sleep before the first grabFrame() call, the while loop I added only seems to run twice.

Credit to: https://stackoverflow.com/a/9285151/2518451

Community
  • 1
  • 1
escapecharacter
  • 954
  • 2
  • 12
  • 29