# Get the smallest rectangle from image

**URL:** <https://forum.opencv.org/t/get-the-smallest-rectangle-from-image/2763>\
**Category:** Android/Java\
**Tags:** imgproc\
**Created:** [April 14, 2021, 2:50pm UTC](https://forum.opencv.org/t/get-the-smallest-rectangle-from-image/2763 "2021-04-14T14:50:56Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![shubha](https://avatars.discourse-cdn.com/v4/letter/s/e9a140/32.png) [@shubha](https://forum.opencv.org/u/shubha)\
**Post date:** [April 14, 2021, 2:50pm UTC](https://forum.opencv.org/t/get-the-smallest-rectangle-from-image/2763/1 "2021-04-14T14:50:56Z")

</div>

I have a image , I have changed the image to grayscale “Imgproc.cvtColor(src, dst, Imgproc.COLOR\_RGB2GRAY)”, I have applied canny detector. I need to get the smallest rectangle from the image , Image example can be a copy of Aadhar card on A4 sheet.  
I have successfully done the edge detections using the link , [OpenCV - Canny Edge Detection - Tutorialspoint](https://www.tutorialspoint.com/opencv/opencv_canny_edge_detection.htm)

---

<div class="post-metadata">

**Author:** ![berak](https://avatars.discourse-cdn.com/v4/letter/b/85f322/32.png) [@berak](https://forum.opencv.org/u/berak)\
**Post date:** [April 15, 2021, 11:02am UTC](https://forum.opencv.org/t/get-the-smallest-rectangle-from-image/2763/2 "2021-04-15T11:02:04Z")

</div>

have a look at the [contours tutorial](https://docs.opencv.org/master/df/d0d/tutorial_find_contours.html)  
(it has java code, too, look at the update() method there !)

---

<div class="post-metadata">

**Author:** ![shubha](https://avatars.discourse-cdn.com/v4/letter/s/e9a140/32.png) [@shubha](https://forum.opencv.org/u/shubha)\
**Post date:** [April 15, 2021, 12:41pm UTC](https://forum.opencv.org/t/get-the-smallest-rectangle-from-image/2763/3 "2021-04-15T12:41:15Z")

</div>

Thanks , I went through and tried implementing , I need to draw contours to the smallest rectangle , I see contours in my entire image. Can you please explain me on how do I get the rectangle or any document which gives me on how to get the smallest rectangle from the image with explanation.Please see the ref link , [Dealing with contours and bounding rectangle in OpenCV 2.4 - python 2.7 - Stack Overflow](https://stackoverflow.com/questions/16538774/dealing-with-contours-and-bounding-rectangle-in-opencv-2-4-python-2-7), here cod e is in Python

---

<div class="post-metadata">

**Author:** ![berak](https://avatars.discourse-cdn.com/v4/letter/b/85f322/32.png) [@berak](https://forum.opencv.org/u/berak)\
**Post date:** [April 15, 2021, 12:46pm UTC](https://forum.opencv.org/t/get-the-smallest-rectangle-from-image/2763/4 "2021-04-15T12:46:13Z")

</div>

loop over the contours and check [contourArea()](https://docs.opencv.org/master/javadoc/org/opencv/imgproc/Imgproc.html#contourArea(org.opencv.core.Mat,boolean))

there are several ways to determine, [if a contour is a rectangle](https://github.com/opencv/opencv/blob/master/samples/cpp/squares.cpp#L87-L121), probably the most simple one is getting the [minAreaRect()](https://docs.opencv.org/master/javadoc/org/opencv/imgproc/Imgproc.html#minAreaRect(org.opencv.core.MatOfPoint2f)) and compare the size with the contour’s area again
