当我运行我的程序来检测方块时,内存会在几秒钟内变满。我之前使用过相同的程序,从未遇到过问题。
所以我的开始方法是跟随,其中 在行 detect_squares = findSquares4( img, storage );
中出现内存泄漏问题,如果我发表评论,那么没问题。
void findRect::start(Mat frame )
{
svm svmClassObject;
storage = cvCreateMemStorage(0);
merging_squaresStorage = cvCreateMemStorage(0);
namedWindow(wndname,1);
IplImage* img = new IplImage(frame); // Mat to IplImage conversion
cvShowImage(wndname, img);
// find the squares
CvSeq* detect_squares;
detect_squares = findSquares4( img, storage );
cvClearSeq(detect_squares);
cvClearMemStorage( storage );
cvClearMemStorage( merging_squaresStorage );
delete img;
cvWaitKey(1);
}
虽然我没有对方法findSquares4( img, storage );
进行任何更改,但我仍在发布它。
CvSeq* findRect::findSquares4( IplImage* img, CvMemStorage* storage )
{
CvSeq* contours;
int i, c, l, N = 11;
CvSize sz = cvSize( img->width & -2, img->height & -2 );
IplImage* timg = cvCloneImage( img ); // make a copy of input image
IplImage* gray = cvCreateImage( sz, 8, 1 );
IplImage* pyr = cvCreateImage( cvSize(sz.width/2, sz.height/2), 8, 3 );
IplImage* tgray;
CvSeq* result;
double s, t;
// create empty sequence that will contain points -
// 4 points per square (the square's vertices)
CvSeq* squares = cvCreateSeq( 0, sizeof(CvSeq), sizeof(CvPoint), storage );
cvSetImageROI( timg, cvRect( 0, 0, sz.width, sz.height ));
cvPyrDown( timg, pyr, 7 );
cvPyrUp( pyr, timg, 7 );
tgray = cvCreateImage( sz, 8, 1 );
// find squares in every color plane of the image
for( c = 0; c < 3; c++ )
{
// extract the c-th color plane
cvSetImageCOI( timg, c+1 );
cvCopy( timg, tgray, 0 );
// try several threshold levels
for( l = 0; l < N; l++ )
{
if( l == 0 )
{
cvCanny( tgray, gray, 0, thresh, 5 );
cvDilate( gray, gray, 0, 1 );
}
else
{
// tgray(x,y) = gray(x,y) < (l+1)*255/N ? 255 : 0
cvThreshold( tgray, gray, (l+1)*255/N, 255, CV_THRESH_BINARY );
}
cvFindContours( gray, storage, &contours, sizeof(CvContour), CV_RETR_LIST, CV_CHAIN_APPROX_SIMPLE, cvPoint(0,0) );
// test each contour
while( contours )
{
result = cvApproxPoly( contours, sizeof(CvContour), storage, CV_POLY_APPROX_DP, cvContourPerimeter(contours)*0.02, 0 );
if( result->total == 4 && fabs(cvContourArea(result,CV_WHOLE_SEQ)) > 1000 && cvCheckContourConvexity(result) )
{
s = 0;
for( i = 0; i < 5; i++ )
{
if( i >= 2 )
{
t = fabs(angle(
(CvPoint*)cvGetSeqElem( result, i ),
(CvPoint*)cvGetSeqElem( result, i-2 ),
(CvPoint*)cvGetSeqElem( result, i-1 ))
);
s = s > t ? s : t;
}
}
if( ( s < 0.5 ) )// && dx1< (height_image/3)
{
CvPoint *pt[4];
CvPoint *final_square_pt[4];
for( i = 0; i < 4; i++ )
{
//cvSeqPush( squares,(CvPoint*)cvGetSeqElem( result, i ));
pt[i]= (CvPoint*)cvGetSeqElem(result, i);
}
flag++; // It can represent the maiximum number of selected squares---without eliminating bigger ones
int difference_x= std::abs(pt[1]->x - pt[0]->x);
int difference_y= std::abs(pt[3]->y - pt[0]->y);
int difference_x1to3= std::abs(pt[3]->x - pt[1]->x);
if( difference_x>10 && difference_x<150 && difference_y>10 && difference_y<150 && difference_x1to3>10 && difference_x1to3<300 )//(height_image/5) )
{
for( i = 0; i < 4; i++ )
{
cvSeqPush( squares,(CvPoint*)cvGetSeqElem( result, i ));
final_square_pt[i]= (CvPoint*)cvGetSeqElem(result, i); // final_square_pt[i] consists of 4 points which makes a Single Square
}
actual_flag++;
}
}
}
// take the next contour
contours = contours->h_next;
}
}
}
// release all the temporary images
cvReleaseImage( &gray );
cvReleaseImage( &pyr );
cvReleaseImage( &tgray );
cvReleaseImage( &timg );
return squares;
}