2011-07-12 93 views
1

我嘗試使用OpenCV的處理一些視頻,然後將其放在PyQt的QImage的...蟒蛇轉換的IplImage到QImage的

我看到一些例子來做到這一點,但他們都在C++中,我只能聽懂蟒蛇,

誰能幫助我,請...謝謝

回答

2

您可以使用下面的代碼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 

,然後就打開轉換簡歷要與CvMat一個numpy的陣列主叫toQImage()

arr = numpy.asarray(mat) 
qim = toQImage(arr) 

之前也http://opencv.willowgarage.com/documentation/python/cookbook.html見的OpenCV的和與CvMat數組numpy的之間的轉換。

1

這對我有效。

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

我想CvMat中從問同樣的問題,以QImage的談話,我沒有找到任何蟒例如。

我解決了它這樣的:

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

此功能使用QT5工作對我來說,使用JPEG從本地磁盤

from PyQt5.QtGui import QImage 

def convertMatToQImage(cvImg): 
    return QImage(cvImg.data, cvImg.shape[1], cvImg.shape[0], QImage.Format_RGB32)