55"""
66import numpy as np
77from scipy .ndimage import label , generate_binary_structure
8-
98from imagepy import IPy
10- from imagepy .core .engine import Filter
11- from imagepy .ui .canvasframe import CanvasFrame
9+ from imagepy .core import ImagePlus
10+ from imagepy .core .engine import Filter , Simple
11+ from imagepy .ipyalg .graph import connect , render
1212
13- class Plugin ( Filter ):
13+ class Label ( Simple ):
1414 title = 'Label Image'
15- note = ['8-bit' , 'not_slice' , 'preview' ]
16-
17- para = {'thr' :128 , 'con' :'4-Connect' }
18- view = [('slide' , 'thr' , (0 ,255 ), 0 , 'Threshold' ),
19- (list , 'con' , ['4-Connect' ,'8-Connect' ], str , 'Structure' , 'connect' )]
20-
21- def load (self , ips ):
22- self .lut = ips .lut
23- ips .lut = self .lut .copy ()
24- return True
25-
26- def preview (self , ips , para ):
27- ips .lut [:] = self .lut
28- ips .lut [para ['thr' ]:] = [255 ,0 ,0 ]
29- ips .update ()
30-
31- def run (self , ips , snap , img , para = None ):
32- if para == None : para = self .para
33- ips .lut = self .lut
34- msk = img > para ['thr' ]
35- con = 1 if para ['con' ]== '4-Connect' else 2
36- strc = generate_binary_structure (2 , con )
37- msk = label (msk , strc , output = np .int16 )
15+ note = ['8-bit' , '16-bit' ]
16+ para = {'slice' :False , 'con' :'4-Connect' }
17+ view = [(list , 'con' , ['4-Connect' ,'8-Connect' ], str , 'Structure' , 'connect' ),
18+ (bool , 'slice' , 'slice' )]
3819
39- IPy .show_img ([msk [0 ]], ips .title + '-label' )
20+ def run (self , ips , imgs , para = None ):
21+ if not para ['slice' ]: imgs = [ips .img ]
22+ labels = []
23+ for i in range (len (imgs )):
24+ self .progress (i , len (imgs ))
25+ con = 1 if para ['con' ]== '4-Connect' else 2
26+ strc = generate_binary_structure (2 , con )
27+ lab , n = label (imgs [i ], strc , output = np .int32 )
28+ labels .append (lab )
29+ IPy .show_img (labels , ips .title + '-label' )
4030
31+ class Render (Simple ):
32+ title = 'Label Render'
33+ note = ['8-bit' , '16-bit' , 'int' ]
34+ para = {'slice' :False , 'con' :'8-Connect' , 'colors' :6 , 'back' :False }
35+ view = [(list , 'con' , ['4-Connect' ,'8-Connect' ], str , 'Structure' , 'connect' ),
36+ (int , 'colors' , (4 , 255 ), 0 , 'colors' , 'n' ),
37+ (bool , 'back' , 'background' ),
38+ (bool , 'slice' , 'slice' )]
4139
42-
40+ def run (self , ips , imgs , para = None ):
41+ if not para ['slice' ]: imgs = [ips .img ]
42+ print (para )
43+ labels = []
44+ for i in range (len (imgs )):
45+ self .progress (i , len (imgs ))
46+ con = 1 if para ['con' ]== '4-Connect' else 2
47+ idx = connect .connect_graph (imgs [i ], con , para ['back' ])
48+ idx = connect .mapidx (idx )
49+ cmap = render .node_render (idx , para ['colors' ], 10 )
50+
51+ lut = np .ones (imgs [i ].max ()+ 1 , dtype = np .uint8 )
52+ lut [0 ] = 0
53+ for j in cmap : lut [j ] = cmap [j ]
54+ labels .append (lut [imgs [i ]])
55+
56+ ips = ImagePlus (labels , ips .title + '-render' )
57+ ips .range = (0 , para ['colors' ])
58+ IPy .show_ips (ips )
59+
60+ plgs = [Label , Render ]
0 commit comments