I am trying to process some videos using openCV and then put it inside pyqt Qimage...
I saw some examples to do that but they are all in C++ and I can understand python only,
Can anyone help me please ... thank you
您可以使用以下代码将 numpy 数组转换为 QImage:
from PyQt4.QtGui import QImage, qRgb
import numpy as np
class NotImplementedException:
pass
gray_color_table = [qRgb(i, i, i) for i in range(256)]
def toQImage(im, copy=False):
if im is None:
return QImage()
if im.dtype == np.uint8:
if len(im.shape) == 2:
qim = QImage(im.data, im.shape[1], im.shape[0], im.strides[0], QImage.Format_Indexed8)
qim.setColorTable(gray_color_table)
return qim.copy() if copy else qim
elif len(im.shape) == 3:
if im.shape[2] == 3:
qim = QImage(im.data, im.shape[1], im.shape[0], im.strides[0], QImage.Format_RGB888);
return qim.copy() if copy else qim
elif im.shape[2] == 4:
qim = QImage(im.data, im.shape[1], im.shape[0], im.strides[0], QImage.Format_ARGB32);
return qim.copy() if copy else qim
raise NotImplementedException
然后在调用之前将 OpenCV 的 CvMat 转换为 numpy 数组toQImage()
arr = numpy.asarray(mat)
qim = toQImage(arr)
有关 OpenCV 的 CvMat 和 numpy 数组之间的转换,另请参见http://opencv.willowgarage.com/documentation/python/cookbook.html。
这对我有用。
camcapture = cv.CaptureFromCAM(0)
cv.SetCaptureProperty(camcapture,cv.CV_CAP_PROP_FRAME_WIDTH, 1280)
cv.SetCaptureProperty(camcapture,cv.CV_CAP_PROP_FRAME_HEIGHT, 720);
frame = cv.QueryFrame(camcapture)
image = QImage(frame.tostring(), frame.width, frame.height, QImage.Format_RGB888).rgbSwapped()
pixmap = QPixmap.fromImage(image)
我想从 cvMat 到 QImage 对话问同样的问题,但我没有找到任何 python 示例。
我是这样解决的:
pano = cv.CreateMat(int(height),int(width),0)
cv.Zero(pano)
cv.Resize(self.image,pano)
self._data=pano.tostring('raw')
image2=QImage(self._data,pano.width,pano.height,QtGui.QImage.Format_Indexed8)
self.imageLabel.setPixmap(QPixmap(image2))
此功能使用 QT5 对我有用,使用本地磁盘中的 JPEG
from PyQt5.QtGui import QImage
def convertMatToQImage(cvImg):
return QImage(cvImg.data, cvImg.shape[1], cvImg.shape[0], QImage.Format_RGB32)