👻 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)![]()
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)
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:
- Italiano Python OpenCV cv2.rectangle () method
- Deutsch Python OpenCV cv2.rectangle () method
- Français Python OpenCV cv2.rectangle () method
- Español Python OpenCV cv2.rectangle () method
- Türk Python OpenCV cv2.rectangle () method
- Русский Python OpenCV cv2.rectangle () method
- Português Python OpenCV cv2.rectangle () method
- Polski Python OpenCV cv2.rectangle () method
- Nederlandse Python OpenCV cv2.rectangle () method
- 中文 Python OpenCV cv2.rectangle () method
- 한국어 Python OpenCV cv2.rectangle () method
- 日本語 Python OpenCV cv2.rectangle () method
- हिन्दी Python OpenCV cv2.rectangle () method
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
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
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