使用OpenCV画折线图
时间: 2023-07-11 07:14:56 浏览: 68
要使用 OpenCV 画折线图,可以使用以下步骤:
1. 导入所需的库和模块:
```python
import numpy as np
import cv2
```
2. 创建一个空白的图像:
```python
width = 800
height = 600
img = np.zeros((height, width, 3), np.uint8)
```
3. 创建一个包含折线图数据的 NumPy 数组:
```python
data = np.array([[100, 200], [200, 400], [300, 300], [400, 500], [500, 200]], np.int32)
```
4. 使用 `cv2.polylines()` 函数画出折线:
```python
thickness = 2
color = (0, 255, 0)
cv2.polylines(img, [data], False, color, thickness)
```
5. 显示图像:
```python
cv2.imshow('image', img)
cv2.waitKey(0)
cv2.destroyAllWindows()
```
完整的代码示例:
```python
import numpy as np
import cv2
width = 800
height = 600
img = np.zeros((height, width, 3), np.uint8)
data = np.array([[100, 200], [200, 400], [300, 300], [400, 500], [500, 200]], np.int32)
thickness = 2
color = (0, 255, 0)
cv2.polylines(img, [data], False, color, thickness)
cv2.imshow('image', img)
cv2.waitKey(0)
cv2.destroyAllWindows()
```
输出的图像应该包含一条绿色的折线。