2014-07-20 7 views
1

나는 라스베리 파이와 카메라 인터페이스를 위해 opencv에서 코드를 완성했습니다. 나는 소스 파일에 포함하고있는 camera.h 파일을 만들었습니다. 제대로 작동하고 있습니다. 그러나, 내 주요 프로그램에서 나는 capture_image() 함수에서 캡처 한 프레임이 필요합니다.OpenCV 카메라 문제 :

#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] 

그것은 논리입니다

내 기능 capture_image()의 마지막 프레임을 반환하려면이

다음

내 코드입니다 int 함수는 int를 반환합니다. 하지만, 나는 어떻게 정의 해야할지 모르겠다 cv :: Mat function(). 도와주세요.

+0

당신이 바로 거기에 있었다 프레임 '을 생성하고 정수 오류 코드를 반환 할 수 없습니다. 다른 방법으로, 함수를'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

출력 매트를 참조로 전달하고 캡처 한 프레임을 복사하기 만하면됩니다. 일반적으로 캡쳐 된 프레임은 덮어 쓰기 때문에 복사하지 않고 반환하려는 경우는 없습니다.

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

'반환'참조의 매트 : 당신이해야`반환을 제외하고`이력서 :: 매트 capture_image (무효) {...}`:

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 –