Skip to content

Instantly share code, notes, and snippets.

@NicoKiaru
Created December 12, 2018 09:23
Show Gist options
  • Save NicoKiaru/ae00117cd6d33fea500d2867a5e669d9 to your computer and use it in GitHub Desktop.
Save NicoKiaru/ae00117cd6d33fea500d2867a5e669d9 to your computer and use it in GitHub Desktop.
Transforms a label image (in fact any imageplus) into a list ot ROI contained in the ROI Manager
#@ImagePlus imp
import ij.ImagePlus;
import ij.gui.Roi;
import java.util.HashSet;
import ij.process.ImageProcessor;
import ij.plugin.filter.ThresholdToSelection;
import ij.plugin.frame.RoiManager;
import ij.process.FloatProcessor
rois = labelImageToRoiArray(imp)
putRoisToRoiManager(rois,false);
//------------- HELPERS
public ArrayList<Roi> labelImageToRoiArray(ImagePlus imp) {
ArrayList<Roi> roiArray = new ArrayList<>();
ImageProcessor ip = imp.getProcessor();
float[][] pixels = ip.getFloatArray();
HashSet<Float> existingPixelValues = new HashSet<>();
for (int x=0;x<ip.getWidth();x++) {
for (int y=0;y<ip.getHeight();y++) {
existingPixelValues.add((pixels[x][y]));
}
}
// Converts data in case thats a RGB Image
fp = new FloatProcessor(ip.getWidth(), ip.getHeight())
fp.setFloatArray(pixels)
imgFloatCopy = new ImagePlus("FloatLabel",fp)
existingPixelValues.each { v ->
fp.setThreshold( v,v,ImageProcessor.NO_LUT_UPDATE);
Roi roi = ThresholdToSelection.run(imgFloatCopy);
roi.setName(Integer.toString((int) (double) v));
roiArray.add(roi);
}
return roiArray;
}
public static void putRoisToRoiManager(ArrayList<Roi> rois, boolean keepROISName) {
RoiManager roiManager = RoiManager.getRoiManager();
if (roiManager==null) {
roiManager = new RoiManager();
}
roiManager.reset();
for (int i = 0; i < rois.size(); i++) {
if (!keepROISName) {
rois.get(i).setName(""+i);
}
roiManager.addRoi(rois.get(i));
}
roiManager.runCommand("Show All");
}
@NicoKiaru
Copy link
Author

NicoKiaru commented Aug 16, 2021

Hi @christianrickert and thanks for the comment. FYI this gist implementation is a bit outdated. @romainGuiet improved this macro a lot and additional functionalities are accessible in https://github.com/BIOP/ijp-LaRoMe

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment