11import math
22from ij import IJ
33from ij .measure import ResultsTable
4- from inra .ijpb .label .LabelImages import keepLabels
4+ from inra .ijpb .label .LabelImages import keepLabels , findAllLabels , remapLabels
5+ from inra .ijpb .measure .region3d import Centroid3D
56
6- up_to = 2
7- bottom_first = True
7+ # How many slices to remove (starting from the most populated ones)
8+ up_to = 3
9+
10+ # ------------------------------------------------------------------
11+
12+ img = IJ .getImage ()
13+ calib = img .getCalibration ()
14+ c3d = Centroid3D ()
815
916# Creating the histogram of spots repartition on the Z axis
10- rt = ResultsTable .getActiveTable ()
11- vals = rt .getColumn ("Centroid.Z" )
12- histo = [0 for _ in range (1 + int (math .ceil (max (vals ))))]
17+ d_clb = calib .copy ()
18+ d_clb .pixelWidth = 1
19+ d_clb .pixelHeight = 1
20+ d_clb .pixelDepth = 1
21+ d_clb .setUnit ("pixel" )
22+
23+ all_lbls = findAllLabels (img )
24+ pts = c3d .analyzeRegions (img .getStack (), all_lbls , d_clb )
25+ vals = [int (round (p .getZ ())) for p in pts ]
1326
27+ histo = [0 for _ in range (1 + int (math .ceil (max (vals ))))]
1428for val in vals :
1529 histo [int (round (val ))] += 1
1630
1731# Sorting slices by the number of spots they contain
1832sliced = [(v , i ) for i , v in enumerate (histo )]
19- sliced = list (reversed (sorted (sliced )))
20- n_spots , last = sliced [up_to - 1 ]
21- last += 1
33+ sliced = list (sorted (sliced ))
34+ n_spots , last = sliced [- 1 ]
35+ last += up_to
36+ print ("Removing all spots below slice " + str (last ))
2237
2338to_keep = []
24- for l in range (len (vals )):
25- lbl = int (rt .getLabel (l ))
26- val = rt .getValue ('Centroid.Z' , l )
27- if val > last :
39+ for lbl , pt in zip (all_lbls , pts ):
40+ if pt .getZ () > last :
2841 to_keep .append (lbl )
2942
3043print ("Keeping " + str (len (to_keep )) + " items from " + str (len (vals )))
3144
32- img = IJ .getImage ()
33- IJ .run ("Select Label(s)" , "label(s)=" + "," .join ([str (l ) for l in to_keep ]))
34-
35-
36-
37-
45+ res_img = keepLabels (img , to_keep )
46+ remapLabels (res_img )
47+ res_img .show ()
0 commit comments