私はしばらくの間OpenCV GUI関数を使っていましたが、その可能性はPythonユーザーにとっては少し制限されています。今日私はPyqtを使い始めて、次の結論に達しました。qtは本当に混乱しています。 OpenCVのでマウスイベントの扱いopencv gui vs pyqt
私は次のようにします:今
マウスイベントに関する質問は
import cv2
cv2.namedWindow('Window',1)
def CallBackFunc(event,x,y,flags,param):
global xc,yc,evt,flg
xc,yc,evt,flg=x,y,event,flags
cv2.setMouseCallback('Window', CallBackFunc)
これは、常にグローバル変数xc,yc,evt,flg
をリフレッシュする別々のスレッドを開き、私はどこでもそれらにアクセスすることができますいつでも私は欲しい。私はリフレッシュを停止したい場合は、私はちょうどcv2.setMouseCallback('Window',nothing)
は、nothing
は
def nothing():
pass
ができることは、マウスイベントに対処するための最も美しい方法ではないかもしれないが、私はそれで元気です。 PyQtでこのような自由をどうやって実現できますか?
EDIT:
は、たとえば、次のスクリプトは、白い円が表示され、常にそれにテキストを描画します。
import sys
from PySide import QtGui
import numpy as np
import cv2
class QCustomLabel (QtGui.QLabel):
def __init__ (self, parent = None):
super(QCustomLabel, self).__init__(parent)
self.setMouseTracking(True)
def mouseMoveEvent (self, eventQMouseEvent):
self.x,self.y=eventQMouseEvent.x(),eventQMouseEvent.y()
cvImg=np.zeros((900,900),dtype=np.uint8)
cv2.circle(cvImg,(449,449),100,255,-1)
cv2.putText(cvImg,"x at {}, y at {}".format(self.x,self.y),(375,455), cv2.FONT_HERSHEY_SIMPLEX,0.5,(0,0,0),1,cv2.LINE_AA)
height, width= cvImg.shape
bytearr=cvImg.data
qImg = QtGui.QImage(bytearr, width, height, QtGui.QImage.Format_Indexed8)
self.setPixmap(QtGui.QPixmap.fromImage(qImg))
def mousePressEvent (self, eventQMouseEvent):
self.evt=eventQMouseEvent.button()
class QCustomWidget (QtGui.QWidget):
def __init__ (self, parent = None):
super(QCustomWidget, self).__init__(parent)
self.setWindowOpacity(1)
# Init QLabel
self.positionQLabel = QCustomLabel(self)
# Init QLayout
layoutQHBoxLayout = QtGui.QHBoxLayout()
layoutQHBoxLayout.addWidget(self.positionQLabel)
self.setLayout(layoutQHBoxLayout)
self.show()
if QtGui.QApplication.instance() is not None:
myQApplication=QtGui.QApplication.instance()
else:
myQApplication = QtGui.QApplication(sys.argv)
myQTestWidget = QCustomWidget()
myQTestWidget.show()
myQApplication.exec_()
ここでの問題は、QCustomLabelクラス内で、またMouseMoveEvent関数内で実行されるという点です。しかし、別の機能が必要なので、そのクラスの外にあるdrawCircle
と呼んで、マウスの位置とイベントにアクセスできます。 opencvでは、これは全く問題ありません。そして、それはpyqtの実装に必要な書き込み作業のほんの一部にすぎません。
私は正しい質問があると思います:私はまだpyqtが好きではないのですか?
あなたはPySide、ないPyQtはを使用しています。しかしどちらの方法でも、ある言語/ライブラリ/フレームワークを別の言語/フレームワークと同じように動作させることは常に間違いです。 – ekhumoro