2012-03-02 02:55:32 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
2012-04-04 16:22:43 +00:00
|
|
|
"""
|
|
|
|
Tests the speed of image updates for an ImageItem and RawImageWidget.
|
|
|
|
The speed will generally depend on the type of data being shown, whether
|
|
|
|
it is being scaled and/or converted by lookup table, and whether OpenGL
|
|
|
|
is used by the view widget
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
|
|
import initExample ## Add path to library (just for examples; you do not need this)
|
2012-03-02 02:55:32 +00:00
|
|
|
|
|
|
|
|
2016-09-18 08:20:24 +00:00
|
|
|
from pyqtgraph.Qt import QtGui, QtCore, USE_PYSIDE, USE_PYSIDE2, USE_PYQT5
|
2012-03-02 02:55:32 +00:00
|
|
|
import numpy as np
|
|
|
|
import pyqtgraph as pg
|
|
|
|
import pyqtgraph.ptime as ptime
|
2012-09-09 23:07:36 +00:00
|
|
|
|
|
|
|
if USE_PYSIDE:
|
|
|
|
import VideoTemplate_pyside as VideoTemplate
|
2016-09-18 08:20:24 +00:00
|
|
|
elif USE_PYSIDE2:
|
|
|
|
import VideoTemplate_pyside2 as VideoTemplate
|
2015-02-28 16:05:57 +00:00
|
|
|
elif USE_PYQT5:
|
|
|
|
import VideoTemplate_pyqt5 as VideoTemplate
|
2012-09-09 23:07:36 +00:00
|
|
|
else:
|
|
|
|
import VideoTemplate_pyqt as VideoTemplate
|
|
|
|
|
2012-03-02 02:55:32 +00:00
|
|
|
|
|
|
|
#QtGui.QApplication.setGraphicsSystem('raster')
|
|
|
|
app = QtGui.QApplication([])
|
|
|
|
#mw = QtGui.QMainWindow()
|
|
|
|
#mw.resize(800,800)
|
|
|
|
|
|
|
|
win = QtGui.QMainWindow()
|
2013-02-25 04:09:03 +00:00
|
|
|
win.setWindowTitle('pyqtgraph example: VideoSpeedTest')
|
2012-03-02 02:55:32 +00:00
|
|
|
ui = VideoTemplate.Ui_MainWindow()
|
|
|
|
ui.setupUi(win)
|
|
|
|
win.show()
|
|
|
|
ui.maxSpin1.setOpts(value=255, step=1)
|
|
|
|
ui.minSpin1.setOpts(value=0, step=1)
|
|
|
|
|
2012-04-04 13:29:35 +00:00
|
|
|
#ui.graphicsView.useOpenGL() ## buggy, but you can try it if you need extra speed.
|
|
|
|
|
2012-03-02 02:55:32 +00:00
|
|
|
vb = pg.ViewBox()
|
|
|
|
ui.graphicsView.setCentralItem(vb)
|
|
|
|
vb.setAspectLocked()
|
|
|
|
img = pg.ImageItem()
|
|
|
|
vb.addItem(img)
|
|
|
|
vb.setRange(QtCore.QRectF(0, 0, 512, 512))
|
|
|
|
|
|
|
|
LUT = None
|
|
|
|
def updateLUT():
|
|
|
|
global LUT, ui
|
|
|
|
dtype = ui.dtypeCombo.currentText()
|
|
|
|
if dtype == 'uint8':
|
|
|
|
n = 256
|
|
|
|
else:
|
|
|
|
n = 4096
|
|
|
|
LUT = ui.gradient.getLookupTable(n, alpha=ui.alphaCheck.isChecked())
|
|
|
|
ui.gradient.sigGradientChanged.connect(updateLUT)
|
|
|
|
updateLUT()
|
|
|
|
|
|
|
|
ui.alphaCheck.toggled.connect(updateLUT)
|
|
|
|
|
|
|
|
def updateScale():
|
|
|
|
global ui
|
|
|
|
spins = [ui.minSpin1, ui.maxSpin1, ui.minSpin2, ui.maxSpin2, ui.minSpin3, ui.maxSpin3]
|
2012-11-23 21:01:25 +00:00
|
|
|
if ui.rgbLevelsCheck.isChecked():
|
2012-03-02 02:55:32 +00:00
|
|
|
for s in spins[2:]:
|
|
|
|
s.setEnabled(True)
|
|
|
|
else:
|
|
|
|
for s in spins[2:]:
|
|
|
|
s.setEnabled(False)
|
2012-11-23 21:01:25 +00:00
|
|
|
ui.rgbLevelsCheck.toggled.connect(updateScale)
|
2012-03-02 02:55:32 +00:00
|
|
|
|
|
|
|
cache = {}
|
|
|
|
def mkData():
|
2014-02-18 01:48:22 +00:00
|
|
|
with pg.BusyCursor():
|
|
|
|
global data, cache, ui
|
|
|
|
frames = ui.framesSpin.value()
|
|
|
|
width = ui.widthSpin.value()
|
|
|
|
height = ui.heightSpin.value()
|
|
|
|
dtype = (ui.dtypeCombo.currentText(), ui.rgbCheck.isChecked(), frames, width, height)
|
|
|
|
if dtype not in cache:
|
|
|
|
if dtype[0] == 'uint8':
|
|
|
|
dt = np.uint8
|
|
|
|
loc = 128
|
|
|
|
scale = 64
|
|
|
|
mx = 255
|
|
|
|
elif dtype[0] == 'uint16':
|
|
|
|
dt = np.uint16
|
|
|
|
loc = 4096
|
|
|
|
scale = 1024
|
|
|
|
mx = 2**16
|
|
|
|
elif dtype[0] == 'float':
|
|
|
|
dt = np.float
|
|
|
|
loc = 1.0
|
|
|
|
scale = 0.1
|
|
|
|
|
|
|
|
if ui.rgbCheck.isChecked():
|
|
|
|
data = np.random.normal(size=(frames,width,height,3), loc=loc, scale=scale)
|
2014-03-11 23:01:34 +00:00
|
|
|
data = pg.gaussianFilter(data, (0, 6, 6, 0))
|
2014-02-18 01:48:22 +00:00
|
|
|
else:
|
|
|
|
data = np.random.normal(size=(frames,width,height), loc=loc, scale=scale)
|
2014-03-11 23:01:34 +00:00
|
|
|
data = pg.gaussianFilter(data, (0, 6, 6))
|
2014-02-18 01:48:22 +00:00
|
|
|
if dtype[0] != 'float':
|
|
|
|
data = np.clip(data, 0, mx)
|
|
|
|
data = data.astype(dt)
|
2016-04-28 05:33:51 +00:00
|
|
|
data[:, 10, 10:50] = mx
|
|
|
|
data[:, 9:12, 48] = mx
|
|
|
|
data[:, 8:13, 47] = mx
|
2014-02-18 01:48:22 +00:00
|
|
|
cache = {dtype: data} # clear to save memory (but keep one to prevent unnecessary regeneration)
|
|
|
|
|
|
|
|
data = cache[dtype]
|
|
|
|
updateLUT()
|
|
|
|
updateSize()
|
|
|
|
|
|
|
|
def updateSize():
|
|
|
|
global ui
|
|
|
|
frames = ui.framesSpin.value()
|
|
|
|
width = ui.widthSpin.value()
|
|
|
|
height = ui.heightSpin.value()
|
|
|
|
dtype = np.dtype(str(ui.dtypeCombo.currentText()))
|
|
|
|
rgb = 3 if ui.rgbCheck.isChecked() else 1
|
|
|
|
ui.sizeLabel.setText('%d MB' % (frames * width * height * rgb * dtype.itemsize / 1e6))
|
|
|
|
|
|
|
|
|
2012-03-02 02:55:32 +00:00
|
|
|
mkData()
|
2014-02-18 01:48:22 +00:00
|
|
|
|
|
|
|
|
2012-03-02 02:55:32 +00:00
|
|
|
ui.dtypeCombo.currentIndexChanged.connect(mkData)
|
2012-11-23 21:01:25 +00:00
|
|
|
ui.rgbCheck.toggled.connect(mkData)
|
2014-02-18 01:48:22 +00:00
|
|
|
ui.widthSpin.editingFinished.connect(mkData)
|
|
|
|
ui.heightSpin.editingFinished.connect(mkData)
|
|
|
|
ui.framesSpin.editingFinished.connect(mkData)
|
|
|
|
|
|
|
|
ui.widthSpin.valueChanged.connect(updateSize)
|
|
|
|
ui.heightSpin.valueChanged.connect(updateSize)
|
|
|
|
ui.framesSpin.valueChanged.connect(updateSize)
|
|
|
|
|
|
|
|
|
2012-03-02 02:55:32 +00:00
|
|
|
|
|
|
|
ptr = 0
|
|
|
|
lastTime = ptime.time()
|
|
|
|
fps = None
|
|
|
|
def update():
|
|
|
|
global ui, ptr, lastTime, fps, LUT, img
|
|
|
|
if ui.lutCheck.isChecked():
|
|
|
|
useLut = LUT
|
|
|
|
else:
|
|
|
|
useLut = None
|
2014-02-18 01:48:22 +00:00
|
|
|
|
|
|
|
downsample = ui.downsampleCheck.isChecked()
|
2012-03-02 02:55:32 +00:00
|
|
|
|
|
|
|
if ui.scaleCheck.isChecked():
|
2012-11-23 21:01:25 +00:00
|
|
|
if ui.rgbLevelsCheck.isChecked():
|
2012-03-02 02:55:32 +00:00
|
|
|
useScale = [
|
|
|
|
[ui.minSpin1.value(), ui.maxSpin1.value()],
|
|
|
|
[ui.minSpin2.value(), ui.maxSpin2.value()],
|
|
|
|
[ui.minSpin3.value(), ui.maxSpin3.value()]]
|
|
|
|
else:
|
|
|
|
useScale = [ui.minSpin1.value(), ui.maxSpin1.value()]
|
|
|
|
else:
|
|
|
|
useScale = None
|
|
|
|
|
|
|
|
if ui.rawRadio.isChecked():
|
|
|
|
ui.rawImg.setImage(data[ptr%data.shape[0]], lut=useLut, levels=useScale)
|
2013-07-10 04:02:16 +00:00
|
|
|
ui.stack.setCurrentIndex(1)
|
|
|
|
elif ui.rawGLRadio.isChecked():
|
|
|
|
ui.rawGLImg.setImage(data[ptr%data.shape[0]], lut=useLut, levels=useScale)
|
|
|
|
ui.stack.setCurrentIndex(2)
|
2012-03-02 02:55:32 +00:00
|
|
|
else:
|
2014-02-18 01:48:22 +00:00
|
|
|
img.setImage(data[ptr%data.shape[0]], autoLevels=False, levels=useScale, lut=useLut, autoDownsample=downsample)
|
2013-07-10 04:02:16 +00:00
|
|
|
ui.stack.setCurrentIndex(0)
|
2012-03-02 02:55:32 +00:00
|
|
|
#img.setImage(data[ptr%data.shape[0]], autoRange=False)
|
|
|
|
|
|
|
|
ptr += 1
|
|
|
|
now = ptime.time()
|
|
|
|
dt = now - lastTime
|
|
|
|
lastTime = now
|
|
|
|
if fps is None:
|
|
|
|
fps = 1.0/dt
|
|
|
|
else:
|
|
|
|
s = np.clip(dt*3., 0, 1)
|
|
|
|
fps = fps * (1-s) + (1.0/dt) * s
|
|
|
|
ui.fpsLabel.setText('%0.2f fps' % fps)
|
|
|
|
app.processEvents() ## force complete redraw for every plot
|
|
|
|
timer = QtCore.QTimer()
|
|
|
|
timer.timeout.connect(update)
|
|
|
|
timer.start(0)
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-04-04 16:22:43 +00:00
|
|
|
## Start Qt event loop unless running in interactive mode or using pyside.
|
2012-12-05 05:25:45 +00:00
|
|
|
if __name__ == '__main__':
|
|
|
|
import sys
|
|
|
|
if (sys.flags.interactive != 1) or not hasattr(QtCore, 'PYQT_VERSION'):
|
|
|
|
QtGui.QApplication.instance().exec_()
|