我正在写一个处理图像的课程。在该类中,我想定义一个方法,该方法可以让我返回鼠标单击的坐标。我可以将坐标作为属性获取,但是如果我调用方法返回坐标,则会得到一个空元组
这是代码:
import cv2
import matplotlib.pyplot as plt
class TestClass():
def __init__(self):
self.fname = 'image.jpg'
self.img = cv2.imread(self.fname)
self.point = ()
def getCoord(self):
fig = plt.figure()
ax = fig.add_subplot(111)
plt.imshow(self.img)
cid = fig.canvas.mpl_connect('button_press_event', self.__onclick__)
return self.point
def __onclick__(self,click):
self.point = (click.xdata,click.ydata)
return self.point
只要我plt.show()
在mpl_connect
中插入以下代码,您的代码就对我有用getCoord
:
def getCoord(self):
fig = plt.figure()
ax = fig.add_subplot(111)
plt.imshow(self.img)
cid = fig.canvas.mpl_connect('button_press_event', self.__onclick__)
plt.show()
return self.point
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句