Itay Krisher
Itay Krisher

Reputation: 95

Emgu CvInvoke.CalcHist throws exception OpenCV: 0 <= _rowRange.start && _rowRange.start <= _rowRange.end && _rowRange.end <= m.rows

I want to create a histogram from a grayscale image using CvInvoke.CalcHist(). For some reason the code is throwing an exception of 0 <= _rowRange.start && _rowRange.start <= _rowRange.end && _rowRange.end <= m.rows.

I saw the solution on Emgu - CalcHist _rowRange error but it didn't work for me. Also I don't see why VectorOfMat should work when UMat doesn't. They are both implementers of InputArrayOfArrays...

This is my code. Any help will be appreciated:

            #region Load image
            Image<Bgr, Byte> img = null;
            try
            {
                img = new Image<Bgr, byte>(imagePath);
            }
            catch (Exception e)
            {
                throw new Exception(string.Format("Image for file {0} was not loaded", imagePath));
            }
            #endregion

            #region Convert the image to grayscale
            UMat gray = new UMat();
            CvInvoke.CvtColor(img, gray, ColorConversion.Bgr2Gray);
            #endregion

            #region Calculate histogram

            Mat hist = new Mat();
            
            try
            {
                UMat v = new UMat();
                
                CvInvoke.CalcHist(gray, new int[] { 0 }, null, hist, new int[] { 256 }, new float[] { 0, 256 }, false);
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                throw;
            }

Upvotes: 1

Views: 812

Answers (1)

Itay Krisher
Itay Krisher

Reputation: 95

So the solution was indeed to use the VectorOfUmat. This way, the input to CalcHist is a single cell variable. The variable is the grayscale image. See the code below for the correction:

        VectorOfUMat vou = new VectorOfUMat();
        vou.Push(gray);
        Mat hist = new Mat();
        
        try
        {
            UMat v = new UMat();
            CvInvoke.CalcHist(vou, new int[] { 0 }, null, hist, new int[] { 256 }, new float[] { 0, 256 }, false);
        }
        catch (Exception e)
        {
            Console.WriteLine(e);
            throw;
        }

Upvotes: 2

Related Questions