我正在尝试创建一个在调用函数时给出框架的函数。因此,当我调用该函数时,它应该在调用该函数时给出相机前面的对象的图片。
我已经尝试了几个小时,但我无法成功。任何人?
主文件:
#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;
}
相机.h 文件
#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;
}
};