我正在使用以下代码来检测鼠标按钮事件中图像中的坐标:
plt.imshow(img,origin="upper")
connect('button_press_event', on_click)
plt.show()
def on_click(event):
# get the x and y coords, flip y from top to bottom
x, y = event.x, event.y
我这里有两个问题。
首先, 尝试在定义“on_click”中返回 x,y 时,我必须存储变量 x,y,因为它们无法直接返回。有没有更好的方法来返回这些变量?
我试过使用下面的函数,但它不能返回值。
cid= implot.figure.canvas.mpl_connect('button_press_event',onclick)
其次,我想从同一张图片中获取多个点的坐标。 你能告诉我怎么做吗?
我可以通过任何不同的来源显示/绘制图像。我的主要任务是从 OpenCV 生成的图像中读取坐标。
最佳答案
您可能想交互式地选择几个点,然后对 x、y 数据做些什么?
如果是这样,您可以考虑改用 plt.ginput()
。
例如:
import matplotlib.pyplot as plt
fig, ax = plt.subplots()
ax.set(title='Draw a 4-sided polygon by clicking points')
data = plt.ginput(4)
data.append(data[0])
ax.autoscale(False)
ax.fill(*zip(*data))
plt.show()
关于python - Python获取多个Button点击事件的方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27945307/