Утечки памяти с помощью EmguCV.Sitching Class - PullRequest
1 голос
/ 29 июня 2019

В настоящее время я пишу программу для сшивания изображений с помощью EmguCV.Я использую класс Emgu.CV.Stiching для алгоритма сшивания, хотя я пытаюсь правильно распределить ресурсы и получаю огромные утечки памяти, в зависимости от количества и размера изображений, которые я использую.

Вот минимальный воспроизводимый пример моего кода

using System;
using System.IO;

using Emgu.CV;
using Emgu.CV.Structure;
using Emgu.CV.Stitching;
using Emgu.CV.Util;
using Emgu.CV.Features2D;
using Emgu.CV.CvEnum;

namespace Example
{
    class Program
    {
        static void Main(string[] args)
        {
            string folderPath = @"e:\Users\Dennis\Desktop\Stitching\ExampleImages2";       //Paste your the path of your folder, which contains the images, here       
            string format = ".jpg";
            string[] filePaths = Directory.GetFiles(folderPath, ("*" + format));
            string storagePath;

            Directory.CreateDirectory(folderPath + @"\Stitched");                                               //creates new Directionary if not exist
            int num = 1;                                                                                        //Sets file numbering
            while (true)                                                                                        //Checks if there is already an exisitng file with that name and change numbering "num" if "true"
            {
                storagePath = folderPath + @"\Stitched" + @"\result_" + num + format;

                if (!File.Exists(storagePath))
                    break;
                num++;
            }



            //Image Stitching

            Stitcher.Mode mode = Stitcher.Mode.Scans;
            float threshhold = 0.0001f;
            Mat result = new Mat();

            using (Stitcher stitcher = new Stitcher(mode))                                                  //Creates a Stitcher configured with one of the stitching modes
            using (AKAZE detector = new AKAZE(AKAZE.DescriptorType.Mldb, 0, 3, threshhold))                 //Creates a Feature Detector with a user-chosen threshhold
            using (VectorOfMat input = new VectorOfMat())
            {
                stitcher.SetFeaturesFinder(detector);

                Mat[] images = new Mat[filePaths.Length];

                for (int i = 0; i < filePaths.Length; i++)
                {
                    images[i] = CvInvoke.Imread(filePaths[i], ImreadModes.AnyColor);
                }

                input.Push(images);

                for (int i = 0; i < filePaths.Length; i++)
                {
                    images[i].Dispose();
                }

                stitcher.Stitch(input, result);

            }

            using (Image<Bgr, Byte> resImage = result.ToImage<Bgr, Byte>())
            {
                resImage.Save(storagePath);
            }

            result.Dispose();


            Console.ReadKey();              //I included this, so the program wont stop and you can see, how the memory consumption graph is not falling
        }

    }
}

Я использую VS 2015 и EmguCV версии 4.1.0.3420 и запускаю сборку отладки.

Она являетсяпример изображения, чтобы вы могли воспроизвести проблему:

image_part_1

image_part_2

Просто поместите изображения в папку и вставьтепуть к папке в предполагаемую кодовую строку.Вы можете установить версию EmguCV, которую я использовал в своем коде, используя NuGet.

...