对于实验室应用,我需要使用投影仪投影图像,然后再使用相机记录投影图像。在这种情况下,重要的是在我们用相机拍摄之前,请确保已投影出框架。但是,由于我们必须对大量图像执行此操作,因此我们不希望后续帧之间的延迟很长。如何创建一个绘制图像并仅在将框架绘制到屏幕上之后才返回的函数?
我在下面有一个代码示例。
import PyQt5
from PyQt5 import QtGui, QtCore, QtWidgets
import numpy as np
class ShowLiveImage(QtWidgets.QWidget):
def __init__(self):
""" Create a tiny window in which a frame can be displayed using
show_image.
"""
super().__init__()
self.imageLabel = QtWidgets.QLabel()
self.layout = QtWidgets.QVBoxLayout()
self.setLayout(self.layout)
self.layout.addWidget(self.imageLabel)
self.window().setMinimumSize(512,512)
self.win = self.window()
self.show()
def show_image(self, image_array, wait_for_drawing=True):
"""
Shows an image given as a numpy array and (should) wait for the frame to be drawn onto the monitor before returning.
"""
N = image_array.shape[0]
self.im = QtGui.QImage(image_array, N, N, QtGui.QImage.Format_Grayscale8)
qpm = QtGui.QPixmap(self.im)
self.imageLabel.setPixmap(qpm)
if wait_for_drawing:
# These do not help, what do I do here?
self.imageLabel.repaint()
QtGui.QGuiApplication.postEvent(self, QtCore.QEvent(QtCore.QEvent.UpdateRequest))
QtGui.QGuiApplication.processEvents()
但是,当我继续使用此代码更新图像时,帧速率太高了,在我的计算机上约为400 fps,我通过以下方式进行了测试:
from PyQt5.Qt import QApplication
import time
app = QApplication([])
viewer = ShowLiveImage()
t0 = time.time()
for i in range(1000):
random_image = np.random.randint(0,255,(512,512), np.uint8)
viewer.show_image(random_image)
t1 = time.time()
print(' FPS: ', 1000./(t1-t0))
我如何确保show_image仅在投影框架时返回?