Reputation: 43
I have completed the code in opencv for camera interfacing with raspberry pi. I have made the camera.h file, which I am including in my source file. It is working properly. But, in my main program I need the frame which I have capture in capture_image() function.
I want to return the frame at the end of my function capture_image( )
Here is my code:
#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)
}
the Error:
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]
It is logical that a int function will return int. But, I do not know how to define a cv:: Mat function() . Please help me.
Upvotes: 1
Views: 632
Reputation: 39796
'return' the Mat in a reference:
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;
Upvotes: 2
Reputation: 11941
Just pass the output Mat as a reference and copy the captured frame into. You normally don't want to return the captured frame without copying because it will get overwritten.
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
}
Upvotes: 2