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; } };
source share