1
目標是在圖像上單擊我的鼠標兩次並檢索2個座標。代碼如下:Matplotlib捕獲錯誤的鼠標座標
class ImageListener(object):
def __init__(self):
self.coordinates = []
def onclick(self, event):
print(event.x, event.y)
self.coordinates.extend([event.x, event.y])
def show_image(self, img, close=True):
fig = plt.figure()
ax = fig.add_subplot(111)
ax.imshow(img)
cid = fig.canvas.mpl_connect('button_press_event', self.onclick)
while len(self.coordinates) < 3:
plt.waitforbuttonpress(0)
if close:
plt.close(fig)
fig.canvas.mpl_disconnect(cid)
但是,捕獲的座標是錯誤的。在下圖中,當我點擊時,我的鼠標位於(700,333)處。 (鼠標在屏幕截圖中消失)。相反,(514,154)被捕獲。
缺少什麼我在這裏?