gpt4 book ai didi

python - 将鼠标限制在 matplotlib 图中的 Canvas 区域

转载 作者:太空宇宙 更新时间:2023-11-04 01:05:38 28 4
gpt4 key购买 nike

我正在尝试编写自己的裁剪函数,以便我可以从较大的图像中选择一个区域,然后对其执行一些其他操作。我使用 matplotlib 导入和显示图像:self.img=plt.imread("someimage.png")

如果我留在 Canvas 区域,效果很好:

 def __onclick__(self, event):

self.point=[]
if event.xdata is not None:
xholder=event.xdata
else:
xholder = 0
if event.ydata is not None:
yholder = event.ydata
else:
yholder = 0
self.point.append([int(xholder),int(yholder)])
def __onrelease__(self,event):
if event.xdata is not None:
xholder=event.xdata
else:
xholder = self.img.shape[0]
if event.ydata is not None:
yholder = event.ydata
else:
yholder = self.img.shape[1]
self.point.append([int(xholder),int(yholder)])
self.plotcanvas.axes.imshow(self.img[self.point[0][1]:self.point[1][1],self.point[0][0]:self.point[1][0]])
self.plotcanvas.draw()

但是,一旦我走出 Canvas 边界,event.xdataevent.ydata 就会返回 (None,None)。一旦在 Canvas 区域中注册了鼠标单击,是否可以将鼠标移动限制在 Canvas 区域?

使用允许导入 .png 文件然后裁剪的代码进行编辑

import sys
from PyQt4 import QtGui, QtCore
from matplotlib import pyplot as plt
from matplotlib.backends.backend_qt4agg import FigureCanvasQTAgg as FigureCanvas
from matplotlib.backends.backend_qt4agg import NavigationToolbar2QTAgg as NavigationToolbar
import numpy as np


class Example(QtGui.QMainWindow):

def __init__(self):
super(Example, self).__init__()
self.initUI()

def initUI(self):
self.statusBar()
self.form_widget=FormWidget(self)
self.setCentralWidget(self.form_widget)
openFile = QtGui.QAction('Open', self)
openFile.setShortcut('Ctrl+O')
openFile.setStatusTip('Open new File')
openFile.triggered.connect(self.showDialog)
toolbar = self.addToolBar('Exit')
toolbar.addAction(openFile)
self.show()

def showDialog(self):

fname = QtGui.QFileDialog.getOpenFileName(self, 'Open file',
'/home')

self.form_widget.redraw(fname)

class FormWidget(QtGui.QWidget):
def __init__(self,parent):
super(FormWidget,self).__init__()
self.layout=QtGui.QVBoxLayout(self)
self.mpcanvas=MyMplCanvas(self)
self.plotcanvas=MyMplCanvas(self)
self.layout.addWidget(self.plotcanvas)
self.layout.addWidget(self.mpcanvas)
self.setLayout(self.layout)
self.cidpresscsv=self.mpcanvas.fig.canvas.mpl_connect('button_press_event', self.__onclick__)
self.cidreleasecsv=self.mpcanvas.fig.canvas.mpl_connect('button_release_event', self.__onrelease__)
def redraw(self,fname):
self.mpcanvas.axes=self.mpcanvas.fig.add_subplot(111)
self.img=plt.imread(fname)
self.mpcanvas.axes.imshow(self.img)
self.mpcanvas.draw()
def __onclick__(self, event):
self.point=[]
if event.xdata is not None:
xholder=event.xdata
else:
xholder = 0
if event.ydata is not None:
yholder = event.ydata
else:
yholder = 0
self.point.append([int(xholder),int(yholder)])
def __onrelease__(self,event):
if event.xdata is not None:
xholder=event.xdata
else:
xholder = self.img.shape[0]
if event.ydata is not None:
yholder = event.ydata
else:
yholder = self.img.shape[1]
self.point.append([int(xholder),int(yholder)])
self.plotcanvas.axes.imshow(self.img[self.point[0][1]:self.point[1][1],self.point[0][0]:self.point[1][0]])
self.plotcanvas.draw()




class MyMplCanvas(FigureCanvas):
def __init__(self, parent=None, width=5, height=4, dpi=100):
self.fig = plt.figure(figsize=(width, height), dpi=dpi)
self.axes = self.fig.add_subplot(111)
self.fig.tight_layout()
self.axes.hold(False)
self.compute_initial_figure()
FigureCanvas.__init__(self, self.fig)
self.setParent(parent)
FigureCanvas.setSizePolicy(self,
QtGui.QSizePolicy.Expanding,
QtGui.QSizePolicy.Expanding)
FigureCanvas.updateGeometry(self)

def compute_initial_figure(self):
pass



def main():

app = QtGui.QApplication(sys.argv)
ex = Example()
sys.exit(app.exec_())


if __name__ == '__main__':
main()

这是在删除了所有不必要的东西之后出现的结果,并且仍然制作了一个功能程序,让您可以导入一个 .png 文件,然后从中挖出一个区域。我确信 Ed Smith 提出的 Rectangle 选择器可能会起作用,但我在使示例代码起作用时遇到了问题,并且对值的存储位置以及 eclick 和 erelease 如何在上下文中与其他事件处理程序一起工作感到困惑。恐怕我一般不太精通 matplotlib 和 python。

最佳答案

框选择器matplotlib widget可能是最简单的方法。您可以将代码与您拥有的图像一起使用:

from matplotlib.widgets import  RectangleSelector
from pylab import *

def onselect(eclick, erelease):
'eclick and erelease are matplotlib events at press and release'
print ' startposition : (%f, %f)' % (eclick.xdata, eclick.ydata)
print ' endposition : (%f, %f)' % (erelease.xdata, erelease.ydata)
print ' used button : ', eclick.button

def toggle_selector(event):
print ' Key pressed.'
if event.key in ['Q', 'q'] and toggle_selector.RS.active:
print ' RectangleSelector deactivated.'
toggle_selector.RS.set_active(False)
if event.key in ['A', 'a'] and not toggle_selector.RS.active:
print ' RectangleSelector activated.'
toggle_selector.RS.set_active(True)

fig = figure
ax = subplot(111)

A = np.random.random((30,30))
ax.imshow(A)
toggle_selector.RS = RectangleSelector(ax, onselect, drawtype='box')
connect('key_press_event', toggle_selector)
show()

虽然鼠标不限于框,但是当 minspanxminspanyNone 时的默认行为是限制返回的坐标到盒子。我不认为您可以轻松地将鼠标移动限制在 Canvas 上。

关于python - 将鼠标限制在 matplotlib 图中的 Canvas 区域,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30619193/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com