AppDividend
Latest Code Tutorials

Python cv2 rectangle: Draw a Rectangle using OpenCV

0

OpenCV is a programming library aimed at real-time computer vision. OpenCV-Python is the main library of Python that is specifically designed to solve computer vision problems. Suppose you are working on Machine Learning projects than you often come across the scenario when you need to identify the specific object from the image. In this case, you need to draw a rectangle around a particular object to distinguish from other objects of the image.

To separate that object, you need to draw a rectangle around that, and there OpenCV-Python library comes to the rescue.

Python cv2 rectangle

To draw a rectangle using OpenCV in Python, use cv2.rectangle() function. The cv2 rectangle() method is used to draw a rectangle on any image.

Syntax

cv2.rectangle(image, start_point, end_point, color, thickness)

Parameters

image: It is the actual image on which the rectangle is to be drawn.
start_point: The start_point is the starting coordinates of the rectangle. These coordinates are represented as tuples of two values, i.e. (X coordinate value, Y coordinate value).
end_point: The end_point is the ending coordinates of the rectangle. These coordinates are represented as tuples of two values, i.e. (X coordinate value, Y coordinate value).
color: This parameter defines the borderline of the rectangle. For BGR, we pass the tuple. eg: (255, 0, 0) for blue color.
thickness: This parameter defines the thickness of the rectangle borderline in px. A thickness of -1 px will fill the rectangle shape by the specified color.

Return Value

The rectangle() method returns an image.

Install Python OpenCV

To work with OpenCV in Python, we have to install the opencv-python module.

Type the following command.

python3 -m pip install opencv-python

# OR

pip install opencv-python

To use the opencv-python in our project, we have to import the cv2 module in the file.

import cv2

Example of cv2 rectangle()

Write the following code inside the app.py method.

# Importing cv2
import cv2

# Path to the local image
path = 'cropped_image.jpg'

# Reading an image in default mode
image = cv2.imread(path)

# Window name in which image is displayed
window_name = 'Image'

# Starting coordinate, here (100, 100)
# Represents the top left corner of rectangle
starting_point = (100, 100)

# Ending coordinate, here (400, 400)
# Represents the bottom right corner of rectangle
ending_point = (400, 400)

# Blue color in BGR
color = (255, 0, 0)

# Line thickness of 2 px
thickness = 2

# Draw a rectangle with blue line borders of thickness of 2 px
image = cv2.rectangle(image, starting_point, ending_point, color, thickness)

# Displaying the image
cv2.imshow(window_name, image)
cv2.waitKey(0)

# Closing all open windows
cv2.destroyAllWindows()

Output

 

Python cv2 rectangle

In this example, first, we have imported the cv2 module and then defined the input image path.

Then we are reading the image using the cv2.imread() method.

In the next step, we defined the window name, which is Image.

Then defined the starting point and ending point. These are coordinates tells the function that when to start drawing a rectangle and where to end.

In the next step, we defined the color of the rectangle and thickness.

To display the image, we used cv2.imshow() method.

To keep the image window, we have used the waitKey() function.

We passed 0 to waitKey() method that means it will remain open forever until we said otherwise.

That is it for the Python cv2.rectangle() function.

Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.