使用带有ginput和imshow的matplotlib轴

时间:2011-12-28 07:28:35

标签: python qt pyqt matplotlib

我正在尝试使用ginput来获取用户选择的多边形。我知道如何用pylab做到这一点。扭曲的是我试图用一个嵌入到PyQt gui中的图形来做这个(使pylab很难)。我想只是让用户点击已经嵌入的图像上的点(右图)。

以下是代码:

import numpy as np
import dicom

from pylab import ginput, show, rand, imshow, gcf
import pylab
import matplotlib
from matplotlib.backends.backend_qt4agg import FigureCanvasQTAgg as FigureCanvas
from matplotlib.backends.backend_qt4agg import NavigationToolbar2QTAgg as NavigationToolbar
from matplotlib.figure import Figure

class MyMplCanvas(FigureCanvas):
    """Ultimately, this is a QWidget (as well as a FigureCanvasAgg, etc.)."""
    def __init__(self, parent=None, width=5, height=4, dpi=100, data=None):
        self.figure = Figure(figsize=(width, height), dpi=dpi)
        self.axes = self.figure.add_subplot(111)
        # We want the axes cleared every time plot() is called
        self.axes.hold(False)
        self.data = data

        if data is not None:
            self.compute_initial_figure()

        #
        FigureCanvas.__init__(self, self.figure)
        self.setParent(parent)

        FigureCanvas.setSizePolicy(self,
                                   QtGui.QSizePolicy.Expanding,
                                   QtGui.QSizePolicy.Expanding)
        FigureCanvas.updateGeometry(self)

    def compute_initial_figure(self):
        pass

class MyDynamicMplCanvas(MyMplCanvas):
    """A canvas that updates itself every second with a new plot."""
    def __init__(self, *args, **kwargs):
        MyMplCanvas.__init__(self, *args, **kwargs)
        # timer = QtCore.QTimer(self)
        # QtCore.QObject.connect(timer, QtCore.SIGNAL("timeout()"), self.update_figure)
        # timer.start(1000)

    def compute_initial_figure(self):
        self.axes.imshow(self.data)

    def update_figure(self):
        print('haha')
        if self.data is not None:
            print('lol')
            FigureCanvas.updateGeometry(self)
            self.axes.clear()
            self.axes.imshow(self.data)
            self.draw()

class MeasureGui(QtGui.QMainWindow):

    def __init__(self, parent=None):
        QtGui.QWidget.__init__(self, parent)
        self.ui = Ui_MainWindow()
        self.ui.setupUi(self)

        self.ui.canvas1 = MyDynamicMplCanvas(self.ui.centralwidget, dpi=100, data=self.pixData)
        initThreImg = self.pixData > (self.ui.horizontalSlider.value() / 100.0)
        self.ui.canvas2 = MyDynamicMplCanvas(self.ui.centralwidget, dpi=100, data=initThreImg)

        self.ui.splitter_2.addWidget(self.ui.canvas1)
        self.ui.splitter_2.addWidget(self.ui.canvas2)

   def measurePoly(self):
        imshow(self.ui.canvas2.data)
        pointList = self.ui.canvas2.figure.ginput(1000, mouse_stop=3, mouse_pop=2)

最后一行是棘手的一点。我可以使用pylab来完成它,但这会启动一个新窗口,我想用嵌入式self.ui.canvas2.figure_or_axes来做。

Lemme知道你们的想法。

谢谢,

tylerthemiler

enter image description here

0 个答案:

没有答案