0

Using latest v2 of OpenCVSharp (2.4.10.201...), which I installed in my project with the NuGet manager.

It seems the floodfill function has a memory-corruption issue. I am applying it to the following image: enter image description here

and here is the result: enter image description here

while the mask produced follows the same corruption pattern: enter image description here

The code I use is:

filledArea = new CvMat( hue.Rows + 2, hue.Cols + 2, MatrixType.U8C1 );
Cv.FloodFill( hue, hintPos, new CvScalar(255,255,255), low, upp, out filledAreaData,
    FloodFillFlag.Link8 | FloodFillFlag.FixedRange, filledArea );

where hue is the input image

hintPos is CvPoint(10,400)

low = upp = CvScalar(10,10,10,10)

Note: the "corruption pattern" is random and changes every time.

Jeru Luke
  • 20,118
  • 13
  • 80
  • 87
Bill Kotsias
  • 3,258
  • 6
  • 33
  • 60

1 Answers1

0

It was a problem with not initializing the mask with zero values. It looks like this:

filledArea = new CvMat( hue.Rows + 2, hue.Cols + 2, MatrixType.U8C1 );

DOESN'T initialize the reserved memory to ANY value, so you get "corrupted" pixels.

filledArea = new CvMat( hue.Rows + 2, hue.Cols + 2, MatrixType.U8C1, new CvScalar(0,0,0,0) );

DOES create a mask with 0 pixels.

My wrong, I thought it wasn't possible in C# to grab new memory without first initializing it to some value, as is the case with Array[] and List...

Bill Kotsias
  • 3,258
  • 6
  • 33
  • 60
  • OpenCvSharp wraps OpenCV which is written in C++ and C. OpenCvSharp `Mat` uses mostly unmanaged memory. – Leonid Vasilev May 19 '17 at 13:42
  • @LeonidVasilyev Yes, but this isn't Mat I was using. It's the C# equivalent CvMat..? – Bill Kotsias May 19 '17 at 14:54
  • 1
    `CvMat` and `Mat` are pretty much same things both in OpenCV and it's managed wrapper OpenCvSharp. Check [Difference between cvMat, Mat and IpImage](http://stackoverflow.com/questions/11037798/difference-between-cvmat-mat-and-ipimage), [Mat versus CvMat](https://github.com/shimat/opencvsharp/issues/95) discussions and `Mat` OpenCvSharp [source code](https://github.com/shimat/opencvsharp/blob/bfaf35f385fd5eda49792dfd19db27bcef079f9f/src/OpenCvSharp/modules/core/Mat/Mat.cs#L47) (note `NativeMethods` class usage). – Leonid Vasilev May 19 '17 at 15:06