最近在做数字图像可视化的实验,学着别人的代码自己写,写到一半准备先试一下读取并显示图片,但是发现选择文件后窗口自己就退出了,后来发现不管按哪个都会直接退出
代码如下:
from PyQt5 import QtWidgets
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
from jiemian import Ui_mainWindow
import numpy as np
import sys
import cv2
import matplotlib.pyplot as plt
tmp_path="C:\\Users\\nobody\\Desktop\\tmp"
class Demo(QtWidgets.QMainWindow, Ui_mainWindow):
def __init__(self):
super(Demo, self).__init__()
self.setupUi(self)
# 打开文件
self.actionopen.triggered.connect(self.read_file)
#清空
self.pushButton.clicked.connect(self.clear)
#直方图均衡
self.actionjunhenghua.triggered.connect(self.equal_func)
#高斯噪声
self.actiongaosi.triggered.connect(self.gaussnoi_func)
#sobel
self.actionsobel.triggered.connect(self.sobel)
#lapres
self.actionlapres.triggered.connect(self.lapres)
'''
#同态
self.actiontongtai.triggered.connect(self.huidu)
'''
def read_file(self):
# 打开原始图像
filename, filetype = QFileDialog.getOpenFileName(self, "打开文件", "D:/", "All Files(*);;Text Files(*.png)")
if filetype == '': # 若路径无效或打开文件失败,则什么也不做,让用户回到主界面
return
img = cv2.imread(filename) # 用cv库打开文件
global imgorg
imgorg = img
cv2.imwrite(tmp_path, img) # 将处理后的图像保存在临时路径中
# 这里虽然只是打开图像而没有进行处理,但是也会拷贝一份作为缓存,供后续的图像处理函数读取
self.label.setPixmap(tmp_path) # 读取原始图像并显示
def equal_func(self):
# 直方图均衡
img = cv2.imread(tmp_path) # 读取临时路径中的图像
src = img.copy()
des = np.empty(src.shape, dtype=np.uint8)
x = np.zeros([256]) # 8位数字图像一共有256个像素
for i in range(src.shape[0]):
for j in range(src.shape[1]):
x[src[i][j]] += 1
x = x / src.size
sum_x = np.zeros([256])
for i, _ in enumerate(x):
sum_x[i] = sum(x[:i])
for i in range(src.shape[0]):
for j in range(src.shape[1]):
des[i][j] = 255 * sum_x[src[i][j]]
cv2.imwrite(tmp_path, des) # 将处理后的图像保存在临时路径中
self.label_2.setPixmap(QPixmap(tmp_path)) # 显示处理后的图像
def gaussnoi_func(self):
img = cv2.imread(tmp_path, 0) # 读取临时路径中的图像
src = img.copy()
des = np.empty(src.shape, dtype=np.uint8)
try: # 如果输入的参数有效,则以该参数处理图像
p1 = float(self.para1.toPlainText())
p2 = float(self.para2.toPlainText())
src = img.copy()
src = np.array(src / 255, dtype=float)
noise = np.random.normal(p1, p2 ** 0.5, src.shape)
des = src + noise
if des.min() < 0:
low_clip = -1.
else:
low_clip = 0.
des = np.clip(des, low_clip, 1.0)
des = np.uint8(des * 255)
except Exception: # 否则,以默认的方式处理图像
src = img.copy()
src = np.array(src / 255, dtype=float)
noise = np.random.normal(0, 1 ** 0.5, src.shape)
des = src + noise
if des.min() < 0:
low_clip = -1.
else:
low_clip = 0.
des = np.clip(des, low_clip, 1.0)
des = np.uint8(des * 255)
cv2.imwrite(tmp_path, des) # 将处理后的图像保存在临时路径中
self.label_2.setPixmap(QPixmap(tmp_path)) # 显示处理后的图像
# sobel算子
def sobel(self):
x = cv2.Sobel(tmp_path, cv2.CV_16S, 1, 0)
y = cv2.Sobel(tmp_path, cv2.CV_16S, 0, 1)
absx = cv2.convertScaleAbs(x)
absy = cv2.convertScaleAbs(y)
Sobel = cv2.addWeighted(absx, 0.5, absy, 0.5, 0)
# 将图像颜色从RGB转换为RGBA
img_cv = cv2.cvtColor(Sobel, cv2.COLOR_BGR2RGB)
cv2.imwrite(tmp_path, img_cv) # 将处理后的图像保存在临时路径中
self.label_2.setPixmap(QPixmap(tmp_path))
#lapres
def lapres(self):
gimg=cv2.cvtColor(tmp_path,cv2.COLOR_BGR2GRAY)
kernel=np.array([[0,-1,0],[-1,5,-1],[0,-1,0]],np,32)
dst=cv2.filter2D(tmp_path,-1,kernel-kernel) #调用opencv
imgesl=np.hstack([tmp_path,dst])
cv2.imwrite(tmp_path, imgesl) # 将处理后的图像保存在临时路径中
self.label_2.setPixmap(QPixmap(tmp_path))
#清空
def clear(self):
tmp_path=imgorg
self.label_2.setPixmap(QPixmap(tmp_path))
#同态滤波器
if __name__ == '__main__':
app = QApplication(sys.argv)
demo = Demo()
demo.show()
sys.exit(app.exec_())
后面是ui代码:
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'jiemian.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_mainWindow(object):
def setupUi(self, mainWindow):
mainWindow.setObjectName("mainWindow")
mainWindow.resize(806, 620)
self.centralwidget = QtWidgets.QWidget(mainWindow)
self.centralwidget.setObjectName("centralwidget")
self.pushButton = QtWidgets.QPushButton(self.centralwidget)
self.pushButton.setGeometry(QtCore.QRect(280, 450, 121, 41))
self.pushButton.setObjectName("pushButton")
self.label_2 = QtWidgets.QLabel(self.centralwidget)
self.label_2.setGeometry(QtCore.QRect(420, 10, 361, 371))
self.label_2.setObjectName("label_2")
self.label = QtWidgets.QLabel(self.centralwidget)
self.label.setGeometry(QtCore.QRect(10, 20, 361, 361))
self.label.setObjectName("label")
mainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(mainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 806, 22))
self.menubar.setObjectName("menubar")
self.menu = QtWidgets.QMenu(self.menubar)
self.menu.setObjectName("menu")
self.menu_2 = QtWidgets.QMenu(self.menubar)
self.menu_2.setObjectName("menu_2")
self.menu_3 = QtWidgets.QMenu(self.menubar)
self.menu_3.setObjectName("menu_3")
self.menu_4 = QtWidgets.QMenu(self.menubar)
self.menu_4.setObjectName("menu_4")
self.menu_5 = QtWidgets.QMenu(self.menubar)
self.menu_5.setObjectName("menu_5")
self.menu_6 = QtWidgets.QMenu(self.menubar)
self.menu_6.setObjectName("menu_6")
mainWindow.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(mainWindow)
self.statusbar.setObjectName("statusbar")
mainWindow.setStatusBar(self.statusbar)
self.actionopen = QtWidgets.QAction(mainWindow)
self.actionopen.setObjectName("actionopen")
self.actionjiaoyan = QtWidgets.QAction(mainWindow)
self.actionjiaoyan.setObjectName("actionjiaoyan")
self.actiongaosi = QtWidgets.QAction(mainWindow)
self.actiongaosi.setObjectName("actiongaosi")
self.actionjunyun = QtWidgets.QAction(mainWindow)
self.actionjunyun.setObjectName("actionjunyun")
self.actionlapres = QtWidgets.QAction(mainWindow)
self.actionlapres.setObjectName("actionlapres")
self.actionsobel = QtWidgets.QAction(mainWindow)
self.actionsobel.setObjectName("actionsobel")
self.actiontongtai = QtWidgets.QAction(mainWindow)
self.actiontongtai.setObjectName("actiontongtai")
self.actionjunhenghua = QtWidgets.QAction(mainWindow)
self.actionjunhenghua.setObjectName("actionjunhenghua")
self.menu.addSeparator()
self.menu.addAction(self.actionopen)
self.menu_2.addSeparator()
self.menu_2.addAction(self.actiontongtai)
self.menu_3.addSeparator()
self.menu_3.addAction(self.actionjunhenghua)
self.menu_4.addAction(self.actionlapres)
self.menu_4.addAction(self.actionsobel)
self.menu_6.addAction(self.actionjiaoyan)
self.menu_6.addAction(self.actiongaosi)
self.menu_6.addAction(self.actionjunyun)
self.menubar.addAction(self.menu.menuAction())
self.menubar.addAction(self.menu_2.menuAction())
self.menubar.addAction(self.menu_3.menuAction())
self.menubar.addAction(self.menu_4.menuAction())
self.menubar.addAction(self.menu_5.menuAction())
self.menubar.addAction(self.menu_6.menuAction())
self.retranslateUi(mainWindow)
QtCore.QMetaObject.connectSlotsByName(mainWindow)
def retranslateUi(self, mainWindow):
_translate = QtCore.QCoreApplication.translate
mainWindow.setWindowTitle(_translate("mainWindow", "数字图像可视化"))
self.pushButton.setText(_translate("mainWindow", "清空"))
self.label_2.setText(_translate("mainWindow", "处理后"))
self.label.setText(_translate("mainWindow", "初始图"))
self.menu.setTitle(_translate("mainWindow", "打开文件"))
self.menu_2.setTitle(_translate("mainWindow", "同态滤波器"))
self.menu_3.setTitle(_translate("mainWindow", "直方图处理"))
self.menu_4.setTitle(_translate("mainWindow", "锐化"))
self.menu_5.setTitle(_translate("mainWindow", "平滑"))
self.menu_6.setTitle(_translate("mainWindow", "添加噪声"))
self.actionopen.setText(_translate("mainWindow", "打开"))
self.actionjiaoyan.setText(_translate("mainWindow", "jiaoyan"))
self.actiongaosi.setText(_translate("mainWindow", "gaosi"))
self.actionjunyun.setText(_translate("mainWindow", "junyun"))
self.actionlapres.setText(_translate("mainWindow", "lapres"))
self.actionsobel.setText(_translate("mainWindow", "sobel"))
self.actiontongtai.setText(_translate("mainWindow", "tongtai"))
self.actionjunhenghua.setText(_translate("mainWindow", "junhenghua"))
即使我照搬了参考的代码也还是不行,知识浅薄请各位求解,谢谢!