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 IInputOutputArray
s (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 Mat
s.
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?
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 usingFloodFillType.MaskOnly
flag.Instead I ended up editing my original image to have values between
0
and254
, then did a normal FloodFill using fill value of255
. Then usedImage.InRange()
method to separate all255
(filled) pixels from the original image. I will report the bug to EmguCV team and see what happens.