How to find the bounding rectangle of an image contour in OpenCV Python?


The bounding rectangle of an object is a rectangle drawn around an object in the image. There are two methods to find the bounding rectangle in OpenCV

Straight Bounding Rectangle

It is a straight rectangle as it does not consider the rotation of an object. It can be computed using the function cv2.boundingRect(). Its syntax is as follows −

x,y,w,h = cv2.boundingRect(cnt)
img = cv2.rectangle(img,(x,y),(x+w,y+h),(0,255,0),2)

Here, "cnt" is an array of the contour points. It returns the top-left coordinate (x, y) and width and height (w, h) of the bounding rectangle.

Rotated Rectangle

It considers the rotation of an object and draws a rectangle with minimum area. Rotated rectangle is found using the function cv2.minAreaRect(). It returns the top-left corner (x, y), (width, height), angle of rotation. The 4 corners of the rectangle are obtained using the function cv2.boxPoints().

rect = cv2.minAreaRect(cnt)
box = cv2.boxPoints(rect)
box = np.int0(box)
img = cv2.drawContours(img,[box],0,(0,255,255),2)

Steps

You can use the following steps to compute the Jacobian of a given function −

  • Import the required libraries. The required libraries are OpenCV and NumPy.

  • Load the input image. Convert the input image to a grayscale image.

  • Apply thresholding on the grayscale image to create a binary image.

  • Find the contours of the objects in image.

  • Compute straight bounding rectangles using the above contours. Draw the rectangles on the image.

  • Compute the rotated bounding rectangles and draw it on the image.

  • Display the image with a drawn straight and rotated bounding rectangle.

We will use the following image as the Input File in the following examples.

Example 1

In the Python code below, we compute the straight bounding rectangle.

# import required libraries import cv2 # read the input image img = cv2.imread('approx.png') # convert the image to grayscale gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) # apply thresholding on the gray image to create a binary image ret,thresh = cv2.threshold(gray,127,255,0) # find the contours contours, _ = cv2.findContours(thresh,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE) # take the first contour cnt = contours[0] # compute the bounding rectangle of the contour x,y,w,h = cv2.boundingRect(cnt) # draw contour img = cv2.drawContours(img,[cnt],0,(0,255,255),2) # draw the bounding rectangle img = cv2.rectangle(img,(x,y),(x+w,y+h),(0,255,0),2) # display the image with bounding rectangle drawn on it cv2.imshow("Bounding Rectangle", img) cv2.waitKey(0) cv2.destroyAllWindows()

Output

When you execute the above code, it will produce the following output window.

In the above output, the green rectangle shows the straight bounding rectangle.

Now, let's compute the rotated bounding rectangle which encloses the minimum area. See the following example.

Example 2

In the Python code below, we compute both the straight bounding rectangle and rotated bounding rectangle. Compare the output windows to get a clear understanding of the difference between the two types of bounding rectangles.

import cv2 import numpy as np img = cv2.imread('approx.png') img1 = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) ret,thresh = cv2.threshold(img1,127,255,0) contours,_ = cv2.findContours(thresh, cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE) # print("Number of contours detected:", len(contours)) cnt = contours[0] # compute straight bounding rectangle x,y,w,h = cv2.boundingRect(cnt) img = cv2.drawContours(img,[cnt],0,(255,255,0),2) img = cv2.rectangle(img,(x,y),(x+w,y+h),(0,255,0),2) # compute rotated rectangle (minimum area) rect = cv2.minAreaRect(cnt) box = cv2.boxPoints(rect) box = np.int0(box) # draw minimum area rectangle (rotated rectangle) img = cv2.drawContours(img,[box],0,(0,255,255),2) cv2.imshow("Bounding Rectangles", img) cv2.waitKey(0) cv2.destroyAllWindows()

Output

When you execute the above code, it will produce the following output window.

In the above output, the green color rectangle is the straight bounding rectangle and in yellow color the rectangle is the rotated rectangle with minimum area. Notice the difference between the two rectangles.

Updated on: 28-Aug-2023

22K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements