1
# -*- coding: utf-8 -*-
3
Created on Fri Jan 25 19:56:48 2019
9
from pyqtgraph.Qt import QtCore, QtGui
12
from matplotlib import cm
16
colormap = cm.get_cmap("nipy_spectral") # cm.get_cmap("CMRmap")
18
lut = (colormap._lut * 255).view(np.ndarray) # Convert matplotlib colormap from 0-1 to 0 -255 for Qt
20
#pg.setConfigOptions(imageAxisOrder='row-major')
22
## Create CT image to display
24
structure = np.ones((nx, ny), dtype=float)
25
structure += np.random.normal(size=(nx,ny))
27
# create field image to overlay
28
field = np.ones((nx, ny)) * range(0, ny)
31
app = QtGui.QApplication([])
32
w = pg.GraphicsWindow(size=(1000,800), border=True)
33
w.setWindowTitle('pyqtgraph example: ROI Examples')
34
w1 = w.addLayout(row=0, col=0)
35
v1a = w1.addViewBox(row=1, col=0, lockAspect=True)
37
# this is the grey level CT image shwoing the structure
38
img1a = pg.ImageItem(structure)
40
v1a.disableAutoRange('xy')
43
# this is the image that holds the transparent overlay
44
img2 = pg.ImageItem(pg.np.random.normal(size=(nx,ny)))
45
img2.setZValue(10) # make sure this image is on top
47
img2.setLookupTable(lut)
51
rois.append(pg.PolyLineROI([[80, 60], [90, 30], [60, 40]], pen=(6,9), closed=True))
53
def show_exception_and_exit(exc_type, exc_value, tb):
55
traceback.print_exception(exc_type, exc_value, tb)
56
raw_input("Press key to exit.")
58
sys.excepthook = show_exception_and_exit
61
def generate_idxmap():
62
idxmap = np.zeros((nx, ny, 2), dtype=int)
65
idxmap[ix, iy, 0] = ix
66
idxmap[ix, iy, 1] = iy
71
#print roi.pos(), roi.boundingRect()
72
rval = roi.getArrayRegion(idxmap, img1a, returnMappedCoords=False).astype(int)
73
print "shape of rval is", np.shape(rval)
75
# create an image to overlay on top of structure.
76
# the overlay image is a copy of the underlying image, except within the selected region
77
overlay_img = np.zeros((nx, ny))
78
overlay_img = overlay_img.view(ma.MaskedArray)
79
overlay_img.mask = True
81
sx, sy, d = np.shape(rval)
89
overlay_img[icx, icy] = field[icx, icy]
90
print "local bounds in ROI: ", overlay_img.min(), overlay_img.mean(), overlay_img.max()
91
img2.setImage(overlay_img, levels=(field.min(), field.max())) # global min/max on colormap
92
img2.setImage(overlay_img, levels=(overlay_img.min(), overlay_img.max())) # local min/max on colormap
94
idxmap = generate_idxmap()
97
roi.sigRegionChanged.connect(update)
105
## Start Qt event loop unless running in interactive mode or using pyside.
106
if __name__ == '__main__':
108
if (sys.flags.interactive != 1) or not hasattr(QtCore, 'PYQT_VERSION'):
109
QtGui.QApplication.instance().exec_()
b'\\ No newline at end of file'