OpenCV Speicherüberlauf

bertonex19

Grünschnabel
Hallo Gemeinde,

ich arbeite mich gerade in OpenCV ein (ich hoffe der Thread passt an diese Stelle) und spiele etwas mit der eigenen Webcam rum, die ich als Quelle benutzte.

Die Frames die ich dort erhalte verarbeite ich weiter und lasse diese mit verschiedenen Filtern/ Funktionen entsprechend verändert ausgeben.

Das Problem das ich habe, ist dass die Frames den Speicher voll krachen und nach der Ausgabe nicht wieder gelöscht werden.

Wie wird der Speichers eines Image (Frame) wieder korrekt freigegeben,

hier mein Code:
Code:
#include "cv.h"
#include "highgui.h"
#include <iostream>

using namespace std;

int g_slider_position = 0;
CvCapture* g_capture = NULL;
 

//         ****      PyrDown                ****

IplImage* doPyrDown( IplImage* in, int filter = IPL_GAUSSIAN_5x5) 
{
 assert( in->width%2 == 0 && in->height%2 == 0 );
 IplImage* out = cvCreateImage(           
 cvSize( in->width/2, in->height/2 ), in->depth, in->nChannels);
 cvPyrDown( in, out );
 return( out );
 cvReleaseImage(&in);
 cvReleaseImage(&out);
};



//****************************************************************

//            ****          Canny Edge                            ****

IplImage* doCanny(IplImage* in, int lowThresh, int highThresh, int aperture) 
{
 IplImage* out = cvCreateImage( cvGetSize(in), IPL_DEPTH_8U,1 );
 IplImage* grey_out = cvCreateImage( cvGetSize(in), IPL_DEPTH_8U,1 );
 
 cvCvtColor(in, out, CV_RGB2GRAY);
 cvCanny( out, grey_out, lowThresh, highThresh, aperture );  
 
 return( grey_out );
 cvReleaseImage(&in);
 cvReleaseImage(&out);
 cvReleaseImage(&grey_out);
};


//************************************************************************


//                                 ***** HOUGH ****

IplImage*  Hough (IplImage* src)
{
    
    
    
        
        IplImage* color_dst = cvCreateImage( cvGetSize(src), 8, 3 );
        CvMemStorage* storage = cvCreateMemStorage(0);
        CvSeq* lines = 0;
        int i;
        
        cvCvtColor( src, color_dst, CV_GRAY2BGR );
#if 0
        lines = cvHoughLines2( src,
                               storage,
                               CV_HOUGH_STANDARD,
                               1,
                               CV_PI/180,
                               100,
                               0,
                               0 );

        for( i = 0; i < MIN(lines->total,100); i++ )
        {
            float* line = (float*)cvGetSeqElem(lines,i);
            float rho = line[0];
            float theta = line[1];
            CvPoint pt1, pt2;
            double a = cos(theta), b = sin(theta);
            double x0 = a*rho, y0 = b*rho;
            pt1.x = cvRound(x0 + 1000*(-b));
            pt1.y = cvRound(y0 + 1000*(a));
            pt2.x = cvRound(x0 - 1000*(-b));
            pt2.y = cvRound(y0 - 1000*(a));
            cvLine( color_dst, pt1, pt2, CV_RGB(255,0,0), 3, 8 );
        }
#else
        lines = cvHoughLines2( src,
                               storage,
                               CV_HOUGH_PROBABILISTIC,
                               1,
                               CV_PI/180,
                               80,
                               30,
                               10 );
        for( i = 0; i < lines->total; i++ )
        {
            CvPoint* line = (CvPoint*)cvGetSeqElem(lines,i);
            cvLine( color_dst, line[0], line[1], CV_RGB(255,0,0), 3, 8 );
        }
#endif
        return color_dst;
        
        cvReleaseImage(&src);
        cvReleaseImage(&color_dst);
       
        
    
}

// **************************************************************


//      ****                   MAIN             ****


int main( int argc, char** argv ) 
{
 int x1,x2,x3,y1,y2,y3;
 
 
 
 


 // Canny Start Values
 int lowthresh= 200;
 int highthresh= 400;
 int aperture = 3;
 
 // Find Contours Start Values
 
 int g_thresh=100;
 
 CvSeq* contours = 0;
 
 // Windows Definition
 
 cvNamedWindow( "Input", CV_WINDOW_AUTOSIZE );
 cvMoveWindow("Input",x1 = 10 , y1 = 10);

 cvNamedWindow( "Output 1", CV_WINDOW_AUTOSIZE );
 cvMoveWindow("Output 1", x2= 337 + x1, y1);
 
 cvNamedWindow("Output 2",CV_WINDOW_AUTOSIZE);
 cvMoveWindow("Output 2", x3= 337 + x2, y1);

 cvNamedWindow("Output 3", CV_WINDOW_AUTOSIZE);
 cvMoveWindow("Output 3", x3+337, y1);
  
 cvNamedWindow("Adjustment",0);
 cvMoveWindow("Adjustment", x3, y1 +275);
 cvResizeWindow("Adjustment", 400,200);
 IplImage* frame; 
 IplImage* frame_rs;
 IplImage* out1;
 IplImage* out2;
 IplImage* out3; 
 



 while(1) 
          {
                            
           g_capture = cvCaptureFromCAM(1);
           frame = cvQueryFrame( g_capture );
           if( !frame ) break;
           frame_rs = doPyrDown ( frame );
           cvShowImage( "Input", frame_rs );
           
                   
           
           //Canny in Out 1
           cvCreateTrackbar( "Low Thresh", "Adjustment", &lowthresh , 500, 0  );
           cvCreateTrackbar( "High Thresh", "Adjustment", &highthresh , 500, 0  );
           
           out1 = doCanny (frame, lowthresh, highthresh, 3); 
           IplImage* frame_rs_o1 = doPyrDown( out1);
           cvShowImage("Output 1", frame_rs_o1);
           
           //Hough in Out 2
           out2  = Hough(out1);
           IplImage* frame_rs_o2 = doPyrDown( out2);
           cvShowImage("Output 2", frame_rs_o2);
           
           //Contour in Out 3
           CvSeq* contours = 0;
           CvMemStorage* storage = NULL;
           
           cvCreateTrackbar("Threshold","Adjustment",&g_thresh,255,0);
           
           if( storage==NULL ) 
               {
               out3 = cvCreateImage( cvGetSize(frame), 8, 1 );
               storage = cvCreateMemStorage(0);
               } 
 
               else 
               {
               cvClearMemStorage( storage );
               }
           
           cvCvtColor( frame, out3, CV_BGR2GRAY );
           cvThreshold( out3, out3, g_thresh, 255, CV_THRESH_BINARY );
           cvFindContours( out3, storage, &contours );
           cvZero( out3 );
           if( contours )
              cvDrawContours(out3,contours,cvScalarAll(255),cvScalarAll(255),100);
                                
           IplImage* frame_rs_o3 = doPyrDown ( out3);
           cvShowImage("Output 3", frame_rs_o3);
           
           
           
           cvReleaseImage(&frame_rs_o3);
           cvReleaseImage(&frame_rs_o2);
           cvReleaseImage(&frame_rs_o1);
           cvReleaseImage(&frame_rs);
           
           cvReleaseImage(&out1);
           cvReleaseImage(&out2);
           cvReleaseImage(&out3);
           cvReleaseMemStorage(&storage);           
           cvReleaseCapture( &g_capture );
           char c = cvWaitKey(33);
           if( c == 27 ) break;
           
          }
          

 
 void cvDestroyAllWindows( void );
 cvReleaseImage(&frame);

 return(0);
}
 
Hi.
Code:
//         ****      PyrDown                ****

IplImage* doPyrDown( IplImage* in, int filter = IPL_GAUSSIAN_5x5) 
{
...
 return( out );
 cvReleaseImage(&in);
 cvReleaseImage(&out);
};
Nach einem return in einer Funktion noch Code anzugeben ist ziemlich unnütz. Hast du denn keine Warnungsmeldungen eingeschaltet? Der Compiler sollte dich eigentlich warnen, dass der Code hinter dem return niemals erreicht werden kann.

Wenn du out als Ergebnis der Funktion zurückgibst, dann mußt du im Programmteil, der die Funktion aufruft dieses Image auch freigeben (wenn du es nicht mehr brauchst).

Gruß
 
Zurück