title: OpenCV detect rectangle with the largest width
body: <p>I'm trying to get this rectangle from that image:
  <a href="https://i.stack.imgur.com/3STqr.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/3STqr.jpg" alt="Wanted rectangle"></a></p>
  
  <p>Found this solution using OpenCV:</p>
  
  <pre><code> private Bitmap findRectangle(Bitmap src) throws Exception {
          Mat imageMat = new Mat();
          Utils.bitmapToMat(src, imageMat);
  
          Mat imgSource=imageMat.clone();
  
          Imgproc.cvtColor(imgSource, imageMat, Imgproc.COLOR_BGR2GRAY);
  
          //find the contours
          List&lt;MatOfPoint&gt; contours = new ArrayList&lt;MatOfPoint&gt;();
          Imgproc.findContours(imageMat, contours, new Mat(), Imgproc.RETR_LIST, Imgproc.CHAIN_APPROX_NONE);
  
          Imgproc.Canny(imageMat,imageMat,0,255);
          Bitmap canny=Bitmap.createBitmap(imageMat.cols(),imageMat.rows(),Bitmap.Config.ARGB_8888);
          Utils.matToBitmap(imageMat,canny);
  
          Imgproc.GaussianBlur(imageMat, imageMat, new  org.opencv.core.Size(1, 1), 2, 2);
          Bitmap blur=Bitmap.createBitmap(imageMat.cols(),imageMat.rows(),Bitmap.Config.ARGB_8888);
          Utils.matToBitmap(imageMat,blur);
  
          MatOfPoint temp_contour = contours.get(0); //the largest is at the index 0 for starting point
  
          for (int idx = 0; idx &lt; contours.size(); idx++) {
              temp_contour = contours.get(idx);
              //check if this contour is a square
  
              MatOfPoint2f new_mat = new MatOfPoint2f( temp_contour.toArray() );
  
              int contourSize = (int)temp_contour.total();
              MatOfPoint2f approxCurve_temp = new MatOfPoint2f();
              Imgproc.approxPolyDP(new_mat, approxCurve_temp, contourSize*0.05, true);
  
              if (approxCurve_temp.total() == 4) {
                  MatOfPoint points = new MatOfPoint( approxCurve_temp.toArray() );
                  Rect rect = Imgproc.boundingRect(points);
                  Imgproc.rectangle(imgSource, new Point(rect.x,rect.y), new Point(rect.x+rect.width,rect.y+rect.height), new Scalar(255, 0, 0, 255), 3);
  
              }
  
          }
          Bitmap analyzed=Bitmap.createBitmap(imgSource.cols(),imgSource.rows(),Bitmap.Config.ARGB_8888);
          Utils.matToBitmap(imgSource,analyzed);
  
          return analyzed;
      }
  </code></pre>
  
  <p>The best i got was this:
  <a href="https://i.stack.imgur.com/LPwhk.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/LPwhk.jpg" alt="result image"></a></p>
  
  <p>The problem is that the rectangle isn't perfect, maybe find the white numbers inside of that can be a best option, but i don't know too much of OpenCV.</p>
  
  <p>Original image:
  <a href="https://i.stack.imgur.com/yWVIN.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/yWVIN.jpg" alt="enter image description here"></a></p>