python cv rectangle
时间: 2023-10-19 19:05:57 浏览: 91
OpenCV计算机视觉基础教程(Python版)(慕课版)-(1).pptx
To draw a rectangle using OpenCV in Python, you can use the following code snippet:
```python
import cv2
# Load an image
image = cv2.imread("image.png")
# Define the rectangle coordinates
x, y = 50, 50 # top-left corner coordinates
width, height = 200, 150 # width and height of the rectangle
# Draw the rectangle on the image
color = (0, 255, 0) # BGR color format (here, green color)
thickness = 2 # thickness of the rectangle border
cv2.rectangle(image, (x, y), (x + width, y + height), color, thickness)
# Display the image with the rectangle
cv2.imshow("Image with Rectangle", image)
cv2.waitKey(0)
cv2.destroyAllWindows()
```
Make sure to replace "image.png" with the actual path to your image file. This code will draw a rectangle on the image starting from the top-left corner (x, y) and having the specified width and height. The color of the rectangle is set to green in this example.
阅读全文