2015-11-16 48 views
2

這個問題之前貶爲一式二份,請注意:轉換OpenCV的3(IplImage結構),以PyQt5的QImage/QPixmap的Python中

  • 使用OpenCV的版本3(不是版本2)
  • 使用Python 3(不是C++)
  • 使用PyQt的5(未PyQt4的)

目標:試圖在一個PyQt5 GUI窗口以流的OpenCV網絡攝像頭的視頻幀

# For testing, I'm simply reading in a single image instead of a video frame 

frame = cv2.imread('Koala.jpg',0)  # this is a numpy.ndarray object 
height, width = frame.shape   
my_image = QImage(frame.tostring(), width, height, QImage.Format_RGB888) 

# By here, things seem okay. print(mimage) basically says <QImage object at 0x32243> 

# my_image.rgbSwapped()    # This line crashes program 
pixmap = QPixmap.fromImage(mimage) # This line crashes program as well 

我不知道如何調試這個了。沒有錯誤回溯被打印到控制檯。任何幫助表示讚賞。提前致謝。

回答

1

您需要首先將openCv圖像轉換爲QImage,並且它們使用像素圖來顯示圖像,如下所示,這是因爲像素圖支持QImage格式。

height, width, bytesPerComponent = Img.shape 
    bytesPerLine = 3 * width 
    cv2.cvtColor(Img, cv2.COLOR_BGR2RGB, Img)           
    QImg = QImage(Img.data, width, height, bytesPerLine,QImage.Format_RGB888) 

現在使用這個QImg至像素圖

pixmap = QPixmap.fromImage(QImg)