~gcg/+junk/trunk

« back to all changes in this revision

Viewing changes to DVC/overlay.py

  • Committer: gcg
  • Date: 2020-07-15 07:46:15 UTC
  • Revision ID: gcg-20200715074615-tr80szbt9xq5m2ae
Major Cleanup

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# -*- coding: utf-8 -*-
 
2
"""
 
3
Created on Fri Jan 25 19:56:48 2019
 
4
 
 
5
@author: gcg
 
6
"""
 
7
 
 
8
import pyqtgraph as pg
 
9
from pyqtgraph.Qt import QtCore, QtGui
 
10
import numpy as np
 
11
import sys, copy
 
12
from matplotlib import cm
 
13
from numpy import ma
 
14
 
 
15
# Get the colormap
 
16
colormap = cm.get_cmap("nipy_spectral")  # cm.get_cmap("CMRmap")
 
17
colormap._init()
 
18
lut = (colormap._lut * 255).view(np.ndarray)  # Convert matplotlib colormap from 0-1 to 0 -255 for Qt
 
19
 
 
20
#pg.setConfigOptions(imageAxisOrder='row-major')
 
21
 
 
22
## Create CT image to display
 
23
nx, ny = 100, 50
 
24
structure = np.ones((nx, ny), dtype=float)
 
25
structure += np.random.normal(size=(nx,ny))
 
26
 
 
27
# create field image to overlay
 
28
field = np.ones((nx, ny)) * range(0, ny)
 
29
 
 
30
## create GUI
 
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)
 
36
 
 
37
# this is the grey level CT image shwoing the structure
 
38
img1a = pg.ImageItem(structure)
 
39
v1a.addItem(img1a)
 
40
v1a.disableAutoRange('xy')
 
41
v1a.autoRange()
 
42
 
 
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
 
46
img2.setOpacity(0.6)
 
47
img2.setLookupTable(lut)
 
48
v1a.addItem(img2)
 
49
 
 
50
rois = []
 
51
rois.append(pg.PolyLineROI([[80, 60], [90, 30], [60, 40]], pen=(6,9), closed=True))
 
52
 
 
53
def show_exception_and_exit(exc_type, exc_value, tb):
 
54
    import traceback
 
55
    traceback.print_exception(exc_type, exc_value, tb)
 
56
    raw_input("Press key to exit.")
 
57
    sys.exit(-1)
 
58
sys.excepthook = show_exception_and_exit
 
59
 
 
60
# generate index map
 
61
def generate_idxmap():
 
62
    idxmap = np.zeros((nx, ny, 2), dtype=int)
 
63
    for iy in xrange(ny):
 
64
        for ix in xrange(nx):
 
65
            idxmap[ix, iy, 0] = ix
 
66
            idxmap[ix, iy, 1] = iy
 
67
    return idxmap
 
68
 
 
69
def update(roi):
 
70
    pass
 
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)
 
74
    
 
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
 
80
    
 
81
    sx, sy, d = np.shape(rval)
 
82
    for ix in xrange(sx):
 
83
        for iy in xrange(sy):
 
84
            cx = rval[ix, iy, 0]
 
85
            cy = rval[ix, iy, 1]
 
86
            if cx and cy:
 
87
                icx = int(cx)
 
88
                icy = int(cy)
 
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
 
93
 
 
94
idxmap = generate_idxmap()
 
95
    
 
96
for roi in rois:
 
97
    roi.sigRegionChanged.connect(update)
 
98
    v1a.addItem(roi)
 
99
 
 
100
update(rois[-1])
 
101
    
 
102
 
 
103
 
 
104
 
 
105
## Start Qt event loop unless running in interactive mode or using pyside.
 
106
if __name__ == '__main__':
 
107
    import sys
 
108
    if (sys.flags.interactive != 1) or not hasattr(QtCore, 'PYQT_VERSION'):
 
109
        QtGui.QApplication.instance().exec_()
 
 
b'\\ No newline at end of file'