(图像处理)课上小作业03

1、Qt Creator创建UI

就是对图像处理程序的一点简单包装,两个label控件用来显示图像,通过输入的全局阈值将不同二值化的图像呈现在右侧。
在这里插入图片描述

2、将UI转换为py

首先需要安装pyqt5,然后通过pyuic5命令转化。

pip install pyqt5
pyuic5 -o dialog.py dialog.ui

转换后的py文件

# -*- coding: utf-8 -*-

# Form implementation generated from reading ui file 'dialog.ui'
#
# Created by: PyQt5 UI code generator 5.15.4
#
# WARNING: Any manual changes made to this file will be lost when pyuic5 is
# run again.  Do not edit this file unless you know what you are doing.


from PyQt5 import QtCore, QtGui, QtWidgets


class Ui_Dialog(object):
    def setupUi(self, Dialog):
        Dialog.setObjectName("Dialog")
        Dialog.resize(915, 545)
        self.horizontalLayoutWidget = QtWidgets.QWidget(Dialog)
        self.horizontalLayoutWidget.setGeometry(QtCore.QRect(20, 20, 291, 411))
        self.horizontalLayoutWidget.setObjectName("horizontalLayoutWidget")
        self.horizontalLayout = QtWidgets.QHBoxLayout(self.horizontalLayoutWidget)
        self.horizontalLayout.setContentsMargins(0, 0, 0, 0)
        self.horizontalLayout.setObjectName("horizontalLayout")
        self.lb1 = QtWidgets.QLabel(self.horizontalLayoutWidget)
        self.lb1.setObjectName("lb1")
        self.horizontalLayout.addWidget(self.lb1)
        self.horizontalLayoutWidget_2 = QtWidgets.QWidget(Dialog)
        self.horizontalLayoutWidget_2.setGeometry(QtCore.QRect(330, 20, 321, 411))
        self.horizontalLayoutWidget_2.setObjectName("horizontalLayoutWidget_2")
        self.horizontalLayout_2 = QtWidgets.QHBoxLayout(self.horizontalLayoutWidget_2)
        self.horizontalLayout_2.setContentsMargins(0, 0, 0, 0)
        self.horizontalLayout_2.setObjectName("horizontalLayout_2")
        self.lb2 = QtWidgets.QLabel(self.horizontalLayoutWidget_2)
        self.lb2.setObjectName("lb2")
        self.horizontalLayout_2.addWidget(self.lb2)
        self.open_btn = QtWidgets.QPushButton(Dialog)
        self.open_btn.setGeometry(QtCore.QRect(20, 440, 93, 28))
        self.open_btn.setObjectName("open_btn")
        self.input_line = QtWidgets.QLineEdit(Dialog)
        self.input_line.setGeometry(QtCore.QRect(130, 440, 113, 21))
        self.input_line.setObjectName("input_line")
        self.process_bi_btn = QtWidgets.QPushButton(Dialog)
        self.process_bi_btn.setGeometry(QtCore.QRect(260, 440, 93, 28))
        self.process_bi_btn.setObjectName("process_bi_btn")

        self.retranslateUi(Dialog)
        QtCore.QMetaObject.connectSlotsByName(Dialog)

    def retranslateUi(self, Dialog):
        _translate = QtCore.QCoreApplication.translate
        Dialog.setWindowTitle(_translate("Dialog", "Dialog"))
        self.lb1.setText(_translate("Dialog", "TextLabel"))
        self.lb2.setText(_translate("Dialog", "TextLabel"))
        self.open_btn.setText(_translate("Dialog", "打开文件"))
        self.input_line.setText(_translate("Dialog", "请输入阈值..."))
        self.process_bi_btn.setText(_translate("Dialog", "二值化"))

在这个基础上,加上主函数就可以看到窗体了

def main():
    """
    主函数,用于运行程序
    :return: None
    """
    app = QtWidgets.QApplication(sys.argv)
    dialog = QtWidgets.QDialog()
    ui = Ui_Dialog()
    ui.setupUi(dialog)
    dialog.show()
    sys.exit(app.exec_())


if __name__ == '__main__':
    main()

效果

在这里插入图片描述

3、添加信号和槽函数

绑定信号和槽

# 绑定槽函数
self.open_btn.clicked.connect(self.open_buttonclicked)
self.process_bi_btn.clicked.connect(self.process_bi_btnclicked)

槽函数

    def open_buttonclicked(self):
        fileName, fileType = QFileDialog.getOpenFileName(self, "选取文件", os.getcwd(),"All Files(*);;Text Files(*.txt)")
        # print("fileName: ",fileName)
        # print("fileType: ",fileType)
        save_fpath=self.scaleImg(fileName)
        if save_fpath is not None:
            print("文件已存在:{}".format(save_fpath))
        self.ori_path=save_fpath
        pix = QPixmap(save_fpath)
        self.lb1.setPixmap(pix)

    def process_bi_btnclicked(self):
        img=cv2.imread(self.ori_path)

        # 获取输入的阈值
        thresh=self.input_line.text()
        ret1, th1 = cv2.threshold(img, int(thresh), 255, cv2.THRESH_BINARY)
        height, width, bytesPerComponent = th1.shape
        bytesPerLine = 3 * width
        # QImg = QImage(th1.data, width, height, bytesPerLine, QImage.Format_RGB888)
        QImg = QImage(th1.data, width, height, bytesPerLine,QImage.Format_Grayscale8)
        pixmap = QPixmap.fromImage(QImg)
        self.lb2.setPixmap(pixmap)

完整代码

# -*- coding: utf-8 -*-

# Form implementation generated from reading ui file 'dialog.ui'
#
# Created by: PyQt5 UI code generator 5.15.4
#
# WARNING: Any manual changes made to this file will be lost when pyuic5 is
# run again.  Do not edit this file unless you know what you are doing.


from PyQt5 import QtCore, QtWidgets
from PyQt5.QtWidgets import (QWidget, QApplication,QLabel,QFileDialog,QPushButton,QHBoxLayout,QLineEdit)
from PyQt5.QtGui import QPixmap,QImage
import sys,os
import cv2

class Ui_Dialog(QtWidgets.QMainWindow):
    def setupUi(self, Dialog):
        # 原图的路径
        self.ori_path=''

        Dialog.setObjectName("Dialog")
        Dialog.resize(1800, 1000)
        self.horizontalLayoutWidget = QtWidgets.QWidget(Dialog)
        self.horizontalLayoutWidget.setGeometry(QtCore.QRect(20, 20, 291, 411))
        self.horizontalLayoutWidget.setObjectName("horizontalLayoutWidget")
        self.horizontalLayout = QtWidgets.QHBoxLayout(self.horizontalLayoutWidget)
        self.horizontalLayout.setContentsMargins(0, 0, 0, 0)
        self.horizontalLayout.setObjectName("horizontalLayout")
        self.lb1 = QtWidgets.QLabel(self.horizontalLayoutWidget)
        self.lb1.setObjectName("lb1")
        self.horizontalLayout.addWidget(self.lb1)
        self.horizontalLayoutWidget_2 = QtWidgets.QWidget(Dialog)
        self.horizontalLayoutWidget_2.setGeometry(QtCore.QRect(330, 20, 321, 411))
        self.horizontalLayoutWidget_2.setObjectName("horizontalLayoutWidget_2")
        self.horizontalLayout_2 = QtWidgets.QHBoxLayout(self.horizontalLayoutWidget_2)
        self.horizontalLayout_2.setContentsMargins(0, 0, 0, 0)
        self.horizontalLayout_2.setObjectName("horizontalLayout_2")
        self.lb2 = QtWidgets.QLabel(self.horizontalLayoutWidget_2)
        self.lb2.setObjectName("lb2")
        self.horizontalLayout_2.addWidget(self.lb2)
        self.open_btn = QtWidgets.QPushButton(Dialog)
        self.open_btn.setGeometry(QtCore.QRect(20, 440, 93, 28))
        self.open_btn.setObjectName("open_btn")
        self.input_line = QtWidgets.QLineEdit(Dialog)
        self.input_line.setGeometry(QtCore.QRect(130, 440, 113, 21))
        self.input_line.setObjectName("input_line")
        self.process_bi_btn = QtWidgets.QPushButton(Dialog)
        self.process_bi_btn.setGeometry(QtCore.QRect(260, 440, 93, 28))
        self.process_bi_btn.setObjectName("process_bi_btn")

        self.retranslateUi(Dialog)
        QtCore.QMetaObject.connectSlotsByName(Dialog)

        # 绑定槽函数
        self.open_btn.clicked.connect(self.open_buttonclicked)
        self.process_bi_btn.clicked.connect(self.process_bi_btnclicked)

    def retranslateUi(self, Dialog):
        _translate = QtCore.QCoreApplication.translate
        Dialog.setWindowTitle(_translate("Dialog", "Dialog"))
        self.lb1.setText(_translate("Dialog", "TextLabel"))
        self.lb2.setText(_translate("Dialog", "TextLabel"))
        self.open_btn.setText(_translate("Dialog", "打开文件"))
        self.input_line.setText(_translate("Dialog", "请输入阈值..."))
        self.process_bi_btn.setText(_translate("Dialog", "二值化"))

    def open_buttonclicked(self):
        fileName, fileType = QFileDialog.getOpenFileName(self, "选取文件", os.getcwd(),"All Files(*);;Text Files(*.txt)")
        # print("fileName: ",fileName)
        # print("fileType: ",fileType)
        save_fpath=self.scaleImg(fileName)
        if save_fpath is not None:
            print("文件已存在:{}".format(save_fpath))
        self.ori_path=save_fpath
        pix = QPixmap(save_fpath)
        self.lb1.setPixmap(pix)

    def process_bi_btnclicked(self):
        img=cv2.imread(self.ori_path)

        # 获取输入的阈值
        thresh=self.input_line.text()
        ret1, th1 = cv2.threshold(img, int(thresh), 255, cv2.THRESH_BINARY)
        height, width, bytesPerComponent = th1.shape
        bytesPerLine = 3 * width
        # QImg = QImage(th1.data, width, height, bytesPerLine, QImage.Format_RGB888)
        QImg = QImage(th1.data, width, height, bytesPerLine,QImage.Format_Grayscale8)
        pixmap = QPixmap.fromImage(QImg)
        self.lb2.setPixmap(pixmap)

    def scaleImg(self,fpath):
        dir = os.path.dirname(fpath)
        fname = fpath.split('/')[-1]
        save_fpath=dir+'/cvt_'+fname
        if os.path.exists(save_fpath):
            return save_fpath
        img=cv2.imread(fpath)
        img_test2 = cv2.resize(img, (300, 200),interpolation=cv2.INTER_NEAREST)
        cv2.imwrite(save_fpath,img_test2)
        return save_fpath


def main():
    """
    主函数,用于运行程序
    :return: None
    """
    app = QtWidgets.QApplication(sys.argv)
    dialog = QtWidgets.QDialog()
    ui = Ui_Dialog()
    ui.setupUi(dialog)
    dialog.show()
    sys.exit(app.exec_())


if __name__ == '__main__':
    main()

遇到问题

  • 我在getOpenFileName方法这里,遇到第一个参数必须是继承于QtWidgets对象的问题,所以修改了Ui_Dialog让其直接继承于QtWidgets.QMainWindow
fileName, fileType = QFileDialog.getOpenFileName(self, "选取文件", os.getcwd(),"All Files(*);;Text Files(*.txt)")

最终效果

在这里插入图片描述

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
# -*- coding: utf-8 -*- # Form implementation generated from reading ui file 'aaa.ui' # # Created by: PyQt5 UI code generator 5.11.3 # # WARNING! All changes made in this file will be lost! import sys, os from PyQt5 import QtCore, QtWidgets, QtGui from PyQt5.QtCore import * from PyQt5.QtWidgets import * from PyQt5.QtGui import * from PyQt5 import QtCore, QtGui, QtWidgets class U_MainWindow(object): def setupUi(self, MainWindow): MainWindow.setObjectName("MainWindow") MainWindow.resize(1204, 836) sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Fixed) sizePolicy.setHorizontalStretch(0) sizePolicy.setVerticalStretch(0) sizePolicy.setHeightForWidth(MainWindow.sizePolicy().hasHeightForWidth()) MainWindow.setSizePolicy(sizePolicy) self.centralwidget = QtWidgets.QWidget(MainWindow) self.centralwidget.setObjectName("centralwidget") self.tableWidget = QtWidgets.QTableWidget(self.centralwidget) self.tableWidget.setGeometry(QtCore.QRect(80, 240, 1041, 371)) self.tableWidget.setAutoScrollMargin(9) self.tableWidget.setRowCount(9) self.tableWidget.setColumnCount(8) self.tableWidget.setObjectName("tableWidget") item = QtWidgets.QTableWidgetItem() self.tableWidget.setVerticalHeaderItem(0, item) item = QtWidgets.QTableWidgetItem() self.tableWidget.setVerticalHeaderItem(1, item) item = QtWidgets.QTableWidgetItem() self.tableWidget.setVerticalHeaderItem(2, item) item = QtWidgets.QTableWidgetItem() self.tableWidget.setVerticalHeaderItem(3, item) item = QtWidgets.QTableWidgetItem() self.tableWidget.setVerticalHeaderItem(4, item) item = QtWidgets.QTableWidgetItem() self.tableWidget.setVerticalHeaderItem(5, item) item = QtWidgets.QTableWidgetItem() self.tableWidget.setVerticalHeaderItem(6, item) item = Q
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值