2014-07-20 162 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 –