Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/opencv/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 2.7 如何在Qt标签中显示图像?_Python 2.7_Opencv_Pyqt4 - Fatal编程技术网

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

Python 2.7 如何在Qt标签中显示图像?,python-2.7,opencv,pyqt4,Python 2.7,Opencv,Pyqt4,我在Qt label中的窗体上显示图像时遇到了一些问题…“b”是我传递给构造函数的图像,当我在构造函数中显示图像时,图像会显示出来,但当我在label中设置图像时,我仍然得到一个没有图像的窗体。它也不会显示任何错误 def on_clicked_micro(self): self.obj3=MyForm2(b=self.blur) self.obj3.show() self.hide() class MyForm2(QtGui.QMainWind

我在Qt label中的窗体上显示图像时遇到了一些问题…“b”是我传递给构造函数的图像,当我在构造函数中显示图像时,图像会显示出来,但当我在label中设置图像时,我仍然得到一个没有图像的窗体。它也不会显示任何错误

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))

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

它有可能以两种方式修复它们

1) 您可以使用设置的数组数据、宽度和高度在
QtGui.QImage
中转换它们(例如转换,您可以阅读答案或博客)。并使用转换为
QtGui.QPixmap
。并使用相同的方法将其加载到
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
。这比解决第一题容易。不要忘记设置
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))