如何在Qt标签中显示图像?

问题描述 投票:1回答:1

我在Qt标签上的表单上显示图像时遇到了一些问题...'b'是我传递给构造函数的图像,当我在构造函数中显示图像时显示图像,但是当我将其设置在标签中,我仍然收到没有图像的表格。也没有显示任何错误。

def on_clicked_micro(self):
        self.obj3=MyForm2(b=self.blur)
        self.obj3.show()
        self.hide()

class MyForm2(QtGui.QMainWindow,Ui_image3):
    def __init__(self,parent=None,b=None):
            QtGui.QMainWindow.__init__(self,parent)
            self.imgPreProc=imagePreProcessor()
            cv2.imshow('blur',b) #############displaying the image #############
            self.label = QtGui.QLabel(self)
            self.setupUi(self)
            self.label.setPixmap(QtGui.QPixmap(b))
python-2.7 opencv pyqt4
1个回答
2
投票

变量b的类型从cv2.imread('picture.bmp')返回。类QtGui.QPixmap是通过文件名(或路径)传递图像而不是cv对象。它不应该显示它。

它可能必须用两种方法修复它们;

1]您可以通过使用设置的数组数据,宽度和高度在QtGui.QImage中进行转换(例如,进行转换的示例,您可以阅读this答案或this博客)。并使用QtGui.QPixmap转换为QPixmap QPixmap.fromImage (QImage image, Qt.ImageConversionFlags flags = Qt.AutoColor)。并使用相同的方法将其加载到QtGui.QLabel中。并且不要忘记设置QtGui.QLabel的布局和大小。如果图像,您在QtGui.QLabel中的通过率很小。我看不到它们。

class MyForm2(QtGui.QMainWindow,Ui_image3):
    def __init__(self,parent=None,b=None):
        QtGui.QMainWindow.__init__(self,parent)
        self.imgPreProc=imagePreProcessor()
        cv2.imshow('blur',b)
        self.label = QtGui.QLabel(self)
        self.setupUi(self)
        myQImage = self.imageOpenCv2ToQImage(b)
        self.label.setPixmap(QtGui.QPixmap.fromImage(myQImage))

   def imageOpenCv2ToQImage (self, cv_img):
        height, width, bytesPerComponent = cv_img.shape
        bytesPerLine = bytesPerComponent * width;
        cv2.cvtColor(cv_img, cv2.CV_BGR2RGB, cv_img)
        return QtGui.QImage(cv_img.data, width, height, bytesPerLine, QtGui.QImage.Format_RGB888)

2]您添加的变量path image file name将图像直接在构造函数中传递给QtGui.QPixmap。比解决No.1容易。并且也不要忘记设置QtGui.QLabel的布局和大小。

class MyForm2(QtGui.QMainWindow,Ui_image3):
    def __init__(self, pathFileName, parent = None):
        QtGui.QMainWindow.__init__(self, parent)
        self.imgPreProc = imagePreProcessor()
        cv2.imshow('blur', cv2.imread(pathFileName))
        self.label = QtGui.QLabel(self)
        self.setupUi(self)
        self.label.setPixmap(QtGui.QPixmap(pathFileName))
© www.soinside.com 2019 - 2024. All rights reserved.