Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/298.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 想在while循环中获得鼠标坐标吗_Python_Matplotlib_Click_Mouse_Coordinate - Fatal编程技术网

Python 想在while循环中获得鼠标坐标吗

Python 想在while循环中获得鼠标坐标吗,python,matplotlib,click,mouse,coordinate,Python,Matplotlib,Click,Mouse,Coordinate,我想得到几个图像的鼠标点击坐标。这是我的代码: import matplotlib.pyplot as plt j = 0 while j < nb_images: plt.ion() fig = plt.figure() coords = [] #Affichage plt.imshow(img[j], cmap="gray") plt.draw() while len(coords) <1: cid =

我想得到几个图像的鼠标点击坐标。这是我的代码:

import matplotlib.pyplot as plt

j = 0
while j < nb_images:
    plt.ion()
    fig = plt.figure()
    coords = []
    #Affichage
    plt.imshow(img[j], cmap="gray")
    plt.draw()

    while len(coords) <1:
        cid = fig.canvas.mpl_connect('button_press_event', onclick)

    print(coords[0][0], coords[0][1])
j = j + 1

def onclick(event):
    global ix, iy
    ix, iy = event.xdata, event.ydata
    global coords
    coords.append((ix, iy))
    if len(coords) == 1:
        fig.canvas.mpl_disconnect(cid)
        plt.close()
    return coords
导入matplotlib.pyplot作为plt
j=0
而j虽然len(coords)我试图回答,但我无法测试,无法在windows上获得mathplotlib,因此我确信它不能按原样工作,但至少它纠正了创建内容和回调的方式

发布的代码有许多缺陷:

  • 它依赖于一种互动的做事方式。Mathplotlib定义了回调,并有一个需要调用的mainloop(
    plt.show()
  • 有一个无限循环,因为j增量在
    循环之外,而
    循环。我用
    for
    简化了循环
  • 所有操作都应在
    onclick
    回调中执行
请随意编辑

import matplotlib.pyplot as plt
i = 0

def onclick(event):
    global ix, iy, i
    ix, iy = event.xdata, event.ydata
    global coords
    coords.append((ix, iy))
    print("clicked "+str(coords))
    i+=1
    if i == nb_images:
         plt.close()
         fig.canvas.mpl_disconnect(cid)
         # then do something with the coords array
    else:
         # show next image and go one
         plt.imshow(img[i], cmap="gray")

plt.ion()
fig = plt.figure()

plt.imshow(img[0], cmap="gray")


fig.canvas.mpl_connect('button_press_event', onclick)
plt.draw()
plt.show()

导入丢失,示例不完整。您使用的是哪种图形框架?是的,这是代码的一部分。我使用import matplotlib.pyplot作为plt刚看到一个未缩进的
j=j+1
语句。如果是这样,它锁定是正常的:无限循环,从未达到条件。请修复缩进。它不能是c当我缩进j=j+1行时,图像会一起显示。但是我想对每个图像(即每个“j”)执行以下步骤:1)显示图2)单击图3)获取坐标。只有在这之后,我才移动到下一个图像来执行相同的步骤,这样所有的图像都会在一次中显示出来。我想展示一个。。。我有一百多张照片,我怀疑。我将尝试更新答案。记住,我没有要测试的mathplotlib。