2010-09-02 97 views
1

我在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如果有的話。

+0

如果您發佈了cvAlloc的代碼,它會更好。 – jfs 2010-09-02 10:51:58

+0

我更新了代碼。 – Jayesh 2010-09-02 10:59:39

+0

然後它也給這個錯誤:「空指針(NULL數組指針傳遞) 在函數cvGetMat,。\ cxarray.cpp(2781)」 – Jayesh 2010-09-02 11:05:46

回答

1

內存泄漏怎麼樣?它似乎並沒有釋放像指向圖像和斑點的所有資源。這應該是你最關心的問題。而cvAlloc()是OpenCV用於內存分配的內部函數。

+1

+1,請參閱運行應用程序時任務管理器中的內存消耗,如果您不止一次使用此dll,則應爲thres_img和blobs_img執行cvReleaseImage(&gray_img)。 – dnul 2010-09-03 18:22:38

+0

謝謝dnul,它幫助。你能移動這個評論來回答嗎? – Jayesh 2010-09-06 08:21:59

+0

我想問一個問題。我用vC++舒服地使用了openCV。現在,如果我想使用如上所述的使用DLL的C#.NET的openCV,那麼由於.NET之間有一層厚厚的.NET,會不會有任何性能損失?基本上我想追蹤對象的運動和控制鼠標。因此,性能在這裏非常重要,因爲滯後會導致應用程序無用。 – nishant 2010-09-11 16:03:12