Correct way of memory management using Emgu CV 3.1

851 views Asked by At

My background is C++ and I have been trying to translate one of my apps that uses OpenCV to a C# app that uses EmguCV. I have read the EmguCV documentation and Wiki as well as StackOverflow Q/As as deeply as humanly possible with no luck in solving some of the basic problems. At this point I wonder if the way I think about EmguCV is fundamentally flawed.

I was under the impression that when EmguCV has provided an interface to the C++ library for let's say FloodFill using CvInvoke.FloodFill(IInputOutputArray, IInputOutputArray, ...), underneath it takes care of its own memory management to the point that the returning IInputOutputArrays (in my case a Mat) is all taken care of. In practice however, A call to CvInvoke.FloodFill fails with an exception from within the dll.

This is what I have:

int i = ...
int j = ...
int intensity = ...
int height = img.Rows;
int width = img.Cols;
Mat outputMask = new Mat(height + 2, width + 2, DepthType.Cv8U, 1);
Rectangle dummy = new Rectangle();
CvInvoke.FloodFill(img, outputMask, new Point(i, j), new MCvScalar(255), out dummy, new MCvScalar(intensity), new MCvScalar(intensity), Connectivity.EightConnected, FloodFillType.MaskOnly);

But the call fails with an exception description that is not helpful. I have used GCHandle allocation and pinning for other methods to be able to access the raw data underneath, but I believe CvInvoke.FloodFill should be safe when called on some local Mats.

My question is whether I'm completely off track with the way EmguCV is supposed to be used given my background with C++ and OpenCV. If so, what is the correct way of calling such function?

1

There are 1 answers

0
Bee On BEST ANSWER

This is for anyone who may find themselves having the same problem with EmguCV 3.1 FloodFill. After debugging my code for quite a long time, I am convinced that EmguCV 3.1 implementation of CvInvoke.FloodFill is buggy. There was no way to get a mask out of that method using FloodFillType.MaskOnly flag.

Instead I ended up editing my original image to have values between 0 and 254, then did a normal FloodFill using fill value of 255. Then used Image.InRange() method to separate all 255 (filled) pixels from the original image. I will report the bug to EmguCV team and see what happens.