1+ from imagepy import IPy
2+ import numpy as np
3+ from imagepy .core .engine import Simple
4+ from skimage .measure import regionprops
5+ from imagepy .core .mark import GeometryMark
6+ from scipy .ndimage import label , generate_binary_structure
7+ from imagepy .ipyalg .graph .connect import connect , mapidx
8+ import pandas as pd
9+
10+ # center, area, l, extent, cov
11+ class Plugin (Simple ):
12+ title = 'Connective Analysis'
13+ note = ['8-bit' , '16-bit' , 'int' ]
14+ para = {'con' :'8-connect' , 'labled' :False , 'slice' :False }
15+ view = [(list , 'con' , ['4-connect' , '8-connect' ], str , 'conection' , 'pix' ),
16+ (bool , 'labled' , 'it is a label image' ),
17+ (bool , 'slice' , 'slice' )]
18+
19+ #process
20+ def run (self , ips , imgs , para = None ):
21+ if not para ['slice' ]:imgs = [ips .img ]
22+ k = ips .unit [0 ]
23+
24+ titles = ['Slice' , 'ID' ][0 if para ['slice' ] else 1 :] + ['Center-X' ,'Center-Y' , 'N' , 'Neighbors' ]
25+ buf = imgs [0 ].astype (np .uint32 )
26+ data , mark = [], {'type' :'layers' , 'body' :{}}
27+ for i in range (len (imgs )):
28+ if para ['labled' ]: buf = imgs [i ]
29+ else : label (imgs [i ], generate_binary_structure (2 , 1 ), output = buf )
30+ conarr = connect (buf , 1 if para ['con' ]== '4-connect' else 2 )
31+ conmap = mapidx (conarr )
32+
33+ ls = regionprops (buf )
34+ dt = [[i ]* len (ls ), list (range (1 ,1 + len (ls )))]
35+
36+ if not para ['slice' ]:dt = dt [1 :]
37+
38+ layer = {'type' :'layer' , 'body' :[]}
39+ texts = [(i .centroid [::- 1 ])+ ('id=%d' % i .label ,) for i in ls ]
40+ lines = [(ls [i - 1 ].centroid [::- 1 ], ls [j - 1 ].centroid [::- 1 ]) for i ,j in conarr ]
41+ layer ['body' ].append ({'type' :'texts' , 'body' :texts })
42+ layer ['body' ].append ({'type' :'lines' , 'body' :lines })
43+ mark ['body' ][i ] = layer
44+
45+ dt .append ([round (i .centroid [1 ]* k ,1 ) for i in ls ])
46+ dt .append ([round (i .centroid [0 ]* k ,1 ) for i in ls ])
47+ neibs = [conmap [i .label ] if i .label in conmap else [] for i in ls ]
48+ dt .extend ([[len (i ) for i in neibs ], neibs ])
49+ data .extend (list (zip (* dt )))
50+ ips .mark = GeometryMark (mark )
51+ IPy .show_table (pd .DataFrame (data , columns = titles ), ips .title + '-region' )
0 commit comments