title: How to choose the right rectangles in an image?
body: <p>I want to detect the colors of a Rubiks Cube. That is what I want: <a href="https://www.youtube.com/watch?v=VaW1dmqRE0o" rel="nofollow noreferrer">Link</a><br />
  I'm able to recognize the 9 colored fields with the <code>findContours</code> function of Open CV.<br />
  Here is my code:</p>
  <pre><code>Mat input = new Mat(); //The image
  Mat blur = new Mat();
  Mat canny = new Mat();
  
  Imgproc.GaussianBlur(input, blur, new Size(3,3), 1.5); //GaussianBlur to reduce noise
  
  Imgproc.Canny(blur, canny, 60, 70); //Canny to detect the edges
  Imgproc.GaussianBlur(canny, canny, new Size(3,3), 1.5); //Again GaussianBlur to reduce noise
  
  List&lt;MatOfPoint&gt; contours = new ArrayList&lt;&gt;();
  Mat hierachy = new Mat();
  
  Imgproc.findContours(canny, contours, hierachy, Imgproc.RETR_LIST, Imgproc.CHAIN_APPROX_SIMPLE); //Find contours
  
  List&lt;MatOfPoint2f&gt; approxedShapes = new ArrayList&lt;&gt;();
  for(MatOfPoint point : contours){
      double area = Imgproc.contourArea(point);
      if(area &gt; 1000){
          MatOfPoint2f shape = new MatOfPoint2f(point.toArray());
          MatOfPoint2f approxedShape = new MatOfPoint2f();
  
          double epsilon = Imgproc.arcLength(shape, true) / 10;
  
          Imgproc.approxPolyDP(shape, approxedShape, epsilon, true); //&quot;Smooth&quot; the edges with approxPolyDP
          approxedShapes.add(approxedShape);
      }
  }
  
  //Visualisation
  for(MatOfPoint2f point : approxedShapes){
      RotatedRect rect = Imgproc.minAreaRect(new MatOfPoint2f(point.toArray()));
      Imgproc.circle(input, rect.center, 5, new Scalar(0, 0, 255));
  
      for(Point p : point.toArray()){
          Imgproc.circle(input, p, 5, new Scalar(0,255,0));
      }
  }
  </code></pre>
  <p>This is the &quot;raw&quot; source image:</p>
  <p><a href="http://postimg.org/image/kqdzi93s9" rel="nofollow noreferrer">Image</a></p>
  <p>It produces this output (Green circles: corners; Blue circles: center of the rectangle):</p>
  <p><img src="https://i.stack.imgur.com/3uXUw.jpg" alt="Image" /></p>
  <p>As you can see, there are more detected rectangles than 9. I want to get the nine midpoints in an array of Points.<br />
  How can I choose the right ones?<br />
  Hopefully, you can understand what I mean</p>