我已经在opencv中完成了与raspberry pi相机接口的代码。 我制作了camera.h文件,我将其包含在源文件中。它运作正常。 但是,在我的主程序中,我需要在capture_image()函数中捕获的帧。
我想在函数capture_image()
的末尾返回帧这是我的代码:
#include<opencv2/opencv.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <iostream>
#include <stdio.h>
using namespace cv
using namespace std;
int n = 0;
static char cam_image[200];
int capture_image() {
VideoCapture capture(2); //try to open string, this will attempt to open it as a video file or image sequence
if (!capture.isOpened()) //if this fails, try to open as a video camera, through the use of an integer param
capture.open(2);
if (!capture.isOpened()) {
cerr << "Failed to open the video device, video file or image sequence!\n" << endl;
//help(av);
return 1;
}
string window_name = "Reference Image";
namedWindow(window_name, CV_WINDOW_KEEPRATIO); //resizable window;
Mat frame;
capture >> frame;
if (frame.empty());
imshow(window_name, frame);
waitKey(30);
sprintf(cam_image,"filename%.3d.jpg",n++);
imwrite(cam_image,frame);
cout << "Saved " << cam_image << endl;
return 0;// Actually I want return (frame)
}
错误:
camera.h: In function ‘int capture_image()’:
camera.h:34:17: error: invalid conversion from ‘cv::Mat*’ to ‘int’ [-fpermissive]
camera.h:24:13: warning: address of local variable ‘frame’ returned [enabled by default]
int函数返回int是合乎逻辑的。但是,我不知道如何定义一个 cv :: Mat函数()。 请帮我。
答案 0 :(得分:2)
只需将输出Mat作为参考传递,然后将捕获的帧复制到。您通常不希望在没有复制的情况下返回捕获的帧,因为它会被覆盖。
int capture_image(cv::Mat& result) // *** pass output Mat as reference
{
VideoCapture capture(2); //try to open string, this will attempt to open it as a video file or image sequence
if (!capture.isOpened()) //if this fails, try to open as a video camera, through the use of an integer param
capture.open(2);
if (!capture.isOpened()) {
cerr << "Failed to open the video device, video file or image sequence!\n" << endl;
//help(av);
return 1;
}
string window_name = "Reference Image";
namedWindow(window_name, CV_WINDOW_KEEPRATIO); //resizable window;
Mat frame;
capture >> frame;
if (!frame.empty())
{
frame.copyTo(result); // *** copy captured frame into output Mat
}
imshow(window_name, frame);
waitKey(30);
sprintf(cam_image,"filename%.3d.jpg",n++);
imwrite(cam_image,frame);
cout << "Saved " << cam_image << endl;
return 0;// Success
}
答案 1 :(得分:2)
&#39;返回&#39;垫子中的垫子:
int capture_image( Mat & frame)
{
if ( ! capture.read(frame) )
return 0;
return 1;
}
... later:
Mat frame;
int ok = capture_image(frame);
// use frame if ok was true;