The Wayback Machine - https://web.archive.org/web/20250118004100/https://www.geeksforgeeks.org/python-opencv-cv2-rectangle-method/
Open In App

Python OpenCV | cv2.rectangle() method

Last Updated : 12 Aug, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

OpenCV-Python

is a library of Python bindings designed to solve computer vision problems.

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 image on which rectangle is to be drawn. start_point: It is the starting coordinates of rectangle. The coordinates are represented as tuples of two values i.e. (X coordinate value, Y coordinate value). end_point: It is the ending coordinates of rectangle. The coordinates are represented as tuples of two values i.e. (X coordinate value, Y coordinate value). color: It is the color of border line of rectangle to be drawn. For BGR, we pass a tuple. eg: (255, 0, 0) for blue color. thickness: It is the thickness of the rectangle border line in px. Thickness of -1 px will fill the rectangle shape by the specified color. Return Value: It returns an image.

Image used for all the below examples:

Example #1:

Python3 1==
# Python program to explain cv2.rectangle() method 
 
# importing cv2 
import cv2 
 
# path 
path = r'C:\Users\Rajnish\Desktop\geeksforgeeks\geeks.png'
 
# Reading an image in default mode
image = cv2.imread(path)
 
# Window name in which image is displayed
window_name = 'Image'

# Start coordinate, here (5, 5)
# represents the top left corner of rectangle
start_point = (5, 5)

# Ending coordinate, here (220, 220)
# represents the bottom right corner of rectangle
end_point = (220, 220)

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

# Line thickness of 2 px
thickness = 2

# Using cv2.rectangle() method
# Draw a rectangle with blue line borders of thickness of 2 px
image = cv2.rectangle(image, start_point, end_point, color, thickness)

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

Output:

Example #2:

Using thickness of -1 px to fill the rectangle by black color.

Python3 1==
# Python program to explain cv2.rectangle() method 
  
# importing cv2 
import cv2 
  
# path 
path = r'C:\Users\Rajnish\Desktop\geeksforgeeks\geeks.png'
  
# Reading an image in grayscale mode
image = cv2.imread(path, 0)
  
# Window name in which image is displayed
window_name = 'Image'
 
# Start coordinate, here (100, 50)
# represents the top left corner of rectangle
start_point = (100, 50)
 
# Ending coordinate, here (125, 80)
# represents the bottom right corner of rectangle
end_point = (125, 80)
 
# Black color in BGR
color = (0, 0, 0)
 
# Line thickness of -1 px
# Thickness of -1 will fill the entire shape
thickness = -1
 
# Using cv2.rectangle() method
# Draw a rectangle of black color of thickness -1 px
image = cv2.rectangle(image, start_point, end_point, color, thickness)
 
# Displaying the image 
cv2.imshow(window_name, image) 

Output:

Python OpenCV | cv2.rectangle() method – FAQs

What is OpenCV Method?

OpenCV (Open Source Computer Vision Library) methods refer to the functions and techniques used within the OpenCV library to perform operations related to computer vision. OpenCV provides a wide range of functions that can handle tasks such as image processing, video capture, feature extraction, object detection, and more. Each function in OpenCV, whether it’s for transforming an image, detecting faces, or tracking movements, can be considered a method of OpenCV.

How to Call OpenCV in Python?

To use OpenCV in Python, you first need to install the package, usually named opencv-python. This can be installed via pip:

pip install opencv-python

Once installed, you can import and use OpenCV in your Python scripts by importing cv2, which is the Python interface for OpenCV:

import cv2

# Example: Loading and displaying an image
image = cv2.imread('path_to_image.jpg')
cv2.imshow('Image Window', image)
cv2.waitKey(0)
cv2.destroyAllWindows()

How to Save an Image with OpenCV?

To save an image using OpenCV after performing operations on it, you can use the cv2.imwrite() function. This function writes an image to a specified file.

import cv2

image = cv2.imread('input.jpg') # Read an image
cv2.imwrite('output.jpg', image) # Save the image to a file

The imwrite() function takes two arguments: the file path where the image will be saved and the image data.

What is the Line Function in OpenCV?

The line function in OpenCV is used to draw a line on an image. It can specify the line’s thickness, color, and line type.

import cv2
import numpy as np

# Create a blank image
image = np.zeros((300, 300, 3), dtype=np.uint8)

# Draw a line on the image
# cv2.line(image, start_point, end_point, color, thickness)
cv2.line(image, (50, 50), (250, 250), (0, 255, 0), 5)

cv2.imshow('Line', image)
cv2.waitKey(0)
cv2.destroyAllWindows()

In this example, a green line is drawn from the point (50, 50) to (250, 250) on a black background. The line’s color is specified in BGR (0, 255, 0) and the thickness is 5 pixels.




Next Article
Article Tags :
Practice Tags :

Similar Reads

three90RightbarBannerImg