Convert Python Opencv Image (numpy array) to PyQt QPixmap image

匿名 (未验证) 提交于 2019-12-03 03:10:03

问题:

I am trying to convert python opencv image to QPixmap.

I follow the instruction shows Page Link and my code is attached below

img = cv2.imread('test.png')[:,:,::1]/255.  imgDown = cv2.pyrDown(img) imgDown = np.float32(imgDown)         cvRGBImg = cv2.cvtColor(imgDown, cv2.cv.CV_BGR2RGB) qimg = QtGui.QImage(cvRGBImg.data,cvRGBImg.shape[1], cvRGBImg.shape[0], QtGui.QImage.Format_RGB888) pixmap01 = QtGui.QPixmap.fromImage(qimg) self.image01TopTxt = QtGui.QLabel('window',self) self.imageLable01 = QtGui.QLabel(self) self.imageLable01.setPixmap(pixmap01) 

The code has no compile and runtime error but the conversion is wrong and I just get some noise image. I am not sure what the problem is. Could anyone help?

回答1:

Use this to convert cvImage to Qimage, here cvImage is original image

height, width, channel = cvImg.shape bytesPerLine = 3 * width qImg = QImage(cvImg.data, width, height, bytesPerLine, QImage.Format_RGB888) 

and set this Qimage to Lable.setPixmap parameter from Qimage. It Works!!!



回答2:

#image is the numpy array that you got from cv2.imread(example_image.jpg)  image = QtGui.QImage(image, image.shape[1],\                             image.shape[0], image.shape[1] * 3,QtGui.QImage.Format_RGB888) pix = QtGui.QPixmap(image)  self.scene.addPixmap(pix) 


易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!