OpenCV - frame capture function

I am trying to create a function that gives a frame at the time the function is called. Therefore, when I call a function, it should give an image of the object in front of the camera at the time the function is called.

I tried for hours, but I cannot succeed. Is 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; } }; 
+4
source share
1 answer

Somewhat unsatisfactory 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 shot. Sleeping for 10 seconds seems reliable, but this is unacceptable. If I don't let him sleep before the first call to grabFrame (), the while loop I added seems to work only twice.

Credit: fooobar.com/questions/545477 / ...

0
source

All Articles