2017-05-21 8 views
1

目的は画像上でマウスを2回クリックして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)が捕捉される。

enter image description here

私はここで何をしないのですか?

答えて

0

事はあなたがevent.xevent.yを使用していることである。

  • X:位置x - キャンバスの左からのピクセル

  • のy:yの位置 - キャンバスの下からのピクセル

マウスの座標をデータ座標にしたい場合は、

  • XDATA:データCOORDSにおけるマウスのX座標
  • YDATA:

docを参照して、データCOORDSにおけるマウスのy COORD。

関連する問題