2014-02-24 5 views
0

프로그램을 만들려고하는데 어떤 이유로 메모리 손상 오류가 발생합니다.OpenCV : 메모리 손상

"비디오 프로세스"방식 만 사용하고 있으며 이미지 처리 방식은 사용하지 않습니다.

특정 비디오를 대상으로 지정하면 한 번만 작동합니다. 나는이 같은 동영상을 다시 사용하려고하면, 내가 이런 종류의 오류 얻을 :

//opencv 
#include <opencv2/core/core.hpp> 
#include <opencv2/highgui/highgui.hpp> 
#include <opencv2/video/background_segm.hpp> 
//C 
#include <stdio.h> 
//C++ 
#include <iostream> 
#include <sstream> 

using namespace cv; 
using namespace std; 

//global variables 
Mat frame; //current frame 
Mat fgMaskMOG; //fg mask generated by MOG method 
Mat fgMaskMOG2; //fg mask fg mask generated by MOG2 method 
Ptr<BackgroundSubtractor> pMOG; //MOG Background subtractor 
Ptr<BackgroundSubtractor> pMOG2; //MOG2 Background subtractor 
int keyboard; 

//function declarations 
void help(); 
void processVideo(char* videoFilename); 
void processImages(char* firstFrameFilename); 

void help() 
{ 
    cout 
     << "--------------------------------------------------------------------------" << endl 
     << "This program shows how to use background subtraction methods provided by " << endl 
     << " OpenCV. You can process both videos (-vid) and images (-img)."    << endl 
     << endl 
     << "Usage:"                  << endl 
     << "./bs {-vid <video filename>|-img <image filename>}"       << endl 
     << "for example: ./bs -vid video.avi"           << endl 
     << "or: ./bs -img /data/images/1.png"           << endl 
     << "--------------------------------------------------------------------------" << endl 
     << endl; 
} 

int main(int argc, char* argv[]) 
{ 
    //print help information 
    help(); 

    //check for the input parameter correctness 
    if(argc != 3) { 
     cerr <<"Incorret input list" << endl; 
     cerr <<"exiting..." << endl; 
     return EXIT_FAILURE; 
    } 

    //create GUI windows 
    namedWindow("Frame"); 
    namedWindow("FG Mask MOG"); 
    namedWindow("FG Mask MOG 2"); 

    //create Background Subtractor objects 
    //NOTE HERE!!!! 
    pMOG= new BackgroundSubtractorMOG(); //MOG approach 
    pMOG2 = new BackgroundSubtractorMOG2(); //MOG2 approach 

    if(strcmp(argv[1], "-vid") == 0) { 
     //input data coming from a video 
     processVideo(argv[2]); 
    } 
    else if(strcmp(argv[1], "-img") == 0) { 
     //input data coming from a sequence of images 
     processImages(argv[2]); 
    } 
    else { 
     //error in reading input parameters 
     cerr <<"Please, check the input parameters." << endl; 
     cerr <<"Exiting..." << endl; 
     return EXIT_FAILURE; 
    } 
    //destroy GUI windows 
    destroyAllWindows(); 
    return EXIT_SUCCESS; 
} 

void processVideo(char* videoFilename) { 
    //create the capture object 
    VideoCapture capture(videoFilename); 
    //VideoCapture capture(0); 
    if(!capture.isOpened()){ 
     //error in opening the video input 
     cerr << "Unable to open video file: " << videoFilename << endl; 
     exit(EXIT_FAILURE); 
    } 
    //read input data. ESC or 'q' for quitting 
    while((char)keyboard != 'q' && (char)keyboard != 27){ 
     //read the current frame 
     if(!capture.read(frame)) { 
      cerr << "Unable to read next frame." << endl; 
      cerr << "Exiting..." << endl; 
      exit(EXIT_FAILURE); 
     } 
     //update the background model 
      //AND HERE!!! 
     pMOG->operator()(frame, fgMaskMOG); 
     pMOG2->operator()(frame, fgMaskMOG2); 
     //get the frame number and write it on the current frame 
     stringstream ss; 
     rectangle(frame, cv::Point(10, 2), cv::Point(100,20), 
      cv::Scalar(255,255,255), -1); 
     ss << capture.get(CV_CAP_PROP_POS_FRAMES); 
     string frameNumberString = ss.str(); 
     putText(frame, frameNumberString.c_str(), cv::Point(15, 15), 
      FONT_HERSHEY_SIMPLEX, 0.5 , cv::Scalar(0,0,0)); 
     //show the current frame and the fg masks 
     imshow("Frame", frame); 
     imshow("FG Mask MOG", fgMaskMOG); 
     imshow("FG Mask MOG 2", fgMaskMOG2); 
     //get the input from the keyboard 
     keyboard = waitKey(30); 
    } 
    //delete capture object 
    capture.release(); 
} 

void processImages(char* fistFrameFilename) { 
    //read the first file of the sequence 
    frame = imread(fistFrameFilename); 
    if(!frame.data){ 
     //error in opening the first image 
     cerr << "Unable to open first image frame: " << fistFrameFilename << endl; 
     exit(EXIT_FAILURE); 
    } 
    //current image filename 
    string fn(fistFrameFilename); 
    //read input data. ESC or 'q' for quitting 
    while((char)keyboard != 'q' && (char)keyboard != 27){ 
     //update the background model 
      //ALSO HERE!!!! 
     pMOG->operator()(frame, fgMaskMOG); 
     pMOG2->operator()(frame, fgMaskMOG2); 
     //get the frame number and write it on the current frame 
     size_t index = fn.find_last_of("/"); 
     if(index == string::npos) { 
      index = fn.find_last_of("\\"); 
     } 
     size_t index2 = fn.find_last_of("."); 
     string prefix = fn.substr(0,index+1); 
     string suffix = fn.substr(index2); 
     string frameNumberString = fn.substr(index+1, index2-index-1); 
     istringstream iss(frameNumberString); 
     int frameNumber = 0; 
     iss >> frameNumber; 
     rectangle(frame, cv::Point(10, 2), cv::Point(100,20), 
      cv::Scalar(255,255,255), -1); 
     putText(frame, frameNumberString.c_str(), cv::Point(15, 15), 
      FONT_HERSHEY_SIMPLEX, 0.5 , cv::Scalar(0,0,0)); 
     //show the current frame and the fg masks 
     imshow("Frame", frame); 
     imshow("FG Mask MOG", fgMaskMOG); 
     imshow("FG Mask MOG 2", fgMaskMOG2); 
     //get the input from the keyboard 
     keyboard = waitKey(30); 
     //search for the next image in the sequence 
     ostringstream oss; 
     oss << (frameNumber + 1); 
     string nextFrameNumberString = oss.str(); 
     string nextFrameFilename = prefix + nextFrameNumberString + suffix; 
     //read the next frame 
     frame = imread(nextFrameFilename); 
     if(!frame.data){ 
      //error in opening the next image in the sequence 
      cerr << "Unable to open image frame: " << nextFrameFilename << endl; 
      exit(EXIT_FAILURE); 
     } 
     //update the path of the current frame 
     fn.assign(nextFrameFilename); 
    } 
} 

수 :

*** Error in `./camack': malloc(): memory corruption: 0x000000000234b2c0 *** 

내가 파악하지 못할 문제가 ... 여기에 코드입니다 이거 도와 줘? 많이 고마워요

+0

나는 당신의 코드를 시험해 보았고 여기서는 완벽하게 작동합니다. – skm

답변

1

글로벌 vars은 악마입니다! 당신은 그들을 사용해서는 안됩니다. 여기

, Ptr<BackgroundSubtractor> pMOG2; 메인에

이동을 발표하지 않으며, processVideo 같은 함수에 인수()

는 또한 BackgroundSubtractor 해야에 전달 된 모든 이미지는 동일한이로 전달받을 않습니다 크기.

0

processImage 부분이 필요하지 않은 경우 부품을 제거하면 짧은 코드를 쉽게 이해할 수 있습니다. 나는 관련없는 부분을 제거했다. 여기 내 시스템에서 잘 작동하는 짧은 코드가있다.

//opencv 
#include <opencv2/core/core.hpp> 
#include <opencv2/highgui/highgui.hpp> 
#include <opencv2/video/background_segm.hpp> 
//C 
#include <stdio.h> 
//C++ 
#include <iostream> 
#include <sstream> 

using namespace cv; 
using namespace std; 

//global variables 
Mat frame; //current frame 
Mat fgMaskMOG; //fg mask generated by MOG method 
Mat fgMaskMOG2; //fg mask fg mask generated by MOG2 method 
Ptr<BackgroundSubtractor> pMOG; //MOG Background subtractor 
Ptr<BackgroundSubtractor> pMOG2; //MOG2 Background subtractor 
int keyboard; 

//function declarations 
void processVideo(char* videoFilename); 

int main(int argc, char* argv[]) 
{ 

    //create GUI windows 
    namedWindow("Frame"); 
    namedWindow("FG Mask MOG"); 
    namedWindow("FG Mask MOG 2"); 

    //create Background Subtractor objects 
    //NOTE HERE!!!! 
    pMOG= new BackgroundSubtractorMOG(); //MOG approach 
    pMOG2 = new BackgroundSubtractorMOG2(); //MOG2 approach 

    if(strcmp(argv[1], "-vid") == 0) { 
     //input data coming from a video 
     processVideo(argv[2]); 
    } 

    //destroy GUI windows 
    destroyAllWindows(); 
    return EXIT_SUCCESS; 
} 

void processVideo(char* videoFilename) { 
    //create the capture object 
    VideoCapture capture(videoFilename); 
    //VideoCapture capture(0); 

    if(!capture.isOpened()){ 
     //error in opening the video input 
     cerr << "Unable to open video file: " << videoFilename << endl; 
     exit(EXIT_FAILURE); 
    } 

    //read input data. ESC or 'q' for quitting 
    while((char)keyboard != 'q' && (char)keyboard != 27){ 
     //read the current frame 
     if(!capture.read(frame)) { 
      cerr << "Unable to read next frame." << endl; 
      cerr << "Exiting..." << endl; 
      exit(EXIT_FAILURE); 
     } 
     //update the background model 
      //AND HERE!!! 
     pMOG->operator()(frame, fgMaskMOG); 
     pMOG2->operator()(frame, fgMaskMOG2); 
     //get the frame number and write it on the current frame 
     stringstream ss; 
     rectangle(frame, cv::Point(10, 2), cv::Point(100,20), 
      cv::Scalar(255,255,255), -1); 
     ss << capture.get(CV_CAP_PROP_POS_FRAMES); 
     string frameNumberString = ss.str(); 
     putText(frame, frameNumberString.c_str(), cv::Point(15, 15), 
      FONT_HERSHEY_SIMPLEX, 0.5 , cv::Scalar(0,0,0)); 
     //show the current frame and the fg masks 
     imshow("Frame", frame); 
     imshow("FG Mask MOG", fgMaskMOG); 
     imshow("FG Mask MOG 2", fgMaskMOG2); 
     //get the input from the keyboard 
     keyboard = waitKey(30); 
    } 
    //delete capture object 
    capture.release(); 
}