python的cv2的交互式曲线绘制代码
时间: 2023-10-10 20:04:34 浏览: 66
VC曲线绘制源代码
好的,以下是一个使用 OpenCV(cv2)库实现的交互式曲线绘制代码,您可以参考一下:
```python
import cv2
import numpy as np
# 定义画布大小
img = np.zeros((512, 512, 3), np.uint8)
# 定义初始点
drawing = False
ix, iy = -1, -1
# 定义回调函数
def draw(event, x, y, flags, param):
global ix, iy, drawing
if event == cv2.EVENT_LBUTTONDOWN:
drawing = True
ix, iy = x, y
elif event == cv2.EVENT_MOUSEMOVE:
if drawing == True:
cv2.line(img, (ix, iy), (x, y), (0, 255, 0), thickness=2)
ix, iy = x, y
elif event == cv2.EVENT_LBUTTONUP:
drawing = False
cv2.line(img, (ix, iy), (x, y), (0, 255, 0), thickness=2)
# 定义窗口和回调函数
cv2.namedWindow('image')
cv2.setMouseCallback('image', draw)
# 循环显示画布
while True:
cv2.imshow('image', img)
if cv2.waitKey(1) & 0xFF == 27:
break
cv2.destroyAllWindows()
```
运行代码后,会弹出一个画布窗口,您可以使用鼠标在画布上绘制曲线。按下 Esc 键可退出程序。
阅读全文