2

I am trying to use ginput to get a user-selected polygon. I know how to do this with pylab. The twist is that I am trying to do this with a figure that I have embedded into a PyQt gui (making the pylab way difficult). I want to just to allow the user to click point on the image already embedded (the right image).

Here is the code:

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)

The last line is the tricky point. I can do it using pylab, but this launches a new window, and I want to do it with the embedded self.ui.canvas2.figure_or_axes.

Lemme know what you guys think.

Thanks,

tylerthemiler

enter image description here

Zephyr
  • 11,891
  • 53
  • 45
  • 80
tylerthemiler
  • 5,496
  • 6
  • 32
  • 40
  • Hi!. Please, remember to delete the part now posted here from your previous question. Btw, note that you do not need the pylab and matplotlib imports in the current code – joaquin Dec 29 '11 at 06:46
  • ...but you do need the Qt imports. – joaquin Dec 29 '11 at 06:53
  • Sorry, I can not adequately revise your code. You should work on it to make it functional but keeping it as minimal as possible. Otherwise it can not be tested. There are things wrong or with partial data. For example where Ui_MainWindow comes from ?. – joaquin Dec 29 '11 at 18:55
  • I think I found a solution, is it kosher to answer your own question after a few days?? – tylerthemiler Dec 29 '11 at 21:45
  • I think you can fill an answer to your own question whenever you want. Let's see what you got! – joaquin Dec 29 '11 at 22:10
  • Yes, it is acceptable to answer your own question. Remember to make it understandable to others who might come across this question in the future. – pelson Aug 11 '12 at 12:28
  • What was the answer? I have the same problem too. – martinako Dec 28 '15 at 18:19

0 Answers0