Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Resize Memory Leak #28

Closed
murph1329 opened this issue May 11, 2017 · 2 comments
Closed

Resize Memory Leak #28

murph1329 opened this issue May 11, 2017 · 2 comments

Comments

@murph1329
Copy link

murph1329 commented May 11, 2017

EmguCV version: 3.1.0.2504

Similar issue:
http://answers.opencv.org/question/93006/resize-memory-leak-minor/

Details:
There's a noticeable memory leak with the "Image<TColor, TDepth>.Resize" method. The leak isn't as bad when using the "Emgu.CV.CvInvoke.Resize" method but it's still present. I've tested this with images of varying sizes; however, it's exacerbated when using a larger image and larger scale. The example below is running against an image at resolution 1920x1080.

I am monitoring the Private Byte growth using Process Explorer's Performance Graph.
https://technet.microsoft.com/en-us/sysinternals/processexplorer.aspx

Setup:

    Bitmap imageToProcess = new Bitmap(@"C:\Users\wesley.murphree\Pictures\test.png");
    OpenCV<ConfigSetting>.Resize(imageToProcess, 400, 400, OpenCV.Scaling.Grow);
    imageToProcess.Dispose();

Function:

    public enum Scaling
    {
        Shrink,
        Grow
    }
    /// <summary>
    /// Shrinks or grows an image based on percent width and height
    /// </summary>
    /// <param name="source">Image to resize</param>
    /// <param name="percentWidth">Percent as whole number</param>
    /// <param name="percentHeight">Percent as whole number</param>
    /// <returns></returns>
    public static Bitmap Resize(Bitmap source, int percentWidth, int percentHeight, Scaling scale)
    {
        try
        {
            Emgu.CV.Image<Emgu.CV.Structure.Bgr, byte> imgBgrSource = new Emgu.CV.Image<Emgu.CV.Structure.Bgr, byte>(source);

            #region Calcuate Width and Height based on percent
            int width = 0;
            int height = 0;
            if (scale == Scaling.Grow)
            {
                if (percentWidth < 1 || percentHeight < 1)
                    throw new Exception("Resize: Value must be greater than 0");

                int widthAdd = 0;
                if (percentWidth < 100)
                    widthAdd = 1;
                int heightAdd = 0;
                if (percentHeight < 100)
                    heightAdd = 1;
                width = Convert.ToInt32(imgBgrSource.Width * (widthAdd + (percentWidth * 0.01)));
                height = Convert.ToInt32(imgBgrSource.Height * (heightAdd + (percentHeight * 0.01)));
            }
            else
            {
                if ((percentWidth >= 100 || percentHeight >= 100)
                    || (percentWidth < 1 || percentHeight < 1))
                    throw new Exception("Resize: Value must be between 1 and 100");
                percentWidth = 100 - percentWidth;
                percentHeight = 100 - percentHeight;
                width = Convert.ToInt32(imgBgrSource.Width * (percentWidth * 0.01));
                height = Convert.ToInt32(imgBgrSource.Height * (percentHeight * 0.01));
            }
            #endregion

            /*
            //UNCOMMENT THIS SECTION TO TEST USING CvInvoke.Resize
            Emgu.CV.Image<Emgu.CV.Structure.Bgr, byte> output = imgBgrSource.CopyBlank();
            Emgu.CV.CvInvoke.Resize(imgBgrSource, output, new Size(width, height), 0, 0, interpolation: Emgu.CV.CvEnum.Inter.Linear);
            output.Dispose();
            */

            Emgu.CV.Image<Emgu.CV.Structure.Bgr, byte> largeImg = imgBgrSource.Resize(width, height, Emgu.CV.CvEnum.Inter.Linear);

            imgBgrSource.Dispose();
            largeImg.Dispose();
            return null;// return imgBgrSource.ToBitmap();
        }
        catch (Exception ex)
        {
            throw;
        }
    }
@emgucv
Copy link
Owner

emgucv commented May 16, 2017

A review in the source code of "Image<TColor, TDepth>.Resize" function from 3.2 release doesn't seem to indicate any managed memory leak from the C# code:

public Image<TColor, TDepth> Resize(int width, int height, CvEnum.Inter interpolationType)
        {
            Image<TColor, TDepth> imgScale = new Image<TColor, TDepth>(width, height);
            CvInvoke.Resize(this, imgScale, new Size(width, height), 0, 0, interpolationType);
            return imgScale;
        }

If the memory leaks comes from the native (C++) Open CV code, it will need to be fixed from the Open CV side.

@emgucv
Copy link
Owner

emgucv commented Jun 14, 2017

Closing the bug now due to inactivity.

@emgucv emgucv closed this as completed Jun 14, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants