gpt4 book ai didi

python - 无法从类传递调整大小的 QLabel 几何

转载 作者:行者123 更新时间:2023-12-02 17:04:52 27 4
gpt4 key购买 nike

我正在使用 PyQt5 和 OpenCV。我想创建一个读取视频帧并执行橡皮筋拉伸(stretch)以生成几何图形的类,该几何图形将由不同的类用于裁剪视频流(此示例中不包括第二类)。

在此示例中,从网络摄像头流中捕获图像,然后显示。在图像上拉伸(stretch)的橡皮筋会生成打印的几何图形。 ReGeomVid 中的几何打印没有问题类,但不在 main() 内.我需要将几何图形放入 main() .帮助表示赞赏。

import sys, cv2
from PyQt5.QtWidgets import QRubberBand, QApplication, QLabel
from PyQt5.QtGui import QPixmap, QImage
from PyQt5.QtCore import QRect, QSize

class ReGeomVid (QLabel):
def __init__(self, cap, parent=None):
super(ReGeomVid, self).__init__(parent)
self.cap = cap
self.currentQRect = QRect()
self.initUI()

def initUI (self):
ret, frame = self.cap.read() #First frame read is black
ret, frame = self.cap.read() #Second frame read is normal
if ret == True:
frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
img = QImage(frame,frame.shape[1], frame.shape[0], QImage.Format_RGB888)
pix = QPixmap.fromImage(img)
self.setPixmap(QPixmap(pix))

def mousePressEvent (self, eventQMouseEvent):
self.originQPoint = eventQMouseEvent.pos()
print(self.originQPoint)
self.currentQRubberBand = QRubberBand(QRubberBand.Rectangle, self)
self.currentQRubberBand.setGeometry(QRect(self.originQPoint, QSize()))
self.currentQRubberBand.show()

def mouseMoveEvent (self, eventQMouseEvent):
self.currentQRubberBand.setGeometry(QRect(self.originQPoint, eventQMouseEvent.pos()).normalized())

def mouseReleaseEvent (self, eventQMouseEvent):
self.currentQRubberBand.hide()
self.currentQRect = self.currentQRubberBand.geometry()
self.currentQRubberBand.deleteLater()
self.croppedPixmap = self.pixmap().copy(self.currentQRect)
print("In mouserelease: Geometry = ", self.currentQRect)

if __name__ == '__main__':
myQApplication = QApplication(sys.argv)
stream = cv2.VideoCapture(0)
x = ReGeomVid(stream)
x.show()
pixMainGeom = x.currentQRect
print("In main: Geometry = ", x.currentQRect)
sys.exit(myQApplication.exec_())

最佳答案

你的变量self.currentQRect设置在 mouseReleaseEvent .所以,当你的 main 中的 print 被执行时,它仍然是无效的。

self.currentQRect 出现时,使用信号在你的 main 中运行代码准备好了:

class ReGeomVid (QLabel):
currentQRectChanged = pyqtSignal(QRect)
...
def mouseReleaseEvent (self, eventQMouseEvent):
...
self.currentQRectChanged.emit(self.currentQRect)

def printCurrentQRect(rect):
print("In main: Geometry = ", rect)


if __name__ == '__main__':
myQApplication = QApplication(sys.argv)
stream = cv2.VideoCapture(0)
x = ReGeomVid(stream)
x.show()
x.currentQRectChanged.connect(printCurrentQRect)
sys.exit(myQApplication.exec_())

关于python - 无法从类传递调整大小的 QLabel 几何,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56395324/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com