Skip to content

Instantly share code, notes, and snippets.

@romainGuiet
Created October 27, 2021 13:53
Show Gist options
  • Save romainGuiet/84ef6df89219c40f0d697199cc1fe4af to your computer and use it in GitHub Desktop.
Save romainGuiet/84ef6df89219c40f0d697199cc1fe4af to your computer and use it in GitHub Desktop.
Convert a Labels image to Measurements image (requires morpholibJ)
#@ImagePlus imp // imp should be a 3D label image
#@String stat_name // morpholibJ output table after Analyze Regions 3D
// using morpholibJ , update site : IJPB-plugins
IJ.run("Analyze Regions 3D", "voxel_count volume surface_area mean_breadth sphericity euler_number bounding_box centroid equivalent_ellipsoid ellipsoid_elongations max._inscribed surface_area_method=[Crofton (13 dirs.)] euler_connectivity=6");
// Duplicate the imp and make it 32-bit
imp_label = imp.duplicate();
IJ.run(imp_label, "32-bit", "");
// run morpholibj, which creates not a ResultsTable but a TextWindow
activeTable = WindowManager.getActiveTable()
// and make it a ResultsTable
rt = activeTable.getResultsTable()
// here we get some Value and corresponding Label
labels = rt.getColumnAsVariables("Label")
stats = rt.getColumnAsVariables(stat_name)
// Now this is the time to make a HashMap (label and stat ),
// KUDOs to olivier.burri
keylist = new HashMap< Double, Double>()
[labels, stats].transpose().each{ lab , stat ->
keylist.put(lab.getValue(), stat.getValue())
}
// go through z, get ImageProcessor and getPixel set Stat value accordingly to Label value
for (z in (0..<imp_label.getNSlices() ) ) {
imp_label.setZ(z);
ip = imp_label.getProcessor().duplicate();
for (x in (0..<imp_label.getWidth() ) ) {
for (y in (0..<imp_label.getHeight() ) ) {
pxVal = ip.getf(x,y)
if (pxVal>0 ) {
ip.setf( x,y , (keylist[pxVal as Double] as float ))
}
}
}
imp_label.setProcessor(ip)
}
// Set LUT and reset display
imp_label.show()
imp_label.setZ(imp_label.getNSlices()/2 as int );
IJ.run(imp_label, "Fire", "");
IJ.resetMinAndMax(imp_label);
import ij.*
import ij.measure.ResultsTable
import ij.WindowManager
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment