您好,登錄后才能下訂單哦!
這篇文章主要介紹了pyqt5如何使用cv2顯示圖片,具有一定借鑒價(jià)值,感興趣的朋友可以參考下,希望大家閱讀完這篇文章之后大有收獲,下面讓小編帶著大家一起了解一下。
如下所示:
#! /usr/bin/python3 # coding = utf-8 # from PyQt5 import QtGui,QtCore,Qt import sys from PyQt5.QtCore import Qt,pyqtSignal,QSize,QRect,QMetaObject, QCoreApplication, pyqtSlot,QPropertyAnimation,QThread from PyQt5.QtGui import QIcon, QFont, QPixmap, QPainter, QImage from PyQt5.QtWidgets import QMainWindow, QApplication import cv2 from gevent.libev.corecext import SIGNAL, time from qtpy importQtCore class mycsms(QMainWindow): def __init__(self): super(mycsms, self).__init__() self.setupUi(self) self.image= QImage() self.device= cv2.VideoCapture(0) self.playTimer= Timer("updatePlay()") self.connect(self.playTimer, SIGNAL("updatePlay()"), self.showCamer) # 讀攝像頭 def showCamer(self): if self.device.isOpened(): ret, frame= self.device.read() else: ret = False # 讀寫磁盤方式 # cv2.imwrite("2.png",frame) #self.image.load("2.png") height, width, bytesPerComponent= frame.shape bytesPerLine = bytesPerComponent* width # 變換彩色空間順序 cv2.cvtColor(frame, cv2.COLOR_BGR2RGB,frame) # 轉(zhuǎn)為QImage對(duì)象 self.image= QImage(frame.data, width, height, bytesPerLine, QImage.Format_RGB888) self.view.setPixmap(QPixmap.fromImage(self.image)) if __name__ == "__main__": app = QApplication(sys.argv) myshow = mycsms() myshow.playTimer.start() myshow.show() sys.exit(app.exec_()) # 線程類: class Timer(QtCore.QThread): def __init__(self, signal="updateTime()", parent=None): super(Timer, self).__init__(parent) self.stoped= False self.signal= signal self.mutex= QtCore.QMutex() def run(self): with QtCore.QMutexLocker(self.mutex): self.stoped= False while True: if self.stoped: return self.emit(QtCore.SIGNAL(self.signal)) #40毫秒發(fā)送一次信號(hào) time.sleep(0.04) def stop(self): with QtCore.QMutexLocker(self.mutex): self.stoped= True def isStoped(self): with QtCore.QMutexLocker(self.mutex): return self.stoped
感謝你能夠認(rèn)真閱讀完這篇文章,希望小編分享的“pyqt5如何使用cv2顯示圖片”這篇文章對(duì)大家有幫助,同時(shí)也希望大家多多支持億速云,關(guān)注億速云行業(yè)資訊頻道,更多相關(guān)知識(shí)等著你來學(xué)習(xí)!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。