我用VC++写了一个斑点跟踪算法。我在一个控制台程序中运行了它,它的表现非常出色。
现在,我想用c#编写应用程序的其余部分,所以我为VC++代码创建了一个动态链接库。我从C#代码中调用这个动态链接库。
现在,在C#中,在运行大约2分钟后,应用程序抛出了一个错误;
Insufficient memory (Out of memory)
in function cvAlloc, .\cxalloc.cpp(111)我不在使用cvAlloc分配内存的代码中,所以我只是想知道是什么导致它抛出这个错误。此外,当我在控制台中运行相同的代码时,它会运行几个小时,而不会使其成为dll。
有人能帮我解释一下是什么原因造成的吗?
谢谢。
代码:
int NumberBlob = 0, PosX = 0, PosY = 0;
IplImage *img = 0;
IplImage *gray_img = 0;
IplImage *thres_img = 0;
IplImage *blobs_img = 0;
int key = 0;
/* Always check if the program can find a device */
if ( !capture )
{
data->status = 0;
return;
}
CBlobResult blobs;
CBlob *currentBlob;
CvRect rect;
int frame_count = 0;
int i = 0;
int screen_x = GetSystemMetrics(SM_CXSCREEN);
int screen_y = GetSystemMetrics(SM_CYSCREEN);
int mouse_x,mouse_y;
double x=0;
double y=0;
if( frame_count == 0 )
{
/* Obtain a frame from the device */
img = cvQueryFrame( capture );
/* Always check if the device returns a frame */
if( !img )
{
data->status = 1;
return;
}
gray_img = cvCreateImage( cvGetSize(img), img->depth, 1);
thres_img = cvCreateImage( cvGetSize(img), img->depth, 1);
blobs_img = cvCreateImage( cvGetSize(img), img->depth, 3);
}
/* Obtain a frame from the device */
img = cvQueryFrame( capture );
/* Always check if the device returns a frame */
if( !img )
{
data->status=2;
return;
}
frame_count = frame_count + 1;
/* Flip image once, after blob processing it is flipped back */
cvFlip(img,img,NULL);
/* Convert image from Color to grayscale and
then to binary (thresholded at 180) */
cvCvtColor(img,gray_img,CV_RGB2GRAY);
cvThreshold(gray_img,thres_img,200,255,CV_THRESH_BINARY);
/* Find Blobs that are White, Hence 'uchar backgroundColor = 0' (Black) */
blobs = CBlobResult(thres_img, NULL,0);
/* Remove blobs if it does not cover minimum area specified below */
blobs.Filter( blobs, B_EXCLUDE, CBlobGetArea(),B_LESS,5,50);
/* Number of blobs */
NumberBlob = blobs.GetNumBlobs();
/* 'i' points to blob 0, i.e., first blob */
/* If some blobs are detected then find the first blob */
if(i==0 && blobs.GetNumBlobs()>i)
{
currentBlocb = blobs.GetBlob(i);
rect = currentBlob->GetBoundingBox();
PosX = currentBlob->MinX();
PosY = currentBlob->MinY();
currentBlob->FillBlob( blobs_img, CV_RGB(255,0,0));
}
cvZero(blobs_img);
data->X=PosX;
data->Y=PosY;
data->status=1;
return;这就是我所做的一切。当我在一个独立的控制台应用程序中运行代码时,这个逻辑工作得很好,但是当我把它包装在dll中并从c#调用它时,这个逻辑就失败了。
除此之外,我也有一个结构体
struct resultData
{
int X, Y, status;
char* error;
};但我想知道它是否会抛出OpenCV异常。
发布于 2010-09-04 01:37:22
内存泄漏怎么办?看起来您并没有释放所有的资源,比如指向图像和blobs的指针。这应该是你的主要关注点。cvAlloc()是OpenCV用于内存分配的内部函数。
https://stackoverflow.com/questions/3625942
复制相似问题