title: OpenCV: Getting x co-ordinate from rect.br()
body: <p>I am able to detect and draw a bounding rectangle around a region of interest like so:</p>
  
  <p><a href="https://i.stack.imgur.com/T4Ufo.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/T4Ufo.png" alt="enter image description here"></a></p>
  
  <p>Now I need to find the "x" value of the bottom horizontal line of the green bounding rectangle. My main aim is to find the "x" value of the base of the battery, and the "x" value of the blue strip of paper, so that I can calculate the distance between them.</p>
  
  <p>There are only 2 values, <code>rect.tl()</code> and <code>rect.br()</code>, that I can see be used to draw the bounding rectangle <code>Imgproc.rectangle(sourceMat, rect.tl(), rect.br(), green, 3);</code>. I'm under the assumption that the "x" value from the bottom right point of the bounding rectangle will be the x co-ordinate of the pixel point. </p>
  
  <p><strong>How do i get the 'x' value from <code>rect.br()</code>
  When i print out <code>rect.br()</code> like so <code>Log.e("BR", rect.br().toString());</code> i get this:</strong></p>
  
  <blockquote>
    <p>E/BR: {793.0, 1686.0}</p>
  </blockquote>
  
  <pre><code>private Bitmap findRoi(Bitmap sourceBitmap) {
          Bitmap roiBitmap = null;
          Scalar green = new Scalar(0, 255, 0, 255);
          Mat sourceMat = new Mat(sourceBitmap.getWidth(), sourceBitmap.getHeight(), CvType.CV_8UC3);
          Utils.bitmapToMat(sourceBitmap, sourceMat);
          Mat roiTmp = sourceMat.clone();
  
          final Mat hsvMat = new Mat();
          sourceMat.copyTo(hsvMat);
  
          // convert mat to HSV format for Core.inRange()
          Imgproc.cvtColor(hsvMat, hsvMat, Imgproc.COLOR_RGB2HSV);
  
          Scalar lowerb = new Scalar(85, 50, 40);         // lower color border for BLUE
          Scalar upperb = new Scalar(135, 255, 255);      // upper color border for BLUE
          Core.inRange(hsvMat, lowerb, upperb, roiTmp);   // select only blue pixels
           // find contours
          List&lt;MatOfPoint&gt; contours = new ArrayList&lt;&gt;();
          List&lt;RotatedRect&gt; boundingRects = new ArrayList&lt;&gt;();
          Imgproc.findContours(roiTmp, contours, new Mat(), Imgproc.RETR_LIST, Imgproc.CHAIN_APPROX_SIMPLE);
  
          // find appropriate bounding rectangles
          for (MatOfPoint contour : contours) {
              MatOfPoint2f areaPoints = new MatOfPoint2f(contour.toArray());
              RotatedRect boundingRect = Imgproc.minAreaRect(areaPoints);
  
              double rectangleArea = boundingRect.size.area();
  
              // test min ROI area in pixels
              if (rectangleArea &gt; 40001) {//400000
                  Point rotated_rect_points[] = new Point[4];
                  boundingRect.points(rotated_rect_points);
  
                  Rect rect = Imgproc.boundingRect(new MatOfPoint(rotated_rect_points));
  
                  // test horizontal ROI orientation
                  if (rect.width &gt; rect.height) {
                      Imgproc.rectangle(sourceMat, rect.tl(), rect.br(), green, 3);
                  }
              }
          }
  
          roiBitmap = Bitmap.createBitmap(sourceMat.cols(), sourceMat.rows(), Bitmap.Config.ARGB_8888);
          Utils.matToBitmap(sourceMat, roiBitmap);
          return roiBitmap;
      }
  </code></pre>