title: OpenCV Hierarchy is always null
body: <p>I have an image with overlapping contours and I have been trying to filter out the contours using hierarchy when I find them. What I am trying to do is filter out contours whose parents aren't equal to -1. However, when I try to get the information containing hierarchy the parent index is almost every time equal to null. Am I not looking at the right information for the getting the status of the current contours parent? Here is my code.</p>
  
  <pre><code>List&lt;MatOfPoint&gt; contours = new ArrayList&lt;&gt;();
      List&lt;MatOfPoint&gt; squareContours = new ArrayList&lt;&gt;();
      Mat hierarchy = new Mat();
      //find all contours
      Imgproc.findContours(dilated, contours, hierarchy, Imgproc.RETR_TREE, Imgproc.CHAIN_APPROX_SIMPLE);
  
      //Remove contours that aren't close to a square shape.
      for(int i = 0; i &lt; contours.size(); i++){
          if(hierarchy != null){
              double area = Imgproc.contourArea(contours.get(i)); 
              MatOfPoint2f contour2f = new MatOfPoint2f(contours.get(i).toArray());
              double perimeter = Imgproc.arcLength(contour2f, true);
              //Found squareness equation on wiki... 
              //https://en.wikipedia.org/wiki/Shape_factor_(image_analysis_and_microscopy)
              double squareness = 4 * Math.PI * area / Math.pow(perimeter, 2);
  
              if(squareness &gt;= 0.7 &amp;&amp; squareness &lt;= 0.9 &amp;&amp; area &gt;= 2000){
                  squareContours.add(contours.get(i));
              }
          }
      }
      //remove contour if it has a parent 
      List&lt;MatOfPoint&gt; finalContours = new ArrayList&lt;&gt;();
      for(int i = 0; i &lt; squareContours.size();i++){
          if(hierarchy.get(i, 3)[3] == -1){ //this should be checking parent index I think.
              finalContours.add(squareContours.get(i));
          }
      }
  </code></pre>
  
  <p>This is the output of the program when I print the hierarchy matrix that contains the parent information <code>Arrays.toString(hierarchy.get(i,3)))</code></p>
  
  <pre><code>[-1.0, -1.0, -1.0, 2.0]
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  </code></pre>