Exemplo n.º 1
0
 def menuActionMatch(self):
     # get parameters
     popup = DialogMatch(self.window)
     popup.exec_()
     if not popup.path:
         return
     # read file at popup.path
     target = cv2.imread(popup.path, 0)
     if target is None:
         print("Could not open file or file is not an image.")
         return
     # copy current state
     old = self.history[self.historyIndex]
     matrix = old['matrix'].copy()
     histogram = deepcopy(old['histogram']['plots'][0])
     # apply histogram matching
     histogram1 = Histogram.hist(target)
     matrix = Transform.match(matrix, histogram, histogram1)
     # compute histogram
     histogram = {
         'plots': [Histogram.hist(matrix), histogram1],
         'legend': ["New", "Target"]
     }
     # add new state
     self.appendHistory({
         'matrix': matrix,
         'histogram': histogram,
         'saved': False
     })
     self.updateImage(matrix)
     hist = [old['histogram']['plots'][0]] + histogram['plots']
     legend = ["Old"] + histogram['legend']
     self.windowHist.setHistogram(hist, legend)
Exemplo n.º 2
0
 def menuActionShape(self):
     # get parameters
     popup = DialogShape(self.window)
     popup.exec_()
     if popup.function is None \
         or popup.center is None or popup.width is None \
         or popup.times is None or popup.distance is None \
         or popup.direction is None:
         return
     # copy current state
     old = self.history[self.historyIndex]
     matrix = old['matrix'].copy()
     histogram = deepcopy(old['histogram']['plots'][0])
     # apply histogram shaping
     matrix, target = Transform.shape(matrix, histogram, popup.function, \
         popup.center, popup.width, popup.times, popup.distance, popup.direction)
     # compute histogram
     h = Histogram.hist(matrix)
     target = Math.scale(target, max(h) / float(max(target)))
     histogram = {
         'plots': [h, target],
         'legend': ["New", "Target"]
     }
     # add new state
     self.appendHistory({
         'matrix': matrix,
         'histogram': histogram,
         'saved': False
     })
     self.updateImage(matrix)
     hist = [old['histogram']['plots'][0]] + histogram['plots']
     legend = ["Old"] + histogram['legend']
     self.windowHist.setHistogram(hist, legend)
Exemplo n.º 3
0
 def menuActionPower(self):
     # get parameters
     popup = DialogPower(self.window)
     popup.exec_()
     if popup.c is None or popup.p is None:
         return
     # copy current state
     old = self.history[self.historyIndex]
     matrix = old['matrix'].copy()
     histogram = deepcopy(old['histogram']['plots'][0])
     # apply power function
     matrix = Transform.power(matrix, popup.c, popup.p)
     # compute histogram
     histogram = {
         'plots': [Histogram.hist(matrix)],
         'legend': [str(popup.c) + "*i^" + str(popup.p)]
     }
     # add new state
     self.appendHistory({
         'matrix': matrix,
         'histogram': histogram,
         'saved': False
     })
     self.updateImage(matrix)
     hist = [old['histogram']['plots'][0]] + histogram['plots']
     legend = ["Old"] + histogram['legend']
     self.windowHist.setHistogram(hist, legend)
Exemplo n.º 4
0
 def menuActionEqualize(self):
     # copy current state
     old = self.history[self.historyIndex]
     matrix = old['matrix'].copy()
     histogram = deepcopy(old['histogram']['plots'][0])
     # apply histogram equalization
     matrix = Transform.equalize(matrix, histogram)
     # compute histogram
     histogram = {
         'plots': [Histogram.hist(matrix)],
         'legend': ["Equalized"]
     }
     # add new state
     self.appendHistory({
         'matrix': matrix,
         'histogram': histogram,
         'saved': False
     })
     self.updateImage(matrix)
     hist = [old['histogram']['plots'][0]] + histogram['plots']
     legend = ["Old"] + histogram['legend']
     self.windowHist.setHistogram(hist, legend)
Exemplo n.º 5
0
    def openImage(self, path):
        if not path:
            return
        
        # close current image
        e = QtCore.QEvent(None)
        self.closeEvent(e)
        if not e.isAccepted():
            return
        
        # read image
        matrix = cv2.imread(path, 0)
        if matrix is None:
            print("Could not open file or file is not an image.")
            return
        
        self.history = [{'matrix': matrix, 'histogram': None, 'saved': True}]
        self.historyIndex = 0
        
        layout = self.centralwidget.layout()
        # clean layout
        for i in reversed(range(layout.count())): 
            item = layout.itemAt(i)
            widget = item.widget()
            if widget is None:
                layout.removeItem(item)
            else:
                widget.deleteLater()
            del item
        
        # show image
        self.scrollArea = QtGui.QScrollArea()
        self.imageLabel = ImageLabel(scrollArea=self.scrollArea)
        self.imageLabel.setBackgroundRole(QtGui.QPalette.Base)
        self.imageLabel.setSizePolicy(QtGui.QSizePolicy.Ignored, QtGui.QSizePolicy.Ignored)
        self.imageLabel.setScaledContents(True)
        
        self.scrollArea.setAlignment(QtCore.Qt.AlignCenter)
        self.scrollArea.setWidget(self.imageLabel)
        layout.addWidget(self.scrollArea)
        layout.setContentsMargins(0, 0, 0, 0)

        self.updateImage(matrix)
        self.zoom = 1
        
        # adjust window's size
        height, width = matrix.shape
        adjustWindowSize(self.window, width + 2, height + 2)
        
        # center window
        geom = desktopGeometry()
        x = geom.x() + 0.5 * (geom.width() - self.window.frameSize().width())
        y = geom.y() + 0.5 * (geom.height() - self.window.frameSize().height())
        self.window.move(x, y)
        
        # create histogram window
        if self.windowHist is None or self.windowHist.isClosed():
            if self.windowHist is not None:
                del self.windowHist
            self.windowHist = WindowHistogram(self.window)
            self.windowHist.show()
        
        hist = [Histogram.hist(matrix)]
        legend = ["Source Image"]
        self.history[0]['histogram'] = {'plots': hist, 'legend': legend}
        
        self.windowHist.setHistogram(hist, legend)
        self.windowHist.adjustWindowSize()
        self.windowHist.adjustWindowPosition()
        
        self.window.activateWindow()
        self.window.setFocus()
        
        # enable _Save, Edit, View+, Transform+ and _Histogram
        self.actionSave.setEnabled(True)
        self.menuEdit.setEnabled(True)
        for a in self.menuView.actions():
            a.setEnabled(True)
        self.menuView.setEnabled(True)
        for a in self.menuTransform.actions():
            a.setEnabled(True)
        self.menuTransform.setEnabled(True)
        self.window.actionHistogram.setEnabled(True)