2010-03-22 05:48:52 +00:00
|
|
|
#!/usr/bin/python -i
|
|
|
|
# -*- coding: utf-8 -*-
|
2010-03-22 06:21:56 +00:00
|
|
|
## Add path to library (just for examples; you do not need this)
|
2012-12-26 22:51:52 +00:00
|
|
|
import initExample
|
2010-03-22 06:21:56 +00:00
|
|
|
|
2011-04-05 14:35:50 +00:00
|
|
|
|
2012-03-02 03:58:02 +00:00
|
|
|
from pyqtgraph.Qt import QtCore, QtGui
|
2011-04-05 14:35:50 +00:00
|
|
|
import numpy as np
|
|
|
|
import pyqtgraph as pg
|
2010-03-22 05:48:52 +00:00
|
|
|
|
2016-04-28 05:33:51 +00:00
|
|
|
pg.setConfigOptions(imageAxisOrder='normal')
|
|
|
|
|
2011-04-05 14:35:50 +00:00
|
|
|
## create GUI
|
2010-03-22 06:21:56 +00:00
|
|
|
app = QtGui.QApplication([])
|
2012-03-02 02:55:32 +00:00
|
|
|
|
|
|
|
w = pg.GraphicsWindow(size=(800,800), border=True)
|
|
|
|
v = w.addViewBox(colspan=2)
|
|
|
|
v.invertY(True) ## Images usually have their Y-axis pointing downward
|
2010-03-22 05:48:52 +00:00
|
|
|
v.setAspectLocked(True)
|
2012-03-02 02:55:32 +00:00
|
|
|
|
2010-03-22 05:48:52 +00:00
|
|
|
|
2011-04-05 14:35:50 +00:00
|
|
|
## Create image to display
|
|
|
|
arr = np.ones((100, 100), dtype=float)
|
2010-03-22 05:48:52 +00:00
|
|
|
arr[45:55, 45:55] = 0
|
|
|
|
arr[25, :] = 5
|
|
|
|
arr[:, 25] = 5
|
|
|
|
arr[75, :] = 5
|
|
|
|
arr[:, 75] = 5
|
|
|
|
arr[50, :] = 10
|
|
|
|
arr[:, 50] = 10
|
|
|
|
|
2016-04-28 05:33:51 +00:00
|
|
|
# add an arrow for asymmetry
|
|
|
|
arr[10, :50] = 10
|
|
|
|
arr[9:12, 44:48] = 10
|
|
|
|
arr[8:13, 44:46] = 10
|
|
|
|
|
2011-04-05 14:35:50 +00:00
|
|
|
## Create image items, add to scene and set position
|
|
|
|
im1 = pg.ImageItem(arr)
|
|
|
|
im2 = pg.ImageItem(arr)
|
2012-03-02 02:55:32 +00:00
|
|
|
v.addItem(im1)
|
|
|
|
v.addItem(im2)
|
2010-03-22 05:48:52 +00:00
|
|
|
im2.moveBy(110, 20)
|
2012-03-02 02:55:32 +00:00
|
|
|
v.setRange(QtCore.QRectF(0, 0, 200, 120))
|
2016-04-28 05:33:51 +00:00
|
|
|
im1.scale(0.8, 0.5)
|
2012-03-02 02:55:32 +00:00
|
|
|
|
2011-04-05 14:35:50 +00:00
|
|
|
im3 = pg.ImageItem()
|
2012-03-02 02:55:32 +00:00
|
|
|
v2 = w.addViewBox(1,0)
|
|
|
|
v2.addItem(im3)
|
|
|
|
v2.setRange(QtCore.QRectF(0, 0, 60, 60))
|
|
|
|
v2.invertY(True)
|
|
|
|
v2.setAspectLocked(True)
|
|
|
|
#im3.moveBy(0, 130)
|
2010-03-22 05:48:52 +00:00
|
|
|
im3.setZValue(10)
|
2012-03-02 02:55:32 +00:00
|
|
|
|
2011-04-05 14:35:50 +00:00
|
|
|
im4 = pg.ImageItem()
|
2012-03-02 02:55:32 +00:00
|
|
|
v3 = w.addViewBox(1,1)
|
|
|
|
v3.addItem(im4)
|
|
|
|
v3.setRange(QtCore.QRectF(0, 0, 60, 60))
|
|
|
|
v3.invertY(True)
|
|
|
|
v3.setAspectLocked(True)
|
|
|
|
#im4.moveBy(110, 130)
|
2010-03-22 05:48:52 +00:00
|
|
|
im4.setZValue(10)
|
|
|
|
|
2011-04-05 14:35:50 +00:00
|
|
|
## create the plot
|
2012-03-02 02:55:32 +00:00
|
|
|
pi1 = w.addPlot(2,0, colspan=2)
|
|
|
|
#pi1 = pg.PlotItem()
|
|
|
|
#s.addItem(pi1)
|
|
|
|
#pi1.scale(0.5, 0.5)
|
|
|
|
#pi1.setGeometry(0, 170, 300, 100)
|
2010-07-25 04:33:26 +00:00
|
|
|
|
|
|
|
lastRoi = None
|
|
|
|
|
|
|
|
def updateRoi(roi):
|
|
|
|
global im1, im2, im3, im4, arr, lastRoi
|
|
|
|
if roi is None:
|
|
|
|
return
|
|
|
|
lastRoi = roi
|
|
|
|
arr1 = roi.getArrayRegion(im1.image, img=im1)
|
2012-03-02 02:55:32 +00:00
|
|
|
im3.setImage(arr1)
|
2010-07-25 04:33:26 +00:00
|
|
|
arr2 = roi.getArrayRegion(im2.image, img=im2)
|
2012-03-02 02:55:32 +00:00
|
|
|
im4.setImage(arr2)
|
2010-07-25 04:33:26 +00:00
|
|
|
updateRoiPlot(roi, arr1)
|
|
|
|
|
|
|
|
def updateRoiPlot(roi, data=None):
|
|
|
|
if data is None:
|
|
|
|
data = roi.getArrayRegion(im1.image, img=im1)
|
|
|
|
if data is not None:
|
2012-03-02 02:55:32 +00:00
|
|
|
roi.curve.setData(data.mean(axis=1))
|
2010-07-25 04:33:26 +00:00
|
|
|
|
|
|
|
|
2011-04-05 14:35:50 +00:00
|
|
|
## Create a variety of different ROI types
|
2010-07-25 04:33:26 +00:00
|
|
|
rois = []
|
2012-03-02 02:55:32 +00:00
|
|
|
rois.append(pg.TestROI([0, 0], [20, 20], maxBounds=QtCore.QRectF(-10, -10, 230, 140), pen=(0,9)))
|
|
|
|
rois.append(pg.LineROI([0, 0], [20, 20], width=5, pen=(1,9)))
|
|
|
|
rois.append(pg.MultiLineROI([[0, 50], [50, 60], [60, 30]], width=5, pen=(2,9)))
|
|
|
|
rois.append(pg.EllipseROI([110, 10], [30, 20], pen=(3,9)))
|
|
|
|
rois.append(pg.CircleROI([110, 50], [20, 20], pen=(4,9)))
|
|
|
|
rois.append(pg.PolygonROI([[2,0], [2.1,0], [2,.1]], pen=(5,9)))
|
2011-02-08 00:40:38 +00:00
|
|
|
#rois.append(SpiralROI([20,30], [1,1], pen=mkPen(0)))
|
2011-04-05 14:35:50 +00:00
|
|
|
|
|
|
|
## Add each ROI to the scene and link its data to a plot curve with the same color
|
2010-07-25 04:33:26 +00:00
|
|
|
for r in rois:
|
2012-03-02 02:55:32 +00:00
|
|
|
v.addItem(r)
|
2010-07-25 04:33:26 +00:00
|
|
|
c = pi1.plot(pen=r.pen)
|
|
|
|
r.curve = c
|
2011-04-05 14:35:50 +00:00
|
|
|
r.sigRegionChanged.connect(updateRoi)
|
2010-07-25 04:33:26 +00:00
|
|
|
|
|
|
|
def updateImage():
|
|
|
|
global im1, arr, lastRoi
|
2011-04-05 14:35:50 +00:00
|
|
|
r = abs(np.random.normal(loc=0, scale=(arr.max()-arr.min())*0.1, size=arr.shape))
|
2010-07-25 04:33:26 +00:00
|
|
|
im1.updateImage(arr + r)
|
|
|
|
updateRoi(lastRoi)
|
|
|
|
for r in rois:
|
|
|
|
updateRoiPlot(r)
|
|
|
|
|
2011-04-05 14:35:50 +00:00
|
|
|
## Rapidly update one of the images with random noise
|
2010-07-25 04:33:26 +00:00
|
|
|
t = QtCore.QTimer()
|
2011-04-05 14:35:50 +00:00
|
|
|
t.timeout.connect(updateImage)
|
2010-07-25 04:33:26 +00:00
|
|
|
t.start(50)
|
2010-03-22 05:48:52 +00:00
|
|
|
|
2011-04-05 14:35:50 +00:00
|
|
|
|
|
|
|
|
|
|
|
## Start Qt event loop unless running in interactive mode.
|
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_()
|