python - Python获取多个Button点击事件的方法

标签 python opencv matplotlib

我正在使用以下代码来检测鼠标按钮事件中图像中的坐标:

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/

相关文章:

python - Python 中奇怪的时间问题

每个类别的Python pptx自定义颜色

c++ - OpenCV:是否可以在 Linux 中的进程之间共享网络摄像头

opencv - 修改网络摄像头流

python - 使用生成器时设置 matplotlib 动画的持续时间

python - Matplotlib 颜色条 : __init__() got an unexpected keyword argument 'location'

python - python 中的 Animate 显示静态图

python - 如何从 numpy ndarray 中删除特定值

python - 检查由 Twisted 的 FTPClient.retrieveFile 方法检索的文件的完整性

python - 在 3 channel 阵列上创建 numpy 掩码阵列永远行不通