views:

110

answers:

1

Hi,

I had written a blob tracking algorithm in VC++. I had run it in a console program, it just performed brilliantly.

Now, I wanted to write the rest of my application in c#, so I made a dll of the VC++ code. And I am calling this dll from C# code.

Now, in C#, after running for around 2 minutes, the application is throwing an error;

Insufficient memory (Out of memory)
in function cvAlloc, .\cxalloc.cpp(111)

I am no where in the code, allocating memory using cvAlloc so I am just wondering what is causing it to throw this error. Moreover, the same code runs for hours when I run it in console without making it's dll.

Can anyone please help me on what is causing it?

Thank You.

Code:

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;

This is all I am doing. This logic works fine when I run the code in an independent console application, but fails when I wrap it in a dll and call it from c#.

Apart from this, I am having a struct too

struct resultData
 {
     int X, Y, status;
     char* error;
 };

but I wonder if it would throw an OpenCV Exception, if any.

+1  A: 

What about memory leaks? It doesn't seem that you're freeing all the resources like pointers to images and blobs. That should be your main concern. And cvAlloc() is internal function that OpenCV uses for memory allocation.

Adi
dnul
Thanks dnul, it helped. Can you move this comment to answer?
James
I wanted to ask one question actually. I have used openCV in vc++ comfortably. Now if I want to use openCV with C#.NET using DLLs as described above, wont there be any performance penalty because of a thick layer of .NET in between? Basically I want to track object movement and control mouse with that. So performance is very important here, as lagging can make the application useless.
nishant