Skip to content

Commit 592365d

Browse files
committed
nothing
1 parent 0880590 commit 592365d

File tree

12 files changed

+192
-104
lines changed

12 files changed

+192
-104
lines changed

imagepy/app/imagepy.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -293,7 +293,8 @@ def on_active_mesh(self, event):
293293

294294
def on_close_mesh(self, event):
295295
# canvas3d = event.GetEventObject().GetPage(event.GetSelection())
296-
self.close_mesh(self.meshnb.canvas().scene3d.name)
296+
App.close_mesh(self, self.meshnb.canvas().scene3d.name)
297+
event.Skip()
297298
# self.remove_mesh_win(canvas3d)
298299

299300
def info(self, value):

imagepy/ipyalg/graph/sknw.py

Lines changed: 79 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -35,109 +35,148 @@ def idx2rc(idx, acc):
3535

3636
@jit(nopython=True) # fill a node (may be two or more points)
3737
def fill(img, p, num, nbs, acc, buf):
38-
back = img[p]
3938
img[p] = num
4039
buf[0] = p
41-
cur = 0; s = 1;
40+
cur = 0; s = 1; iso = True;
4241

4342
while True:
4443
p = buf[cur]
4544
for dp in nbs:
4645
cp = p+dp
47-
if img[cp]==back:
46+
if img[cp]==2:
4847
img[cp] = num
4948
buf[s] = cp
5049
s+=1
50+
if img[cp]==1: iso=False
5151
cur += 1
5252
if cur==s:break
53-
return idx2rc(buf[:s], acc)
53+
return iso, idx2rc(buf[:s], acc)
5454

5555
@jit(nopython=True) # trace the edge and use a buffer, then buf.copy, if use [] numba not works
5656
def trace(img, p, nbs, acc, buf):
5757
c1 = 0; c2 = 0;
5858
newp = 0
59-
cur = 0
60-
59+
cur = 1
6160
while True:
6261
buf[cur] = p
6362
img[p] = 0
6463
cur += 1
6564
for dp in nbs:
6665
cp = p + dp
6766
if img[cp] >= 10:
68-
if c1==0:c1=img[cp]
69-
else: c2 = img[cp]
67+
if c1==0:
68+
c1 = img[cp]
69+
buf[0] = cp
70+
else:
71+
c2 = img[cp]
72+
buf[cur] = cp
7073
if img[cp] == 1:
7174
newp = cp
7275
p = newp
7376
if c2!=0:break
74-
return (c1-10, c2-10, idx2rc(buf[:cur], acc))
77+
return (c1-10, c2-10, idx2rc(buf[:cur+1], acc))
7578

7679
@jit(nopython=True) # parse the image then get the nodes and edges
77-
def parse_struc(img, pts, nbs, acc):
80+
def parse_struc(img, nbs, acc, iso, ring):
7881
img = img.ravel()
7982
buf = np.zeros(131072, dtype=np.int64)
8083
num = 10
8184
nodes = []
82-
for p in pts:
85+
for p in range(len(img)):
8386
if img[p] == 2:
84-
nds = fill(img, p, num, nbs, acc, buf)
87+
isiso, nds = fill(img, p, num, nbs, acc, buf)
88+
if isiso and not iso: continue
8589
num += 1
8690
nodes.append(nds)
87-
8891
edges = []
89-
for p in pts:
92+
for p in range(len(img)):
93+
if img[p] <10: continue
94+
for dp in nbs:
95+
if img[p+dp]==1:
96+
edge = trace(img, p+dp, nbs, acc, buf)
97+
edges.append(edge)
98+
if not ring: return nodes, edges
99+
for p in range(len(img)):
100+
if img[p]!=1: continue
101+
img[p] = num; num += 1
102+
nodes.append(idx2rc([p], acc))
90103
for dp in nbs:
91104
if img[p+dp]==1:
92105
edge = trace(img, p+dp, nbs, acc, buf)
93106
edges.append(edge)
94107
return nodes, edges
95108

96109
# use nodes and edges build a networkx graph
97-
def build_graph(nodes, edges, multi=False):
110+
def build_graph(nodes, edges, multi=False, full=True):
111+
os = np.array([i.mean(axis=0) for i in nodes])
112+
if full: os = os.round().astype(np.uint16)
98113
graph = nx.MultiGraph() if multi else nx.Graph()
99114
for i in range(len(nodes)):
100-
graph.add_node(i, pts=nodes[i], o=nodes[i].mean(axis=0))
115+
graph.add_node(i, pts=nodes[i], o=os[i])
101116
for s,e,pts in edges:
117+
if full: pts[[0,-1]] = os[[s,e]]
102118
l = np.linalg.norm(pts[1:]-pts[:-1], axis=1).sum()
103119
graph.add_edge(s,e, pts=pts, weight=l)
104120
return graph
105121

106-
def buffer(ske):
107-
buf = np.zeros(tuple(np.array(ske.shape)+2), dtype=np.uint16)
108-
buf[tuple([slice(1,-1)]*buf.ndim)] = ske
122+
def mark_node(ske):
123+
buf = np.pad(ske, (1,1), mode='constant')
124+
nbs = neighbors(buf.shape)
125+
acc = np.cumprod((1,)+buf.shape[::-1][:-1])[::-1]
126+
mark(buf, nbs)
109127
return buf
110-
111-
def build_sknw(ske, multi=False):
112-
buf = buffer(ske)
128+
129+
def build_sknw(ske, multi=False, iso=True, ring=True, full=True):
130+
buf = np.pad(ske, (1,1), mode='constant')
113131
nbs = neighbors(buf.shape)
114132
acc = np.cumprod((1,)+buf.shape[::-1][:-1])[::-1]
115133
mark(buf, nbs)
116-
pts = np.array(np.where(buf.ravel()==2))[0]
117-
nodes, edges = parse_struc(buf, pts, nbs, acc)
118-
return build_graph(nodes, edges, multi)
134+
nodes, edges = parse_struc(buf, nbs, acc, iso, ring)
135+
return build_graph(nodes, edges, multi, full)
119136

120137
# draw the graph
121138
def draw_graph(img, graph, cn=255, ce=128):
122139
acc = np.cumprod((1,)+img.shape[::-1][:-1])[::-1]
123140
img = img.ravel()
141+
for (s, e) in graph.edges():
142+
eds = graph[s][e]
143+
if isinstance(graph, nx.MultiGraph):
144+
for i in eds:
145+
pts = eds[i]['pts']
146+
img[np.dot(pts, acc)] = ce
147+
else: img[np.dot(eds['pts'], acc)] = ce
124148
for idx in graph.nodes():
125149
pts = graph.nodes[idx]['pts']
126150
img[np.dot(pts, acc)] = cn
127-
for (s, e) in graph.edges():
128-
eds = graph[s][e]
129-
for i in eds:
130-
pts = eds[i]['pts']
131-
img[np.dot(pts, acc)] = ce
132151

133152
if __name__ == '__main__':
134-
g = nx.MultiGraph()
135-
g.add_nodes_from([1,2,3,4,5])
136-
g.add_edges_from([(1,2),(1,3),(2,3),(4,5),(5,4)])
137-
print(g.nodes())
138-
print(g.edges())
139-
a = g.subgraph(1)
140-
print('d')
141-
print(a)
142-
print('d')
143-
153+
import matplotlib.pyplot as plt
154+
155+
img = np.array([
156+
[0,0,0,1,0,0,0,0,0],
157+
[0,0,0,1,0,0,0,1,0],
158+
[0,0,0,1,0,0,0,0,0],
159+
[1,1,1,1,0,0,0,0,0],
160+
[0,0,0,0,1,0,0,0,0],
161+
[0,1,0,0,0,1,0,0,0],
162+
[1,0,1,0,0,1,1,1,1],
163+
[0,1,0,0,1,0,0,0,0],
164+
[0,0,0,1,0,0,0,0,0]])
165+
166+
node_img = mark_node(img)
167+
graph = build_sknw(img, False, iso=True, ring=True)
168+
plt.imshow(node_img[1:-1,1:-1], cmap='gray')
169+
170+
# draw edges by pts
171+
for (s,e) in graph.edges():
172+
ps = graph[s][e]['pts']
173+
plt.plot(ps[:,1], ps[:,0], 'green')
174+
175+
# draw node by o
176+
nodes = graph.nodes()
177+
ps = np.array([nodes[i]['o'] for i in nodes])
178+
plt.plot(ps[:,1], ps[:,0], 'r.')
179+
180+
# title and show
181+
plt.title('Build Graph')
182+
plt.show()

imagepy/menus/Process/Hydrology/hydrology_plgs.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,7 @@ def run(self, ips, snap, img, para = None):
217217
if not para['ud']:img[:] = 255-img
218218
mark = watershed(img, markers, line=True, conn=para['con']+1)
219219
mark = np.multiply((mark==0), 255, dtype=np.uint8)
220-
220+
221221
if para['type'] == 'white line':
222222
img[:] = mark
223223
if para['type'] == 'gray line':

sciapp/app.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ def show_table(self, tab, name):
6262
if not isinstance(tab, Table):
6363
tab = Table(tab, name)
6464
tab.name = self.tab_manager.name(name)
65-
self.tab_manager.add(name, tab)
65+
self.tab_manager.add(tab.name, tab)
6666
print(tab.info)
6767

6868
def close_table(self, name):

sciapp/object/image.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,14 +46,15 @@ class Image:
4646
def __init__(self, imgs=None, name='Image'):
4747
self.name = name
4848
self.cur = 0
49+
self.rg = [(0, 255)]
4950
self.set_imgs(imgs)
5051
self.roi = None
5152
self.mark = None
5253
self.unit = 1, 'pix'
5354
self.msk = None
5455
self.pos = (0,0)
5556
self.cn = 0
56-
self.rg = [(0,255)]
57+
5758
self.lut = default_lut
5859
self.log = False
5960
self.mode = 'set'

sciapp/object/surface.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ def __init__(self, verts=None, faces=None, colors=None, cmap=None, **key):
7878
self.faces = faces.astype(np.uint32, copy=False) if not faces is None else None
7979
self.colors = colors
8080
self.mode, self.visible, self.dirty = 'mesh', True, 'geom'
81-
self.alpha = 1; self.edges = None
81+
self.alpha = 1; self.edges = None; self.shiness = 60
8282
self.high_light = False; self.cmap = 'gray' if cmap is None else cmap
8383
self.set_data(**key)
8484

@@ -98,6 +98,7 @@ def set_data(self, verts=None, faces=None, colors=None, **key):
9898
self.visible = key.get('visible', self.visible)
9999
self.alpha = key.get('alpha', self.alpha)
100100
self.high_light = key.get('high_light', False)
101+
self.shiness = key.get('shiness', self.shiness)
101102
self.cmap = key.get('cmap', self.cmap)
102103
self.dirty = self.dirty or True
103104

@@ -116,7 +117,7 @@ def __init__(self, texts=None, verts=None, colors=(1,1,1), size=12, **key):
116117
self.texts, self.verts, self.size, self.colors = texts, verts, size, colors
117118
self.visible, self.dirty = True, 'geom'
118119
self.alpha = 1; self.edges = None
119-
self.high_light = False;
120+
self.high_light = False; self.shiness = 0
120121
self.set_data(**key)
121122

122123
def set_data(self, texts=None, verts=None, colors=None, size=None, **key):
@@ -128,6 +129,7 @@ def set_data(self, texts=None, verts=None, colors=None, size=None, **key):
128129
self.visible = key.get('visible', self.visible)
129130
self.alpha = key.get('alpha', self.alpha)
130131
self.high_light = key.get('high_light', False)
132+
self.shiness = key.get('shiness', self.shiness)
131133
self.dirty = self.dirty or True
132134

133135
class Surface2d(Mesh):
@@ -171,7 +173,7 @@ def __init__(self, imgs=None, level=0.25, sample=1, step=1, cmap=None, **key):
171173
self.imgs, self.level, self.sample, self.step = imgs, level, sample, step
172174
self.visible, self.dirty = True, 'geom'
173175
self.cmap = 'gray' if cmap is None else cmap
174-
self.alpha = 1
176+
self.alpha = 1; self.shiness = 0
175177
self.high_light = False;
176178
self.set_data(**key)
177179

@@ -184,5 +186,6 @@ def set_data(self, imgs=None, level=None, step=None, **key):
184186
self.visible = key.get('visible', self.visible)
185187
self.alpha = key.get('alpha', self.alpha)
186188
self.high_light = key.get('high_light', False)
189+
self.shiness = key.get('shiness', self.shiness)
187190
self.cmap = key.get('cmap', self.cmap)
188191
self.dirty = self.dirty or True

sciwx/demo/mesh1_demo.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,12 @@
1010
verts, faces = meshutil.create_ball((0,0,0), 1)
1111
ball = Mesh(verts=verts, faces=faces, colors=verts[:,2], cmap='jet')
1212
ball2 = Mesh(verts=verts, faces=faces, colors=verts[:,2], mode='grid')
13+
ball3 = Mesh(verts=verts, faces=faces, colors=verts[:,2], mode='grid')
1314

1415
def canvas3d_test():
1516
frame = wx.Frame(None, title='Canvas3D')
16-
canvas3d = Canvas3D(frame)
17+
canvas3d = Canvas3D(parent=frame)
18+
canvas3d.SetBackgroundColour((255,0,0))
1719
canvas3d.add_obj('ball', ball)
1820
frame.Show()
1921

@@ -35,17 +37,21 @@ def canvas3d_note_book():
3537
canvas1.add_obj('ball', ball)
3638
canvas2 = cnb.add_canvas()
3739
canvas2.add_obj('ball2', ball2)
40+
canvas3 = cnb.add_canvas()
41+
canvas3.add_obj('ball3', ball3)
3842
frame.Show()
3943

4044
def canvas3d_note_frame():
4145
cnf = Canvas3DNoteFrame(None)
4246
canvas1 = cnf.add_canvas()
43-
canvas1.add_obj('ball', ball)
47+
canvas1.add_obj('ball1', ball)
48+
canvas2 = cnf.add_canvas()
49+
canvas2.add_obj('ball2', ball2)
4450
canvas2 = cnf.add_canvas()
45-
canvas2.add_obj('ball', ball2)
51+
canvas2.add_obj('ball3', ball3)
4652
cnf.Show()
4753

4854
if __name__ == '__main__':
4955
app = wx.App()
50-
canvas3d_note_frame()
56+
canvas3d_note_book()
5157
app.MainLoop()

0 commit comments

Comments
 (0)