2014-07-20 130 views
1

我已经完成了opencv中与覆盆子pi相机接口的代码。 我已经制作了camera.h文件,我将其包含在我的源文件中。它工作正常。 但是,在我的主程序中,我需要捕获capture_image()函数中的帧。OpenCV相机问题:

我想在我的功能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

你说得对有:'CV ::垫capture_image(无效){...}',除了你必须回'框架“,你不能返回一个整数错误代码。或者,您可以将函数设计为int capture_image(cv :: Mat&frame){...;帽>>框架; ...},允许您返回错误代码和/或框架。 –

+0

我已经尝试使用相同的东西cv :: Mat capture_image(void){... return frame}但我得到错误camera.h:在函数'cv :: Mat capture_image()': camera.h: 20:16:错误:从'int'到'cv :: Mat'的转换不明确 –

回答

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 
} 
2

'回归' 中的引用垫:

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; 
+0

谢谢@berak –