Created
December 12, 2018 09:23
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#@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"); | |
} |
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
Thanks @NicoKiaru for the clean implementation!
I've written two complementary macro functions,
getRoisFromMasks
andsetMasksFromRois
, as part of our CU-MacroLibrary repository: The first function imports selections from a grayscale mask image into the ROI Manager and the second function exports selections from the ROI Manager to a new grayscale mask image.Chris