我在VC++中編寫了一個blob跟蹤算法。我在一個控制檯程序中運行它,它表現出色。內存溢出問題-opeCV
現在,我想用c#編寫我的應用程序的其餘部分,所以我製作了一個dll的VC++代碼。我從C#代碼調用這個DLL。
現在,在C#中運行大約2分鐘後,應用程序拋出一個錯誤;
Insufficient memory (Out of memory)
in function cvAlloc, .\cxalloc.cpp(111)
我沒有在代碼中,使用cvAlloc所以我只是想知道是什麼原因造成它拋出這個錯誤分配內存。而且,當我在控制檯中運行它時,同樣的代碼運行了幾個小時而沒有使用它。
任何人都可以請幫助我什麼是造成它?
謝謝。
代碼:
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太
struct resultData
{
int X, Y, status;
char* error;
};
,但我不知道是否會拋出異常的OpenCV如果有的話。
如果您發佈了cvAlloc的代碼,它會更好。 – jfs 2010-09-02 10:51:58
我更新了代碼。 – Jayesh 2010-09-02 10:59:39
然後它也給這個錯誤:「空指針(NULL數組指針傳遞) 在函數cvGetMat,。\ cxarray.cpp(2781)」 – Jayesh 2010-09-02 11:05:46