Renaming more than 1000 ROIs

Dear ImageJ users

I encountered some problems using the ROI Manager .

If I use the function Roi.setName(name+index) to name the ROIs, it works fine until index 999, but after ROIs are named with the default pattern.

This bug, can be overcome using the function roiManager(“Select”) and roiManager(“Rename”) .

Please see code below,



bugStatus = false ;    // true to trigger the bug

imageSize = 1000;      // define and create an image
newImage("Untitled", "8-bit ramp", imageSize, imageSize, 1);

roiName = "test-";     // a roiName pattern

for (index = 0 ; index < 1200 ; index++){     // for more than 1000 ROIs
    xPoint = random * imageSize ;             // @ a random x 
    yPoint = random * imageSize ;             // and y position
    makePoint(xPoint,yPoint);                 // make a point
    roiNameAndNumber = roiName+(index+1);     // named after the pattern and a nbr 
    if(bugStatus){                            //
        Roi.setName(roiNameAndNumber);        // using Roi.setName naming bugs after 999
        roiManager("Add");                    // 
    } else{                                   //
        roiManager("Add");                    //
        roiManager("Select",index);           // but works well if we select and rename using roiManager

Hi @romainGuiet

the reason is quite likely these lines in that check if your ROI name is a standard one:

The following Groovy script is a quick test for this method that can be run from the Script Editor:

boolean isStandardName(String name) {
        if (name==null) return false;
        boolean isStandard = false;
        int len = name.length();
        if (len>=14 && name.charAt(4)=='-' && name.charAt(9)=='-' )
            isStandard = true;
        else if (len>=17 && name.charAt(5)=='-' && name.charAt(11)=='-' )
            isStandard = true;
        else if (len>=9 && name.charAt(4)=='-')
            isStandard = true;
        else if (len>=11 && name.charAt(5)=='-')
            isStandard = true;
        return isStandard;

testNames = ["0000-0000", "abcd-1234", "abcd-abcd", "abcde-1234", "abcd-12345", "test-999", "test-1000", "test1-1000"]

for (n in testNames) {
    println(n + ": " + isStandardName(n));

@Wayne might fix this in the ImageJ1.x source, but as a workaround, you can use any ROI name pattern that is different from four letters and a hyphen, e.g.

roiName = "testX-";     // a roiName pattern

or use a full-fledged scripting language.

1 Like

thank you very much for your answer!

This bug is fixed in the latest ImageJ daily build (1.50f22).