I am currently writing a program for stitching images using EmguCV. I use the Emgu.CV.Stiching Class for the stitching algorithm, an although i try to dispose the resources correctly i get huge memory leaks, depending on the amount and size of the images i use.
Here is a minimal reproducible example of my code
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
}
}
}
I am using VS 2015 and EmguCV Version 4.1.0.3420 and running a Debug Build.
Her is an example image so you can reproduce the problem:
Just put the images into a folder and paste the folderPath into the intended codeline. You can install the version of EmguCV i used in my code using NuGet.