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.**
