问题遇到的现象和发生背景
使用PyQt5的label展示cv处理后的图片时,图片无法显示出来
问题相关代码,请勿粘贴截图
下面是主程序代码,要在Mainwindow.label上显示图片
import sys
from PIL import ImageQt, Image
from PyQt5 import QtWidgets
from PyQt5.QtWidgets import QMainWindow
from PyQt5.QtGui import QPixmap
from UI.window import Ui_Form
from PyQt5 import QtCore
from Net.fasterrcnn import Net
import cv2
from PyQt5 import QtGui
import os
def cvImgtoQtImg(cvImg): # 定义opencv图像转PyQt图像的函数
qt_img = cv2.cvtColor(cvImg, cv2.COLOR_BGR2RGB)
qt_img = QtGui.QImage(cvImg.data, # 数据源
cvImg.shape[1], # 宽度
cvImg.shape[0], # 高度
cvImg.shape[1] * 3, # 行字节数
QtGui.QImage.Format_RGB888)
return QtGui.QPixmap.fromImage(qt_img)
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
Mainwindow = Ui_Form()
mainwindow = QMainWindow()
Mainwindow.setupUi(mainwindow)
# pix = QPixmap("./1.jpeg").scaled(Mainwindow.label.width(), Mainwindow.label.height())
# Mainwindow.label.setPixmap(pix)
QtCore.QCoreApplication.addLibraryPath(os.path.join(os.path.dirname(QtCore.__file__), "plugins"))
img = cv2.imread('1.png')
# 图片转灰
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# 实例化网络
net = Net()
img = net.get_img(img, gray)
下面是界面的代码
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_Form(QtWidgets.QWidget):
def setupUi(self, Form):
Form.setObjectName("Form")
Form.resize(905, 809)
self.label = QtWidgets.QLabel(Form)
self.label.setGeometry(QtCore.QRect(100, 50, 721, 631))
self.label.setScaledContents(True)
self.label.setAlignment(QtCore.Qt.AlignCenter)
self.label.setObjectName("label")
self.retranslateUi(Form)
QtCore.QMetaObject.connectSlotsByName(Form)
def retranslateUi(self, Form):
_translate = QtCore.QCoreApplication.translate
Form.setWindowTitle(_translate("Form", "Form"))
self.label.setText(_translate("Form", "Video"))