OpenCVで取得した画像をPySideで表示する

2015/8/11

Python2.7.6, OpenCV3.0.0, PySide1.2.2

変換部分としてはこんな。

import cv2
from PySide import QtCore,QtGui
~~~
cv_img = cv2.imread('test.png')
cv_img =  cv2.cvtColor(cv_img,cv2.COLOR_BGR2RGB)

height, width, dim = cv_img.shape
bytesPerLine = dim * width

qt_img = QtGui.QImage(cv_img.data, width, height, bytesPerLine, QtGui.QImage.Format_RGB888)


OpenCVを使って取得したUSBカメラの画像をPySideのウィンドウに表示する

# -*- coding: utf-8 -*-

import sys,os
from PySide import QtCore,QtGui
import cv2

class Viewer():

    repeatTime = 100 # ms

    def __init__(self):
        #camera setup
        self.capture = cv2.VideoCapture(0)

        if self.capture.isOpened() is False:
            raise("IO Error")

        #window setup
        self.view = QtGui.QGraphicsView()
        self.scene = QtGui.QGraphicsScene()
        self.set()

        #update timer
        timer = QtCore.QTimer(self.view)
        timer.timeout.connect(self.set)
        timer.start(self.repeatTime)

    def set(self):

        ret, cv_img = self.capture.read()
        if ret == False:
            return
        cv_img = cv2.cvtColor(cv_img,cv2.COLOR_BGR2RGB)
        height, width, dim = cv_img.shape
        bytesPerLine = dim * width
        self.image = QtGui.QImage(cv_img.data, width, height, bytesPerLine, QtGui.QImage.Format_RGB888)
        self.item = QtGui.QGraphicsPixmapItem(QtGui.QPixmap.fromImage(self.image))
        self.scene.addItem(self.item)
        self.view.setScene(self.scene)

    def show(self):
        self.view.show()

if __name__ == '__main__':
    app = QtGui.QApplication(sys.argv)
    viewer = Viewer()
    viewer.show()
    sys.exit(app.exec_())


PyQt(PySide)で画像処理その3(OpenCVとの連携)
http://tatabox.hatenablog.com/entry/2014/09/02/185727