Question

I use Emgu CV wrapper for OpenCV.

I want to create function which generates contour in some way and returns it.

To create contour I use the following like:

Contour<PointF> contour = new Contour<PointF>(new MemStorage());

Who should care of deallocation of MemStorage?
Should I perform additional actions or will EmguCV handles this case correctly without additional code?

Or is the following code correct:

MemStorage memStorage = new MemStorage();
Contour<PointF> contour = new Contour<PointF>(memStorage);

and memStorage shouldbe freed explicitly?

Was it helpful?

Solution

Good question thus the upvote, I expect the us of memory storage (Memstorage) is the same as any other variable in .Net. If you intend to create it in multiple methods when called, then it would be more efficient to create a global memory storage allocation. As Memstorage is a pointer to a physical address where variables are stored it is much better to overwrite an individual variable within it then create a new Memstorage where resources have to be re-pooled and reallocated.

Thankfully the garbage collector is very efficient and when you exit the method in which Memstorage was collected the resources it took will be re-distributed when required or when you program is not doing anything. You could always tell the garbage collector to do this manually by setting the Memstorage variable to null and calling GB.Collect() method, of course this relies on Memstorage class being nullable.

To ensure that the memory allocation is handled as efficiently as possible then the using statement should be used. This will free up the resources used by the Memstrage variable. Here is an example I converted from opencv to accomplish the same as Matlabs bwareopen in which smaller items or surpressed. It does require work but that's on the data suppression side of things. Again however the using statement is only good when not accessing the function multiple times such as within a loop.

In specific answer to your question you should consider the use of a using statement to ensure MemStorage is released properly but this should only be if the method is called with sufficient breaks. If this method is to be called within a for loop sequentially them MemStorage should be declared as a global variable within a class and then if possible made null in the IDisposable method of the class before the garbage collector is called. Once the loop is complete then the dispose method of the class can be called to efficiently re-ditribute the resources. If you would like examples of this as well please let me know and I'll update my answer accordingly.

Example of using and MemStorage:

private Image<Bgr, byte> bwareaopen(Image<Bgr, byte> Input_Image, int threshold)
{

    Image<Bgr, byte> bwresults = Input_Image.Copy();

    using (MemStorage storage = new MemStorage())
    {
        for (Contour<Point> contours = Input_Image.Convert<Gray, byte>().FindContours(Emgu.CV.CvEnum.CHAIN_APPROX_METHOD.CV_CHAIN_APPROX_SIMPLE, Emgu.CV.CvEnum.RETR_TYPE.CV_RETR_LIST, storage); contours != null; contours = contours.HNext)
        {
            Contour<Point> currentContour = contours.ApproxPoly(contours.Perimeter * 0.05, storage);
            if (currentContour.Area < threshold) 
            {
                for (int i = currentContour.BoundingRectangle.X; i < currentContour.BoundingRectangle.X + currentContour.BoundingRectangle.Width; i++)
                {
                    for (int j = currentContour.BoundingRectangle.Y; j < currentContour.BoundingRectangle.Y + currentContour.BoundingRectangle.Height; j++)
                    {
                        bwresults.Data[j, i, 0] = 0;
                        bwresults.Data[j, i, 1] = 0;
                        bwresults.Data[j, i, 2] = 0;
                    }
                }
            }
        }
    }
    return bwresults;
}

The openCV code is available here http://tech.groups.yahoo.com/group/OpenCV/message/27345 and all credit goes to M.Klien for converting the method in matlab.

For those interested to get this function working correctly the for loop suppressing the data should use the boundary constraints of the contours rather than the Bounding Rectangle. As soon as I adjust this I will update the code accordingly.

I hope this helps answer you question somewhat,

Cheers,

Chris

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top