Beispiel #1
0
    def __init__(self):
        QDialog.__init__(self)
        self.ui = Ui_Dialog()
        self.ui.setupUi(self)
        
        self.ui.pushButton_open.clicked.connect(self.open_file)
        self.ui.pushButton_quit.clicked.connect(self.close)
        self.ui.pushButton_run.clicked.connect(self.run_image_transformation)
        self.ui.pushButton_denoise.clicked.connect(self.run_image_denoise)
        self.ui.pushButton_smooth.clicked.connect(self.run_image_smooth)
        self.ui.pushButton_learn.clicked.connect(self.run_image_learn)
        self.ui.pushButton_check.clicked.connect(self.run_image_check)
        self.ui.pushButton_check.clicked.connect(self.display)
        
        self.ui.pushButton_settings.clicked.connect(self.settings)
        self.ui.pushButton_save.clicked.connect(self.save_file)
        self.ui.pushButton_find.clicked.connect(self.run_image_find)
         
        self.ui.tabWidget.currentChanged.connect(self.display)
        
        self.piximage = None
        
        self.tree = None
        self.background = None
        
        self.ss_labels = None
        self.s_labels = None
        
        
#         t = open('C:/Users/Sfgv/workspace/HMM/Save/tree.txt', 'r')
#         self.model_tree = Model.read(t)
#          
#         o = open('C:/Users/Sfgv/workspace/HMM/Save/other.txt', 'r')
#         self.model_other = Model.read(o)
#          
#         g = open('C:/Users/Sfgv/workspace/HMM/Save/trawa.txt', 'r')
#         self.model_trawa = Model.read(g)
#         
        class GraphicsScene(QGraphicsScene):
            def __init__(self, parent=None):
                super(GraphicsScene, self).__init__()
                self.parent = parent
                
            def mousePressEvent(self, event):
                super(GraphicsScene, self).mousePressEvent(event)
                x = int(event.pos().x())
                y = int(event.pos().y())
                color = get_hsv( self.parent.piximage.matrix_o[y,x] )
                label = self.parent.piximage.get_label_name(x,y)
                print(x,y,color,label,self.parent.piximage.matrix_o[y,x])
            
        self.scene_original = GraphicsScene(self)
        self.ui.graphicsView_original.setScene(self.scene_original)
        
        self.scene_transformation = GraphicsScene(self)
        self.ui.graphicsView_transformation.setScene(self.scene_transformation)
        
        self.scene_transformation_color = GraphicsScene(self)
        self.ui.graphicsView_transformation_color.setScene(self.scene_transformation_color)
Beispiel #2
0
class Main(QDialog):
                
    def __init__(self):
        QDialog.__init__(self)
        self.ui = Ui_Dialog()
        self.ui.setupUi(self)
        
        self.ui.pushButton_open.clicked.connect(self.open_file)
        self.ui.pushButton_quit.clicked.connect(self.close)
        self.ui.pushButton_run.clicked.connect(self.run_image_transformation)
        self.ui.pushButton_denoise.clicked.connect(self.run_image_denoise)
        self.ui.pushButton_smooth.clicked.connect(self.run_image_smooth)
        self.ui.pushButton_learn.clicked.connect(self.run_image_learn)
        self.ui.pushButton_check.clicked.connect(self.run_image_check)
        self.ui.pushButton_check.clicked.connect(self.display)
        
        self.ui.pushButton_settings.clicked.connect(self.settings)
        self.ui.pushButton_save.clicked.connect(self.save_file)
        self.ui.pushButton_find.clicked.connect(self.run_image_find)
         
        self.ui.tabWidget.currentChanged.connect(self.display)
        
        self.piximage = None
        
        self.tree = None
        self.background = None
        
        self.ss_labels = None
        self.s_labels = None
        
        
#         t = open('C:/Users/Sfgv/workspace/HMM/Save/tree.txt', 'r')
#         self.model_tree = Model.read(t)
#          
#         o = open('C:/Users/Sfgv/workspace/HMM/Save/other.txt', 'r')
#         self.model_other = Model.read(o)
#          
#         g = open('C:/Users/Sfgv/workspace/HMM/Save/trawa.txt', 'r')
#         self.model_trawa = Model.read(g)
#         
        class GraphicsScene(QGraphicsScene):
            def __init__(self, parent=None):
                super(GraphicsScene, self).__init__()
                self.parent = parent
                
            def mousePressEvent(self, event):
                super(GraphicsScene, self).mousePressEvent(event)
                x = int(event.pos().x())
                y = int(event.pos().y())
                color = get_hsv( self.parent.piximage.matrix_o[y,x] )
                label = self.parent.piximage.get_label_name(x,y)
                print(x,y,color,label,self.parent.piximage.matrix_o[y,x])
            
        self.scene_original = GraphicsScene(self)
        self.ui.graphicsView_original.setScene(self.scene_original)
        
        self.scene_transformation = GraphicsScene(self)
        self.ui.graphicsView_transformation.setScene(self.scene_transformation)
        
        self.scene_transformation_color = GraphicsScene(self)
        self.ui.graphicsView_transformation_color.setScene(self.scene_transformation_color)
        
    def open_file(self):
        dlg = QFileDialog(self)
        dlg.setWindowTitle('Open image')
        dlg.setViewMode(QFileDialog.Detail)
        dlg.setNameFilters([self.tr('Image Files (*.bmp)'), self.tr('All Files (*)')])
        dlg.setDefaultSuffix('.bmp')
        functions.set_style_sheet(dlg, STYLE_SHEET)
                
        if dlg.exec_() :
            self.piximage = piximage2.PixImage(dlg.selectedFiles()[0])
            self.display()

    def save_file(self):
        dlg = QFileDialog(self)
        dlg.setWindowTitle('Open image')
        dlg.setViewMode(QFileDialog.Detail)
        dlg.setFileMode(QFileDialog.AnyFile)
        dlg.setAcceptMode(QFileDialog.AcceptSave)
        dlg.setNameFilters([self.tr('Image Files (*.bmp)'), self.tr('All Files (*)')])
        dlg.setDefaultSuffix('.bmp')
        functions.set_style_sheet(dlg, STYLE_SHEET)
                
        if dlg.exec_():
            path = dlg.selectedFiles()[0]
            i = self.ui.tabWidget.currentIndex()
        
            if self.piximage is not None: 
                if (i == 0):
                    self.piximage.image_o.save(path)
                elif (i == 1):
                    self.piximage.image_t.save(path)
                elif (i == 2):
                    self.piximage.image_t_color.save(path)
    
    def display(self):
        i = self.ui.tabWidget.currentIndex()
        
        if self.piximage is not None: 
            if (i == 0):
                self.display_image_original()
            elif (i == 1):
                self.display_image_transformation()
            elif (i == 2):
                self.piximage.add_edges_to_image()
                self.display_image_transformation_color()
        QtWidgets.QApplication.processEvents()
            
    def display_image_original(self):
        self.scene_original.clear()
        if self.piximage.image_o is not None:
            w, h = self.piximage.image_o.size
            self.imgQ_o = ImageQt.ImageQt(self.piximage.image_o)  # we need to hold reference to imgQ, or it will crash
            pixMap = QtGui.QPixmap.fromImage(self.imgQ_o)
            self.scene_original.addPixmap(pixMap)
            self.ui.graphicsView_original.fitInView(QtCore.QRectF(0, 0, w, h), QtCore.Qt.KeepAspectRatio)
            self.scene_original.update()
        
    def display_image_transformation(self):
        self.scene_transformation.clear()
        if self.piximage.image_t is not None:
            w, h = self.piximage.image_t.size
            self.imgQ_t = ImageQt.ImageQt(self.piximage.image_t)  # we need to hold reference to imgQ, or it will crash
            pixMap = QtGui.QPixmap.fromImage(self.imgQ_t)
            self.scene_transformation.addPixmap(pixMap)
            self.ui.graphicsView_transformation.fitInView(QtCore.QRectF(0, 0, w, h), QtCore.Qt.KeepAspectRatio)
            self.scene_transformation.update()

    def display_image_transformation_color(self):
        self.scene_transformation_color.clear()
        if self.piximage.image_t_color is not None:
            w, h = self.piximage.image_t_color.size
            self.imgQ_t = ImageQt.ImageQt(self.piximage.image_t_color)  # we need to hold reference to imgQ, or it will crash
            pixMap = QtGui.QPixmap.fromImage(self.imgQ_t)
            self.scene_transformation_color.addPixmap(pixMap)
            self.ui.graphicsView_transformation_color.fitInView(QtCore.QRectF(0, 0, w, h), QtCore.Qt.KeepAspectRatio)
            self.scene_transformation_color.update()
        
    def settings(self):
        self.setEnabled(False)
        self.form = SettingsForm()
        self.form.set_parent_window(self)
        self.form.show()
    
    def run_image_transformation(self):
        self.piximage.diff_image()
#         self.piximage.quantize_image()
       
    def run_image_denoise(self):
        self.tree = P2DHMMFactory.make(P2DHMM.P2DHMM_TYPE_TREE)
        self.tree.manual_train(self.piximage)
        
#         self.background = P2DHMMFactory.make(P2DHMM.P2DHMM_TYPE_BACKGROUND)
#         self.background.train(self.piximage.image_ycrcb)
#         
#         for ss_state in self.background.model.states:
#             if ss_state.name == self.background.superstate_names[0]:
#                 for s_state in ss_state.distribution.model.states:
#                     if s_state.name == ss_state.distribution.state_names[0]:
#                         s_state.distribution = self.tree.backgorund_state.distribution
                     
       
    def run_image_smooth(self):
        ss_labels, s_labels = find_object(self.piximage.image_ycrcb, self.piximage.image_o, self.tree, self.background)
        
        self.ss_labels = ss_labels
        self.s_labels = s_labels
        
    def run_image_learn(self):
        is_manual_train  = VARIABLE['hand_border']
        
        self.tree = P2DHMMFactory.make(P2DHMM.P2DHMM_TYPE_TREE)
        self.background = P2DHMMFactory.make(P2DHMM.P2DHMM_TYPE_BACKGROUND)
        
        for root, dirs, files in os.walk(VARIABLE['directory']):
            for file in files:
                if file.endswith('.bmp'):
                    print(root+"/"+file)
                    self.piximage = piximage2.PixImage(root+"/"+file)
                    self.display()
                    
                    if is_manual_train:
                        self.tree.manual_train(self.piximage)
                    else:
                        self.tree.auto_train(self.piximage.image_ycrcb)
                    self.tree.summarize()
          
        self.tree.from_summaries()
        
        self.background.train(self.piximage.image_ycrcb)
        for ss_state in self.background.model.states:
            if ss_state.name == self.background.superstate_names[0]:
                for s_state in ss_state.distribution.model.states:
                    if s_state.name == ss_state.distribution.state_names[0]:
                        s_state.distribution = self.tree.backgorund_state.distribution
                        
    def run_image_check(self):
#         t = open('C:/Users/Sfgv/workspace/HMM/Save/tree.txt', 'w')
#         self.model_tree.write(t)
#          
#         b = open('C:/Users/Sfgv/workspace/HMM/Save/background.txt', 'w')
#         self.model_other.write(b)
         
        prob, ss_labels, s_labels = self.tree.check_image(self.piximage.image_ycrcb)
        print('tree', prob, ss_labels )
        probb, ss_labelsb, s_labelsb = self.background.check_image(self.piximage.image_ycrcb)
        print('back', probb )
        
        print( self.tree)
        print( self.background)
        
        self.ss_labels = ss_labels
        self.s_labels = s_labels
        
    def run_image_find(self):
        
        if self.ss_labels is None or self.s_labels is None:
            return
        
        featured_labels = self.tree.get_featured( VARIABLE['edge_mode'] )
#         print( featured_labels )
        
        object_color = { }
        color = 1 
        
        label_img = numpy.zeros( (len( self.ss_labels), len( self.s_labels[0])) )
        for object_name, object_featured in featured_labels.items():
            object_color[ object_name ] = color
            
            
            for i, ss_label in enumerate( self.ss_labels ):
                print(i, ss_label, object_featured.keys())
                if ss_label in object_featured.keys():
                    for j, label in enumerate( self.s_labels[i] ):
                        if label in object_featured[ ss_label ]:
                            label_img[i,j] =  color 
                        
                        
            color += 1
            
        for row in label_img:
            print(row)
            
        print( self.ss_labels )
        print( object_color )
        
        self.piximage.color_objects(label_img)