栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

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

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

(图像处理)课上小作业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)")
最终效果

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/822117.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号