Edmar Sicabalo
Edmar Sicabalo

Reputation: 21

Emgu.CV.Util.CvException: 'OpenCV: i < 0'

I am new on EmguCv and kinect v2 development. I am trying to draw the contour of the hand that was detected on a blank Gray Image. And I am encountering this Exception that always occurs after the line of code:
CvInvoke.DrawContours(image, temp, -1, new MCvScalar(255, 0, 0), thickness);

Here is my function for drawing the contour:

 private void drawOnEachHand(Hand whichHand, Image<Gray, byte> image) {

            int thickness = 2;

            //Console.WriteLine("Check2 " + (whichHand == null));
            if (whichHand != null)
            {
                VectorOfPoint temp = new VectorOfPoint(whichHand.ContourDepth.Count);
                List<Point> arrTemp = new List<Point>();

                for (int intCounter = 0; intCounter < whichHand.ContourDepth.Count; intCounter++)
                {
                    int X = Convert.ToInt32(MathExtensions.ToPoint(whichHand.ContourDepth[intCounter]).X);
                    int Y = Convert.ToInt32(MathExtensions.ToPoint(whichHand.ContourDepth[intCounter]).Y);
                    arrTemp.Add(new Point(X, Y));
                }

                temp.Push(arrTemp.ToArray());

                CvInvoke.DrawContours(image, temp, -1, new MCvScalar(255, 0, 0), thickness);
                Console.WriteLine(image.Cols);
            }
        }

This is the exception message:

Exception thrown: 'Emgu.CV.Util.CvException' in Emgu.CV.World.dll<br>
An unhandled exception of type 'Emgu.CV.Util.CvException' occurred in Emgu.CV.World.dll<br>
OpenCV: i < 0

I am using Visual Studio 2017, Emgu Cv 3.x and I install it using the nugget. I can't figure out what's the meaning of the exception message.

Upvotes: 0

Views: 3240

Answers (2)

erik7854
erik7854

Reputation: 170

It's late but I encountered the same problem now and, due to the transition to Mat instead of Image<>, I'm posting the answer.

As stated in the EmguCV wiki:

Each contour is stored as a point vector.

So the DrawContours method excpect the contours to be stored in a VectorOfVectorOfPoint.

The easiest way to make it happy is to create a new VectorOfVectorOfPoint and store your contours there.

In your case it will be:

CvInvoke.DrawContours(image, new VectorOfVectorOfPoint(temp), -1, new MCvScalar(255, 0, 0), thickness);

Hope this helps solve someone else headache.

Upvotes: 0

Edmar Sicabalo
Edmar Sicabalo

Reputation: 21

I don't figure out what I was doing wrong but instead of using CvInvoke.DrawContours(), I use Image.DrawPolyline to achieve my goal.

I use:
image.DrawPolyline(points, false, new Gray, thickness);

Upvotes: 2

Related Questions