cv2.rectangle
时间: 2023-10-16 17:10:58 浏览: 108
cv2
cv2.rectangle() is a function from the OpenCV library in Python, used to draw a rectangle on an image. It takes the following parameters:
- image: The input image on which the rectangle is to be drawn. This should be a NumPy array.
- pt1: The top-left corner of the rectangle specified as a tuple of (x,y) coordinates.
- pt2: The bottom-right corner of the rectangle specified as a tuple of (x,y) coordinates.
- color: The color of the rectangle in BGR format, specified as a tuple of (blue, green, red) values.
- thickness: The thickness of the rectangle border. A negative value will fill the rectangle with the specified color.
- lineType: The type of line used to draw the rectangle. This can be cv2.LINE_4, cv2.LINE_8, or cv2.LINE_AA.
- shift: The number of fractional bits in the point coordinates.
Here is an example usage of cv2.rectangle():
```
import cv2
# Load an image
img = cv2.imread('image.jpg')
# Draw a red rectangle on the image
pt1 = (100, 100)
pt2 = (200, 200)
color = (0, 0, 255) # BGR
thickness = 2
cv2.rectangle(img, pt1, pt2, color, thickness)
# Display the image
cv2.imshow('Image', img)
cv2.waitKey(0)
cv2.destroyAllWindows()
```
阅读全文