请教一下大伙们,刚刚开始学习使用Pyqt5时遇到的一个问题,想问问为什么会有这个报错:
AttributeError: 'myWindow' object has no attribute 'label'
具体就是在运行可以弹出窗口,但是点击第一个[RGB-Lab]按钮就报错,我想要的是能显示图片
主函数
import sys
import cv2
from PyQt5.QtGui import QImage, QPixmap
from colortransfer import Ui_MainWindow #导入转化为py的ui文件
from PyQt5.QtWidgets import QApplication, QMainWindow
# 导入必要的包
import numpy as np
import cv2 as cv #修改
from matplotlib import pyplot as plt
def color_transfer(source, target):
# 将图像从 RGB 转换为 Lab 色彩空间,确保使用浮点数据类型
# (注意:OpenCV 希望浮点数为 32 位,因此请使用它而不是 64 位)
source = cv.cvtColor(source, cv.COLOR_BGR2LAB).astype("float32")
target = cv.cvtColor(target, cv.COLOR_BGR2LAB).astype("float32")
# 计算源图像和目标图像的颜色统计信息
(lMeanSrc, lStdSrc, aMeanSrc, aStdSrc, bMeanSrc, bStdSrc) = image_stats(source)
(lMeanTar, lStdTar, aMeanTar, aStdTar,bMeanTar, bStdTar) = image_stats(target)
# 从目标图像中减去均值
(l, a, b) = cv.split(target)
l -= lMeanTar
a -= aMeanTar
b -= bMeanTar
# 按标准差缩放
l = (lStdTar / lStdSrc) * l
a = (aStdTar / aStdSrc) * a
b = (bStdTar / bStdSrc) * b
# 添加源均值
l += lMeanSrc
a += aMeanSrc
b += bMeanSrc
# 如果像素强度超出此范围,则将像素强度裁剪为[0, 255]
l = np.clip(l, 0, 255)
a = np.clip(a, 0, 255)
b = np.clip(b, 0, 255)
# 将通道合并在一起并转换回 RGB 颜色空间,确保使用 8 位无符号整数数据类型
transfer = cv.merge([l, a, b])
transfer = cv.cvtColor(transfer.astype("uint8"), cv.COLOR_LAB2BGR)
# 返回彩色传输的图像
return transfer
def image_stats(image):
# 计算每个通道的平均值和标准偏差,mean是平均值,std是标准偏差
(l, a, b) = cv.split(image)
(lMean, lStd) = (l.mean(), l.std())
(aMean, aStd) = (a.mean(), a.std())
(bMean, bStd) = (b.mean(), b.std())
# 返回颜色统计信息
return (lMean, lStd, aMean, aStd, bMean, bStd)
class myWindow(QMainWindow,Ui_MainWindow): # 调用主页面类
def __init__(self):
QMainWindow.__init__(self)
self.main_ui = Ui_MainWindow()
self.main_ui.setupUi(self)
def RGBtoLab(self):
image_source = cv.imread(r"C:\Users\cmj\Desktop\color_picture\source1.jpg")
# image_target = cv.imread(r"C:\Users\cmj\Desktop\color_picture\target1.jpg")
# image_transfer = color_transfer(image_source, image_target)
image1_height,image1_width,image1_depth = image_source.shape
QIm1 = cv2.cvtColor(image_source,cv2.COLOR_BGR2RGB)
QIm1 = QImage(QIm1.data, image1_width, image1_height, image1_width * image1_depth, QImage.Format_RGB888)
self.label.setPixmap(QPixmap.fromImage(QIm1))
def exit(self):
sys.exit()
if __name__ == '__main__':
app = QApplication(sys.argv)
# MainWindow = QMainWindow()
# ui = gui_file_name.Ui_MainWindow()
# ui.setupUi(MainWindow)
# 三行与下面一行加上面的类相等
myWin = myWindow()
# 显示
myWin.show()
sys.exit(app.exec_())
ui的py文件
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(1095, 823)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.label = QtWidgets.QLabel(self.centralwidget)
self.label.setGeometry(QtCore.QRect(150, 410, 72, 16))
self.label.setObjectName("label")
self.label_2 = QtWidgets.QLabel(self.centralwidget)
self.label_2.setGeometry(QtCore.QRect(500, 410, 72, 15))
self.label_2.setObjectName("label_2")
self.label_3 = QtWidgets.QLabel(self.centralwidget)
self.label_3.setGeometry(QtCore.QRect(830, 410, 72, 15))
self.label_3.setObjectName("label_3")
self.RGBtoLab = QtWidgets.QPushButton(self.centralwidget)
self.RGBtoLab.setGeometry(QtCore.QRect(190, 550, 93, 28))
self.RGBtoLab.setObjectName("RGBtoLab")
self.pushButton_2 = QtWidgets.QPushButton(self.centralwidget)
self.pushButton_2.setGeometry(QtCore.QRect(490, 550, 93, 28))
self.pushButton_2.setObjectName("pushButton_2")
self.pushButton_3 = QtWidgets.QPushButton(self.centralwidget)
self.pushButton_3.setGeometry(QtCore.QRect(710, 540, 93, 28))
self.pushButton_3.setObjectName("pushButton_3")
self.pushButton_4 = QtWidgets.QPushButton(self.centralwidget)
self.pushButton_4.setGeometry(QtCore.QRect(920, 560, 93, 28))
self.pushButton_4.setObjectName("pushButton_4")
self.exit_button = QtWidgets.QPushButton(self.centralwidget)
self.exit_button.setGeometry(QtCore.QRect(910, 700, 93, 28))
self.exit_button.setObjectName("exit_button")
self.verticalLayoutWidget = QtWidgets.QWidget(self.centralwidget)
self.verticalLayoutWidget.setGeometry(QtCore.QRect(50, 160, 271, 181))
self.verticalLayoutWidget.setObjectName("verticalLayoutWidget")
self.verticalLayout = QtWidgets.QVBoxLayout(self.verticalLayoutWidget)
self.verticalLayout.setContentsMargins(0, 0, 0, 0)
self.verticalLayout.setObjectName("verticalLayout")
self.verticalLayoutWidget_2 = QtWidgets.QWidget(self.centralwidget)
self.verticalLayoutWidget_2.setGeometry(QtCore.QRect(380, 160, 271, 181))
self.verticalLayoutWidget_2.setObjectName("verticalLayoutWidget_2")
self.verticalLayout_2 = QtWidgets.QVBoxLayout(self.verticalLayoutWidget_2)
self.verticalLayout_2.setContentsMargins(0, 0, 0, 0)
self.verticalLayout_2.setObjectName("verticalLayout_2")
self.verticalLayoutWidget_3 = QtWidgets.QWidget(self.centralwidget)
self.verticalLayoutWidget_3.setGeometry(QtCore.QRect(730, 160, 271, 181))
self.verticalLayoutWidget_3.setObjectName("verticalLayoutWidget_3")
self.verticalLayout_3 = QtWidgets.QVBoxLayout(self.verticalLayoutWidget_3)
self.verticalLayout_3.setContentsMargins(0, 0, 0, 0)
self.verticalLayout_3.setObjectName("verticalLayout_3")
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 1095, 26))
self.menubar.setObjectName("menubar")
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)
self.RGBtoLab.clicked.connect(MainWindow.RGBtoLab)
self.exit_button.clicked.connect(MainWindow.exit)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.label.setText(_translate("MainWindow", "源图片"))
self.label_2.setText(_translate("MainWindow", "目标图片"))
self.label_3.setText(_translate("MainWindow", "结果图片"))
self.RGBtoLab.setText(_translate("MainWindow", "RGB-Lab"))
self.pushButton_2.setText(_translate("MainWindow", "PushButton"))
self.pushButton_3.setText(_translate("MainWindow", "PushButton"))
self.pushButton_4.setText(_translate("MainWindow", "PushButton"))
self.exit_button.setText(_translate("MainWindow", "退出"))