OpenCV结合PYQT5实现简单的图像处理GUI

提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档

OpenCV结合PYQT5实现简单的图像处理GUI

  • 前言
  • 一、图像处理模型
  • 二、GUI界面设计
    • 1.QT Designer
    • 2.中英切换
    • 3.灾害统计
  • 三、效果图


前言

提示:本博客涉及图像处理的GUI
主要应用场景:遥感图像灾害检测


提示:以下是本篇文章正文内容,下面案例可供参考

一、图像处理模型

示例:模型使用的是Mask R-CNN,详细内容不展开细讲。由于这是一个实例分割模型,根据不同的Demo可以生成目标检测、语义分割和实例分割的输出。
代码详解可以参考以下博客:https://blog.csdn.net/ghw15221836342/article/details/80084984
输出如下(示例):
OpenCV结合PYQT5实现简单的图像处理GUI_第1张图片
核心代码如下(示例):

def Object_Detectron_Visual(image_path):
    image = skimage.io.imread(image_path)
    dataset_test = xBD.xBDDataset()
    dataset_test.load_xBD('F:/Python_Project/Data/xBD/hurricane-florence/', "train")
    dataset_test.prepare()
    
    mrcnn = model.run_graph([image], [
        ("proposals", model.keras_model.get_layer("ROI").output),
        ("probs", model.keras_model.get_layer("mrcnn_class").output),
        ("deltas", model.keras_model.get_layer("mrcnn_bbox").output),
        ("masks", model.keras_model.get_layer("mrcnn_mask").output),
        ("detections", model.keras_model.get_layer("mrcnn_detection").output),
    ])
    
    det_class_ids = mrcnn['detections'][0, :, 4].astype(np.int32)
    det_count = np.where(det_class_ids == 0)[0][0]
    det_class_ids = det_class_ids[:det_count]
    detections = mrcnn['detections'][0, :det_count]
     
    captions = ["{} {:.3f}".format(dataset_test.class_names[int(c)], s) if c > 0 else ""
                for c, s in zip(detections[:, 4], detections[:, 5])]
    visualize.draw_boxes(image, refined_boxes=utils.denorm_boxes(detections[:, :4], image.shape[:2]),visibilities=[2] * len(detections),captions=captions, title="Detections",ax=get_ax())
    return det_class_ids
    
def Instance_Segmentation_Visual(image_path):
    # config.display()
    image = skimage.io.imread(image_path)
    results = model.detect([image], verbose=1)
    r = results[0]
    visualize.display_instances(image, r['rois'], r['masks'], r['class_ids'], class_names, r['scores'])
    return r['class_ids'] 

二、GUI界面设计

1.QT Designer

先在QT设计师中把所有的原件布置完毕,然后通过程序进行导入操作。
关于界面美化部分,使用的图标可以参考以下链接:https://www.cnblogs.com/pyw0818/p/8052047.html
http://www.fontawesome.com.cn/faicons/


2.中英切换

中英切换主要用的是QT linguist软件,先在该软件下把所有的文本都翻译了,翻译完成后通过软件输出一个.qm文件,然后利用以下代码进行导入
代码如下(示例):

app = QApplication(sys.argv)
trans = QTranslator()
trans.load("CN1")  # 没有后缀.qm
app.installTranslator(trans)

翻译后的软件界面如下(部分界面需马赛克):
OpenCV结合PYQT5实现简单的图像处理GUI_第2张图片


3.灾害统计

为了更好地理解图像,提供了灾害统计界面,快速对图像中的受灾建筑进行统计并进行可视化操作。

class Statistical_Figure(QWidget):   #灾害统计图
    def __init__(self):
        super(Statistical_Figure, self).__init__()
        self.resize(700,500)
        self.setWindowTitle('统计图')

    def paintEvent(self, QPaintEvent):
        global Global_Disaster_statistics
        X = ['灾害类型统计图']
        Y = [[Counter(Global_Disaster_statistics)[1], Counter(Global_Disaster_statistics)[2], Counter(Global_Disaster_statistics)[3], 
              Counter(Global_Disaster_statistics)[4],Counter(Global_Disaster_statistics)[5]], [0, 0, 0, 0,0]]
        painter = QPainter()
        painter.begin(self)
        painter.setPen(Qt.NoPen)
        painter.setRenderHint(QPainter.Antialiasing)
        pen = QPen(Qt.black, 2, Qt.SolidLine)
        painter.setPen(pen)
        pointx = 32
        pointy = self.height() - 50

        width = self.width() - 100 - pointx
        height = self.height()
        # X轴
        painter.drawLine(pointx, pointy, width + pointx, pointy)
        # Y轴
        painter.drawLine(pointx, pointy - height, pointx, pointy)

        X_Spacing = width / len(X)
        X_little_Spacing = width / len(X) / (len(Y[1]) + 1)

        Y_Spacing = height / len(Y)

        pen = QPen(Qt.red, 5, Qt.SolidLine)
        painter.setPen(pen)
        X_start = X_little_Spacing * 3 + 35
        X_pox = []
        for i in range(len(X)):
            painter.drawPoint(X_start - X_little_Spacing * 2, pointy)
            painter.drawPoint(X_start - X_little_Spacing, pointy)
            painter.drawPoint(X_start, pointy)
            painter.drawPoint(X_start + X_little_Spacing * 2, pointy)
            painter.drawPoint(X_start + X_little_Spacing, pointy)
            painter.drawText(X_start - 15, pointy + 20, str(X[i]))
            X_pox.append(X_start)

            X_start = X_start + X_Spacing
        # print(X_pox)
        Y_max = 0
        for i in range(len(Y)):
            if Y_max < max(Y[i]):
                Y_max = max(Y[i])
        num_Spacing = int(Y_max / (len(Y) - 1)) + 1
        num_start = 0
        Y_start_pox = 0
        end_Pox = 0
        for i in range(len(Y)):
            painter.drawPoint(35, pointy - Y_start_pox)
            end_Pox = pointy - Y_start_pox
            painter.drawText(pointx - 30, pointy - Y_start_pox + 5, str(num_start))
            Y_start_pox = Y_start_pox + Y_Spacing
            num_start = num_start + num_Spacing
        every_pox = (pointy - end_Pox) / (num_start - num_Spacing)

        pen = QPen(Qt.black, 1, Qt.SolidLine)
        painter.setPen(pen)
        # print(X_pox)
        for i in range(len(X)):
            brush = QBrush(Qt.Dense1Pattern)
            painter.setBrush(brush)
            painter.drawText(X_pox[i] +33 - X_little_Spacing * 2, pointy - Y[i][0] * every_pox - 5, str(Y[i][0]))
            painter.drawRect(X_pox[i] - X_little_Spacing * 2, pointy - Y[i][0] * every_pox, X_little_Spacing,
                             Y[i][0] * every_pox)
            brush = QBrush(Qt.DiagCrossPattern)
            painter.setBrush(brush)
            painter.drawText(X_pox[i] +33 - X_little_Spacing, pointy - Y[i][1] * every_pox - 5, str(Y[i][1]))
            painter.drawRect(X_pox[i] - X_little_Spacing, pointy - Y[i][1] * every_pox, X_little_Spacing,
                             Y[i][1] * every_pox)
            brush = QBrush(Qt.Dense2Pattern)
            painter.setBrush(brush)
            painter.drawText(X_pox[i] +33, pointy - Y[i][2] * every_pox - 5, str(Y[i][2]))
            painter.drawRect(X_pox[i], pointy - Y[i][2] * every_pox, X_little_Spacing, Y[i][2] * every_pox)
            brush = QBrush(Qt.Dense3Pattern)
            painter.setBrush(brush)
            painter.drawText(X_pox[i] +33 + X_little_Spacing, pointy - Y[i][3] * every_pox - 5, str(Y[i][3]))
            painter.drawRect(X_pox[i] + X_little_Spacing, pointy - Y[i][3] * every_pox, X_little_Spacing,
                             Y[i][3] * every_pox)
            painter.setBrush(brush)
            painter.drawText(X_pox[i] +33 + 2*X_little_Spacing, pointy - Y[i][4] * every_pox - 5, str(Y[i][4]))
            painter.drawRect(X_pox[i] + 2*X_little_Spacing, pointy - Y[i][4] * every_pox, X_little_Spacing,
                             Y[i][4] * every_pox)
            brush = QBrush(Qt.Dense5Pattern)
        brush = QBrush(Qt.Dense1Pattern)
        painter.setBrush(brush)
        painter.drawRect(self.width() - 180, 10, 50, 20)
        painter.drawText(self.width() - 120, 25, "no-damage")

        brush = QBrush(Qt.DiagCrossPattern)
        painter.setBrush(brush)
        painter.drawRect(self.width() - 180, 35, 50, 20)
        painter.drawText(self.width() - 120, 50, "minor-damage")

        brush = QBrush(Qt.Dense2Pattern)
        painter.setBrush(brush)
        painter.drawRect(self.width() - 180, 60, 50, 20)
        painter.drawText(self.width() - 120, 75, "major-damage")

        brush = QBrush(Qt.Dense3Pattern)
        painter.setBrush(brush)
        painter.drawRect(self.width() - 180, 85, 50, 20)
        painter.drawText(self.width() - 120, 100, "destoryed")
        
        brush = QBrush(Qt.Dense5Pattern)
        painter.setBrush(brush)
        painter.drawRect(self.width() - 180, 110, 50, 20)
        painter.drawText(self.width() - 120, 125, "un-classified")      

三、效果图

软件界面如下(部分界面需马赛克):
OpenCV结合PYQT5实现简单的图像处理GUI_第3张图片

受灾建筑统计图如下(部分界面需马赛克):OpenCV结合PYQT5实现简单的图像处理GUI_第4张图片

提示:本软件对1024*1024的图像处理速度为7s左右,设备性能比较拉,不过大致满足实际需求,如果对实时性要求较高的,氪金就完了。

你可能感兴趣的:(GUI,深度学习,tensorflow,caffe)