26

I need the explanation on boundingRect of OpenCV. I have used it, it works great. Is there any reference where this function is fully explained please?

Christoph Rackwitz
  • 11,317
  • 4
  • 27
  • 36
vashish doolooa
  • 269
  • 1
  • 3
  • 4
  • 4
    Have you seen [THIS DOC](http://docs.opencv.org/3.1.0/dd/d49/tutorial_py_contour_features.html) – Jeru Luke Feb 25 '17 at 08:44
  • @JeruLuke : Yes, I did went through it several times. But i need more information about how it works in the background. – vashish doolooa Feb 26 '17 at 11:40
  • It gets the min and max locations of the contour in both x and y. Those define the bounding box. left=min_x, top=min_y, width=(max_x-min_x), height=(max_y-min_y) – fmw42 Nov 10 '22 at 20:23

2 Answers2

31

The cv2.boundingRect() function of OpenCV is used to draw an approximate rectangle around the binary image. This function is used mainly to highlight the region of interest after obtaining contours from an image.

As per the documentation there are two types of bounding rectangles:

  1. Straight Bounding Rectangle

Here a simple rectangle is drawn around the contour (ROI). As you can see in the documentation, a green rectangle is drawn around the ROI. Corresponding rectangle coordinates are obtained such that a rectangle completely encloses the contour.

  1. Rotated Rectangle
  • In this case cv2.minAreaRect() function is used to highlight the minimum rectangular area enclosing a contour.
  • cv2.boxPoints() obtains the 4 corner points of the obtained rectangle.
  • np.int0() is done to convert the corrdinates from float to integer format.
  • These points are then used to draw the rectangle. This is depicted by the red rectangle in the documentation.
Jeru Luke
  • 20,118
  • 13
  • 80
  • 87
0

just find min_x , min_y , max_x, max_y by looping over the points in the contour, and calculate the width max_x - min_x and height max_y - min_y

yazan sayed
  • 777
  • 7
  • 24