class PreviewTomography(QDialog):
    def __init__(self, inspection, scenario, transducer, parent=None):
        super(PreviewTomography, self).__init__(parent)
        self.parent = parent

        self.mpl = MplCanvas(width=5, height=5, dpi=100)
        self.mpl.ax.axis('off')

        layout = QVBoxLayout()
        layout.addWidget(self.mpl)

        self.setLayout(layout)
        self.setWindowFlags(self.windowFlags() | Qt.WindowStaysOnTopHint)
        self.setWindowTitle(self.tr("Preview"))
        self.setWindowIcon(QIcon(":/previewImage.png"))

        self.inspection = inspection
        self.scenario = scenario
        self.transducer = transducer
        QTimer.singleShot(300, self.Show)

    def Show(self):
        M, N = np.shape(self.scenario.I)
        Pixel_mm = self.scenario.Pixel_mm
        DiameterRing = self.inspection.DiameterRing
        Theta = self.inspection.Theta

        XL, YL = self.inspection.view(M, N, DiameterRing, Pixel_mm, Theta,
                                      self.transducer)
        self.mpl.ax.imshow(self.scenario.I, vmin=0, vmax=255)
        self.mpl.ax.hold(True)
        self.mpl.ax.plot(YL, XL, 'ks')
        self.mpl.ax.axis('off')
        self.mpl.ax.hold(False)
        self.mpl.draw()
class PreviewLinearScan(QDialog):
    def __init__(self, method, inspection, scenario, transducer, parent=None):
        super(PreviewLinearScan, self).__init__(parent)
        self.parent = parent

        self.mpl = MplCanvas(width=5, height=5, dpi=100)
        self.mpl.ax.axis('off')

        layout = QVBoxLayout()
        layout.addWidget(self.mpl)

        self.setLayout(layout)

        self.setWindowFlags(self.windowFlags() | Qt.WindowStaysOnTopHint)
        self.setWindowTitle(self.tr("Preview"))
        self.setWindowIcon(QIcon(":/previewImage.png"))

        self.method = method
        self.inspection = inspection
        self.scenario = scenario
        self.transducer = transducer
        QTimer.singleShot(300, self.Show)

    def Show(self):
        M, N = np.shape(self.scenario.I)
        Pixel_mm = self.scenario.Pixel_mm

        for offset in self.inspection.ScanVector:
            self.transducer.CenterOffset = offset
            XL, YL = self.method.view(M, N, Pixel_mm, self.method.Theta,
                                      self.transducer)
            self.mpl.ax.imshow(self.scenario.I, vmin=0, vmax=255)
            self.mpl.ax.hold(True)
            self.mpl.ax.plot(YL, XL, 'ks')
            self.mpl.ax.axis('off')
            self.mpl.ax.hold(False)
            self.mpl.draw()
            QCoreApplication.processEvents()
            time.sleep(0.25)
Example #3
0
class SignalSetup(QDialog):
    def __init__(self, Signal=None, parent=None):
        super(SignalSetup, self).__init__(parent)

        self.widget = SignalSetupWidget()

        self.buttonBox = QDialogButtonBox(QDialogButtonBox.Ok
                                          | QDialogButtonBox.Cancel)
        self.connect(self.buttonBox, SIGNAL("accepted()"), self.accept)
        self.connect(self.buttonBox, SIGNAL("rejected()"), self.reject)

        self.widget.previewPushButton.pressed.connect(self.preview)
        self.widget.typeComboBox.currentIndexChanged.connect(
            self.handleTypeSignal)
        self.widget.cyclesLabel.setVisible(False)
        self.widget.cyclesDoubleSpinBox.setVisible(False)

        self.mpl = MplCanvas(width=2, height=2, dpi=100)
        self.mpl.ax.axis("off")

        w = QWidget(self)
        h = QVBoxLayout()
        h.addWidget(self.mpl)
        w.setLayout(h)

        hBox = QHBoxLayout()
        hBox.addWidget(w)
        hBox.addWidget(self.widget)

        vBox = QVBoxLayout()
        vBox.addLayout(hBox)
        vBox.addWidget(self.buttonBox)

        self.setLayout(vBox)
        # On Top
        self.setWindowFlags((self.windowFlags() | Qt.WindowStaysOnTopHint)
                            & ~(Qt.WindowContextHelpButtonHint))
        self.setWindowTitle(self.tr("Signal Parameters Setup"))
        self.setWindowIcon(QIcon(":/signal.png"))

        self.setFixedSize(600, 300)

        self.Signal = None

        if Signal is not None:

            self.widget.amplitudeLineEdit.setText(str(Signal.Amplitude))
            self.widget.frequencyLineEdit.setText(str(Signal.Frequency * 1e-6))

            if Signal.Name == "RaisedCosine":
                self.widget.typeComboBox.setCurrentIndex(
                    c.SignalType.RaisedCosinePulse)
            elif Signal.Name == "GaussianSine":
                self.widget.typeComboBox.setCurrentIndex(
                    c.SignalType.GaussianSinePulse)
                self.widget.cyclesLabel.setVisible(True)
                self.widget.cyclesDoubleSpinBox.setVisible(True)
                self.widget.cyclesDoubleSpinBox.setValue(Signal.N_Cycles)

            t = self.generate(Signal.Frequency)
            sig = Signal.generate(t)
            self.mpl.ax.plot(sig)
            self.mpl.ax.axis("off")
            self.mpl.ax.hold(False)
            self.mpl.draw()

    def generate(self, Frequency):
        FreqMin = Frequency / 20.0
        return np.linspace(0.0, (1.0 / FreqMin),
                           int((5e8 / (Frequency * 1e6)) + 250))

    def preview(self):

        type = self.widget.typeComboBox.currentIndex()

        if self.getValues(type):
            self.mpl.ax.plot(np.zeros((10, 1)))
            self.mpl.ax.axis("off")
            self.mpl.ax.hold(False)
            self.mpl.draw()

            return

        t = self.generate(self.Signal.Frequency)
        sig = self.Signal.generate(t)

        self.mpl.ax.plot(sig)
        self.mpl.ax.axis("off")
        self.mpl.draw()

    def accept(self):

        type = self.widget.typeComboBox.currentIndex()

        try:
            if self.getValues(type):
                return
        except:
            return

        QDialog.accept(self)

    def handleTypeSignal(self, index):

        if index == c.SignalType.GaussianSinePulse:
            self.widget.cyclesLabel.setVisible(True)
            self.widget.cyclesDoubleSpinBox.setVisible(True)
        else:
            self.widget.cyclesLabel.setVisible(False)
            self.widget.cyclesDoubleSpinBox.setVisible(False)

    def getValues(self, type):

        try:
            amplitude = float(self.widget.amplitudeLineEdit.text())
        except:
            msgBox = WarningParms("Give correctly the Amplitude")
            if msgBox.exec_():
                return True

        try:
            frequency = float(self.widget.frequencyLineEdit.text()) * 1e6
            if frequency > 1e9:
                msgBox = WarningParms("Give correctly the Frequency (MHz)")
                if msgBox.exec_():
                    return True

        except:
            msgBox = WarningParms("Give correctly the Frequency (MHz)")
            if msgBox.exec_():
                return True

        if type == c.SignalType.RaisedCosinePulse:
            self.Signal = Signals("RaisedCosine", amplitude, frequency)
            cycles = 1

        elif type == c.SignalType.GaussianSinePulse:
            try:
                cycles = float(self.widget.cyclesDoubleSpinBox.value())
                if cycles == 0:
                    msgBox = WarningParms("Give correctly the # Cycles")
                    if msgBox.exec_():
                        return True

            except:
                msgBox = WarningParms("Give correctly the # Cycles")
                self.widget.cyclesDoubleSpinBox.setValue(0)
                if msgBox.exec_():
                    return True

            try:
                self.Signal = Signals("GaussianSine", amplitude, frequency,
                                      cycles)
                t = self.generate(self.Signal.Frequency)
                sig = self.Signal.generate(t)

            except:
                msgBox = WarningParms("Give lower # Cycles")
                self.widget.cyclesDoubleSpinBox.setValue(0)
                if msgBox.exec_():
                    return True
class LoadScenarioFromImage(QDialog):
    def __init__(self, filename=None, parent=None):
        super(LoadScenarioFromImage, self).__init__(parent)
        self.init()
        self.connectionSetup()

        self.filename = filename
        self.imageToDraw = None
        self.Pixel = None

    def init(self):

        self.mainwindow = LoadImageWindowMain()
        self.mainwindow.setEnabledToolbar(False)

        buttonBox = QDialogButtonBox(QDialogButtonBox.Ok
                                     | QDialogButtonBox.Cancel)
        self.connect(buttonBox, SIGNAL("accepted()"), self, SLOT("accept()"))
        self.connect(buttonBox, SIGNAL("rejected()"), self, SLOT("reject()"))

        layout = QVBoxLayout()
        layout.addWidget(self.mainwindow)
        layout.addWidget(buttonBox)
        self.setLayout(layout)

        w = self.mainwindow.imageFrame.width()
        h = self.mainwindow.imageFrame.height()

        self.mpl = MplCanvas(width=int(w / 100.0),
                             height=int(h / 100.0),
                             dpi=100)
        self.mpl.ax.axis("off")

        imageLayout = QVBoxLayout()
        imageLayout.addWidget(self.mpl)

        self.mainwindow.imageFrame.setLayout(imageLayout)
        self.mainwindow.infoLabel.clear()
        self.mainwindow.statusLabel.clear()
        self.mainwindow.moreInfoLabel.clear()

    def connectionSetup(self):
        self.mainwindow.actionOpen_Image.triggered.connect(self.openImage)
        self.mainwindow.actionShow_Original_Image.triggered.connect(
            self.showOriginalImage)
        self.mainwindow.actionRotate_90_Clockwise.triggered.connect(
            self.rotate90ClockWise)
        self.mainwindow.actionRotate_90_Counter_Clockwise.triggered.connect(
            self.rotate90CounterClockWise)
        self.mainwindow.actionFlip_Horizontal.triggered.connect(
            self.flipHorizontal)
        self.mainwindow.actionFlip_Vertical.triggered.connect(
            self.flipVertical)
        self.mainwindow.colormapComboBox.currentIndexChanged.connect(
            self.changeColormap)
        self.mainwindow.getLabeledImagePushButton.pressed.connect(
            self.getLabeledImage)

    def openImage(self):

        self.mainwindow.statusLabel.clear()
        self.mainwindow.moreInfoLabel.clear()

        self.labels = None
        self.image_labeled = None

        dir = os.path.dirname(
            self.filename) if self.filename is not None else "."
        fname, filters = QFileDialog.getOpenFileName(
            None, "Load Image", dir,
            self.tr("Image Files (*.png *.jpg *.bmp *.jpeg)"))

        if fname:

            self.imageOriginal = imread(fname)
            try:
                M, N, R = np.shape(self.imageOriginal)
                self.mainwindow.colormapComboBox.setEnabled(False)
            except:
                M, N = np.shape(self.imageOriginal)
                self.mainwindow.colormapComboBox.setEnabled(True)

            self.imageToDraw = self.imageOriginal
            self.mainwindow.setEnabledToolbar(True)
            self.updateImage()

    def changeColormap(self, index):

        if self.imageToDraw is not None:

            if index == 0:
                Colormap = cm.jet
            elif index == 1:
                Colormap = cm.spectral
            elif index == 2:
                Colormap = cm.Set1

            self.mpl.fig.clear()
            self.mpl.ax = self.mpl.fig.add_subplot(111)
            cax = self.mpl.ax.imshow(self.imageToDraw,
                                     cmap=Colormap,
                                     vmin=0,
                                     vmax=255)
            self.showColorbar(cax)
            self.mpl.ax.axis("off")
            self.mpl.draw()
            QCoreApplication.processEvents()

            return cax

    def showOriginalImage(self):

        self.mainwindow.statusLabel.clear()
        self.mainwindow.moreInfoLabel.clear()

        self.imageToDraw = self.imageOriginal
        self.updateImage()

    def rotate90ClockWise(self):

        self.imageToDraw = imrotate(self.imageToDraw, -90, interp='nearest')
        self.updateImage()

    def rotate90CounterClockWise(self):

        self.imageToDraw = imrotate(self.imageToDraw, 90, interp='nearest')
        self.updateImage()

    def flipHorizontal(self):

        self.imageToDraw = np.fliplr(self.imageToDraw)
        self.updateImage()

    def flipVertical(self):

        self.imageToDraw = np.flipud(self.imageToDraw)
        self.updateImage()

    def updateImage(self):

        self.mainwindow.statusLabel.clear()
        self.mainwindow.moreInfoLabel.clear()

        self.mpl.ax.imshow(self.imageToDraw)
        self.mpl.ax.axis("off")
        self.mpl.draw()
        QCoreApplication.processEvents()

        try:
            M, N, R = np.shape(self.imageToDraw)
            self.mainwindow.colormapComboBox.setEnabled(False)
        except:
            M, N = np.shape(self.imageToDraw)

        self.mainwindow.infoLabel.setText("Dimensions: %d x %d" % (M, N))

    def getLabeledImage(self):

        self.labels = None

        self.mainwindow.colormapComboBox.setEnabled(True)
        self.mainwindow.statusLabel.setText(self.tr("Procesing......"))
        QCoreApplication.processEvents()

        try:
            M, N, R = np.shape(self.imageToDraw)
            self.image_gray = rgb2gray(self.imageToDraw)
        except:
            M, N = np.shape(self.imageToDraw)

        n_colors = self.mainwindow.numberLabelsSpinBox.value()
        self.image = median_filter(self.image_gray, 2)
        image_array = np.reshape(self.image, (M * N, 1))
        centroids, self.labels = kmeans2(image_array, n_colors)
        check = np.size(np.unique(self.labels))

        if check != n_colors:
            self.image_labeled = np.zeros((M, N))
            self.mpl.fig.clear()
            self.mpl.ax = self.mpl.fig.add_subplot(111)
            self.mpl.ax.imshow(np.zeros((M, N)), cmap=cm.jet, vmin=0, vmax=255)
            self.mpl.ax.axis("off")
            self.mpl.draw()

            QCoreApplication.processEvents()
            msgBox = WarningParms("Try again")
            if msgBox.exec_():
                self.mainwindow.statusLabel.clear()
                QCoreApplication.processEvents()
                return

        self.labels *= 40

        self.image_labeled = np.reshape(self.labels, (M, N))
        self.imageToDraw = self.image_labeled

        index = self.mainwindow.colormapComboBox.currentIndex()
        cax = self.changeColormap(index)

        self.mpl.draw()
        self.mainwindow.moreInfoLabel.setText(
            self.tr("<b>Apply several times to change labels"))
        self.mainwindow.statusLabel.setText(self.tr("Done"))
        QCoreApplication.processEvents()

    def showColorbar(self, cax):

        if self.labels is not None:

            ticks_at = np.unique(self.labels)
            cbar = self.mpl.fig.colorbar(cax,
                                         ticks=ticks_at,
                                         orientation='horizontal')
            cbar.set_label("Labels")

    def accept(self):

        if self.image_labeled is None:
            msgBox = WarningParms("Get the labeled Image")
            if msgBox.exec_():
                return

        try:
            self.Pixel = float(self.mainwindow.PixelLineEdit.text())

        except:
            msgBox = WarningParms("Please give correctly the Pixel/mm")
            if msgBox.exec_():
                return

        QDialog.accept(self)