Jurjen
Jurjen

Reputation: 1396

C# - Emgu CV - FindContours hierarchy data always null

I am working on contour finding in a C# EmguCV project. For this project it is essential that I obtain the contour hierarchy data. I have used this method before in a C++ OpenCV project, so I understand the workings of the FindContours method and the Hierarchy information. Please find the relevant code below:

Mat grayImage = new Mat(originalImage.Size, originalImage.Depth, 1);
Mat edges = new Mat(originalImage.Size, originalImage.Depth, 1);

CvInvoke.CvtColor(originalImage, grayImage, ColorConversion.Bgr2Gray);            
CvInvoke.Canny(grayImage, edges, 100, 200, 3);

var contours = new VectorOfVectorOfPoint();

Mat hierarchy = new Mat();
CvInvoke.FindContours(edges, contours, hierarchy, RetrType.Tree, ChainApproxMethod.ChainApproxSimple);

When I run this code contours are found as expected. Also the Hierarchy Mat object seems to get populated as expected, namely 1 x size of outputResult x 4 (see image below), yet the data in this object remains null:

Screenshot of hierarchy mat object

I have not been able to find an answer on how to extract the hierarchy contour (tree) data from this object. I have seen in other StackOverflow posts (see also the comments) that others have been struggling and/or that there may be bugs in the EmguCV 3.x library, but I have not been able to find an answer.

My question is thus: is it normal that the data field in the Hierarchy Mat object is null?

Upvotes: 0

Views: 3004

Answers (2)

Salo7ty
Salo7ty

Reputation: 495

this is working for me :

Mat hierarchy = new Mat();

CvInvoke.FindContours(edges, contours, hierarchy, RetrType.Tree, ChainApproxMethod.ChainApproxSimple);

Array arr = hierarchy.GetData(true);

and i extract hier data to dataGridView

        dataGridView1.Columns.Add("n-cont", "n-cont");
        dataGridView1.Columns.Add("next", "next");
        dataGridView1.Columns.Add("prev", "prev");

        dataGridView1.Columns.Add("f-child", "f-child");
        dataGridView1.Columns.Add("parent", "parent");


        for (int y = 0; y < hier.Cols; y++)
        {

            dataGridView1.Rows.Add(y.ToString(), arr.GetValue(0, y, 0).ToString(), arr.GetValue(0, y, 1).ToString()
                , arr.GetValue(0, y, 2).ToString(), arr.GetValue(0, y, 3).ToString());

        }

data in hierarchy Mat is put in ( 1 rows , n (num of contours) for cols , 4 channels (next , previos , first child , parent)

Upvotes: 0

Carlos Sodr&#233;
Carlos Sodr&#233;

Reputation: 11

Maybe it's late, but...

You can try this:

using (VectorOfVectorOfPoint vecVecPts2 = new VectorOfVectorOfPoint())
{

    int[,] hierarchy = CvInvoke.FindContourTree(img3, vecVecPts2, 
    ChainApproxMethod.ChainApproxNone);
}

or: Emgu CV 3 findContours and hierarchy parameter of type Vec4i equivalent?

        /// >>>>Based on [joshuanapoli] answer<<<
        /// <summary>
        /// Get a neighbor index in the heirarchy tree.
        /// </summary>
        /// <returns>
        /// A neighbor index or -1 if the given neighbor does not exist.
        /// </returns>
        //public int Get(HierarchyIndex component, int index)
        //public int GetHierarchy(Mat Hierarchy, int contourIdx, int component)
        public int[] GetHierarchy(Mat Hierarchy, int contourIdx)
        {
            int[] ret = new int[] { };

            if (Hierarchy.Depth != Emgu.CV.CvEnum.DepthType.Cv32S)
            {
                throw new ArgumentOutOfRangeException("ContourData must have Cv32S hierarchy element type.");
            }
            if (Hierarchy.Rows != 1)
            {
                throw new ArgumentOutOfRangeException("ContourData must have one hierarchy hierarchy row.");
            }
            if (Hierarchy.NumberOfChannels != 4)
            {
                throw new ArgumentOutOfRangeException("ContourData must have four hierarchy channels.");
            }
            if (Hierarchy.Dims != 2)
            {
                throw new ArgumentOutOfRangeException("ContourData must have two dimensional hierarchy.");
            }
            long elementStride = Hierarchy.ElementSize / sizeof(Int32);
            var offset0 = (long)0 + contourIdx * elementStride;
            if (0 <= offset0 && offset0 < Hierarchy.Total.ToInt64() * elementStride)
            {


                var offset1 = (long)1 + contourIdx * elementStride;
                var offset2 = (long)2 + contourIdx * elementStride;
                var offset3 = (long)3 + contourIdx * elementStride;

                ret = new int[4];

                unsafe
                {
                    //return *((Int32*)Hierarchy.DataPointer.ToPointer() + offset);

                    ret[0] = *((Int32*)Hierarchy.DataPointer.ToPointer() + offset0);
                    ret[1] = *((Int32*)Hierarchy.DataPointer.ToPointer() + offset1);
                    ret[2] = *((Int32*)Hierarchy.DataPointer.ToPointer() + offset2);
                    ret[3] = *((Int32*)Hierarchy.DataPointer.ToPointer() + offset3);
                }


            }
            //else
            //{
            //    return new int[] { };
            //}

            return ret;
        }

Upvotes: 1

Related Questions