Python OpenCV cv2.rectangle () method

| | | | | | | | | | | | | | | |

👻 Check our latest review to choose the best laptop for Machine Learning engineers and Deep learning tasks!

OpenCV-Python — is a Python bindings library for solving computer vision problems. cv2.rectangle () 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  ( X coordinate value, Y coordinate value).
end_point: It is the ending coordinates of rectangle. The coordinates are represented as tuples of two values ( 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.

Example # 1:

# Python program to explain cv2.rectangle() method

# importing cv2
import cv2

# path
path = r’C:UsersRajnishDesktopgeeksforgeeksgeeks.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)

cv2.rectangle

Example # 2:

Using thickness -1 px to fill the rectangle with black.

# Python program to explain cv2.rectangle() method
	
# importing cv2
import cv2
	
# path
path = r’C:UsersRajnishDesktopgeeksforgeeksgeeks.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)


cv2 rectangle

opencv cv2.rectangle

he cv2.rectangle function is to draw a simple rectangle on the image. The cv2.rectangle function definition given on the opencv official site is as follows:

Python: cv2.rectangle(img, pt1, pt2, color[, thickness[, lineType[, shift]]]) → None

  • img – Image.
  • pt1 – Vertex of the rectangle.
  • pt2 – Vertex of the rectangle opposite to pt1 .
  • color – Rectangle color or brightness (grayscale image).
  • thickness – Thickness of lines that make up the rectangle. Negative
    values, like CV_FILLED , mean that the function has to draw a filled
    rectangle.
  • lineType – Type of the line. See the line() description.
    — 8 (or omitted) - 8-connected line.
    — 4 - 4-connected line.
  • —CV_AA - antialiased line.
  • shift – Number of fractional bits in the point coordinates.**

👻 Read also: what is the best laptop for engineering students?

We hope this article has helped you to resolve the problem. Apart from Python OpenCV cv2.rectangle () method, check other code Python module-related topics.

Want to excel in Python? See our review of the best Python online courses 2023. If you are interested in Data Science, check also how to learn programming in R.

By the way, this material is also available in other languages:



Angelo Nickolson

Berlin | 2023-02-07

Simply put and clear. Thank you for sharing. Python OpenCV cv2.rectangle () method and other issues with site Python module was always my weak point 😁. I just hope that will not emerge anymore

Manuel Nickolson

Prague | 2023-02-07

Thanks for explaining! I was stuck with Python OpenCV cv2.rectangle () method for some hours, finally got it done 🤗. Will get back tomorrow with feedback

Boris Jackson

Munchen | 2023-02-07

Maybe there are another answers? What Python OpenCV cv2.rectangle () method exactly means?. I am just not quite sure it is the best method

Shop

Gifts for programmers

Learn programming in R: courses

$FREE
Gifts for programmers

Best Python online courses for 2022

$FREE
Gifts for programmers

Best laptop for Fortnite

$399+
Gifts for programmers

Best laptop for Excel

$
Gifts for programmers

Best laptop for Solidworks

$399+
Gifts for programmers

Best laptop for Roblox

$399+
Gifts for programmers

Best computer for crypto mining

$499+
Gifts for programmers

Best laptop for Sims 4

$

Latest questions

PythonStackOverflow

Common xlabel/ylabel for matplotlib subplots

1947 answers

PythonStackOverflow

Check if one list is a subset of another in Python

1173 answers

PythonStackOverflow

How to specify multiple return types using type-hints

1002 answers

PythonStackOverflow

Printing words vertically in Python

909 answers

PythonStackOverflow

Python Extract words from a given string

798 answers

PythonStackOverflow

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

606 answers

PythonStackOverflow

Python os.path.join () method

384 answers

PythonStackOverflow

Flake8: Ignore specific warning for entire file

360 answers

News


Wiki

Python | How to copy data from one Excel sheet to another

Common xlabel/ylabel for matplotlib subplots

Check if one list is a subset of another in Python

How to specify multiple return types using type-hints

Printing words vertically in Python

Python Extract words from a given string

Cyclic redundancy check in Python

Finding mean, median, mode in Python without libraries

Python add suffix / add prefix to strings in a list

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

Python - Move item to the end of the list

Python - Print list vertically