Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6889555
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T06:10:16+00:00 2026-05-27T06:10:16+00:00

I use Emgu CV wrapper for OpenCV . I want to create function which

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T06:10:17+00:00Added an answer on May 27, 2026 at 6:10 am

    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

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The image resizing function provided by Emgu (a .net wrapper for OpenCV) can use
Im using EMGU (opencv wrapper) for image processing. I want to load images one
Use case: we have some project meta-data files which we want tracked, but are
Use case: I've just entered insert mode, and typed some text. Now I want
use PHP and MySQL and want to use SELECT statement which date_post(datetime variable) start
use case is simple: I want to run some boiler plate code before each
use pthread_create to create limited number of threads running concurrently Successfully compile and run
Use the static modifier to declare a static member, which belongs to the type
Use: The user searches for a partial postcode such as 'RG20' which should then
Use trap to capture signals like this: i=-1;while((++i<33)); do trap echo $i >> log.txt

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.