我正在尝试编写自己的裁剪功能,因此我可以从较大的图像中选择一个区域,然后对其执行一些其他操作。我使用matplotlib导入和显示图像:self.img=plt.imread("someimage.png")
如果我留在画布区域,这可以正常工作:
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()
然而,当我走出画布边界event.xdata
和event.ydata
时,返回(无,无)。一旦鼠标单击在画布区域中注册,是否仍然只将鼠标移动限制在画布区域?
使用允许导入.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。
答案 0 :(得分:3)
框选择器matplotlib widget可能是最简单的方法。您可以将代码与您拥有的图像一起使用:
<?
ignore_user_abort();
ob_start();
// do stuff, generate output
// get size of the content
$length = ob_get_length();
// tell client to close the connection after $length bytes received
header('Connection: close');
header("Content-Length: $length");
// flush all output
ob_end_flush();
flush();
// close session if you have one ...
// continue your processing tasks ...
?>
虽然鼠标不限于此框,但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()
和minspanx
为minspany
时的默认行为是将返回的坐标限制在框中。我不认为你可以很容易地将鼠标移动限制在画布上。