2014-03-14 60 views
-1

我需要幫助openCV。系統:WinXP,OpenCV 2.4.7.2,QT 5.2.1。我有內存泄漏問題。當此功能啓動,2分鐘左右,我有這樣的錯誤:「的std :: bad_alloc的內存位置0x0012a9e8 ...OpenCV內存泄漏

void EnclosingContour(IplImage* _image){ 

    assert(_image!=0); 
    //Clone src image and convert to gray 
    clone_image = cvCreateImage(cvGetSize(_image), IPL_DEPTH_8U, 1); 
    cvConvertImage(_image, clone_image, CV_BGR2GRAY); 

    //Some images for processing 
    dst = cvCreateImage(cvGetSize(_image), IPL_DEPTH_8U, 1); 
    temp = cvCreateImage(cvGetSize(_image), IPL_DEPTH_8U, 1); 
    //Make ROI 
    if (ui.chb_ROI->isChecked()){ 
     cvSetImageROI(clone_image, cvRect(ui.spb_x1->value(), ui.spb_y1->value(),ui.spb_x2->value(),ui.spb_y2->value()));} 
    //Create image for processing 
    bin = cvCreateImage(cvGetSize(clone_image), IPL_DEPTH_8U, 1); 
    bin = cvCloneImage(clone_image); 
    //Canny before 
    if (ui.chb_canny_before->isChecked()){ 
     cvCanny(bin, bin, ui.hsl_threshold_1->value(), ui.hsl_threshold_2->value()); 
    } 
    //Adaptive threshold 
    if (Adaptive==true){ 
     cvAdaptiveThreshold(bin, dst, ui.hsl_adaptive->value(), 0, 0, 3, 5); 
     bin = cvCloneImage(dst); 
     cvReleaseImage(&dst); 
    } 
    //Morphology operations 
    if (morphology==true){ 
     cvMorphologyEx(bin, bin, temp, NULL, operations, 1); 
     cvReleaseImage(&temp); 
    } 
    //Canny after 
    if (ui.chb_canny_after->isChecked()){ 
     cvCanny(bin, bin, ui.hsl_threshold_1->value(), ui.hsl_threshold_2->value()); 
    } 
    //Zero ROI 
    cvZero(clone_image); 
    cvCopyImage(bin, clone_image); 
    cvResetImageROI(clone_image); 
    //Show 
     cvNamedWindow("bin", 1); 
     cvShowImage("bin", clone_image); 
    cvReleaseImage(&clone_image); 
    //storage for contours 
     storage = cvCreateMemStorage(0); 
     contours=0; 
    // find contours 
    if (ui.chb_ROI->isChecked()){ 
     int contoursCont = cvFindContours(bin, storage,&contours,sizeof(CvContour),CV_RETR_LIST,method,cvPoint(ui.spb_x1->value(), ui.spb_y1->value())); 
    }else 
    { 
     int contoursCont = cvFindContours(bin, storage,&contours,sizeof(CvContour),CV_RETR_LIST,method,cvPoint(0,0)); 
    } 
    assert(contours!=0); 
    //How many contours 
    // All contours 
    for(CvSeq* current = contours; current != NULL; current = current->h_next){ 

      //Draw rectangle over all contours 
      CvRect r = cvBoundingRect(current, 1); 
      cvRectangleR(_image, r, cvScalar(0, 0, 255, 0), 3, 8, 0); 
      //Show width of rect 
      ui.textEdit_2->setText(QString::number(r.width));  
    } 
    // Clean resources 
    cvReleaseMemStorage(&storage); 
    cvReleaseImage(&bin); 

}

+2

請不要使用西里爾文的評論。他們在英文網站上沒有價值,他們只是帶來了噪音。 – lpapp

+1

請不要使用過時的c-api。它已經死了,走了。你只會用它射擊你的腳。 – berak

回答

2

您正在釋放‘臨時’和‘DST’圖像這是一個內存泄漏的肯定祕訣,因爲它們可能根本不會被釋放。

在附註中,您使用的是OpenCV的C接口,它已被棄用,並且很快會被刪除。切換到C++界面(即,如果您使用Mat而不是IplImage *),而不是由於未發佈的圖像造成的內存泄漏問題,則不可能發生。