Hi All,

I’m new to ImageJ image processing and have a question about drawing multiple custom ROIs on an image and saving each ROI as a distinct TIFF. I’m working with images of a rodent brain, and I need to divide the brain into 8 different regions that vary in size and shape. *(see image of brain)

I’d love to have a macro that can do the following:

  • Open file directory
  • Select TIFF files in that directory
  • Set the scale on the TIFF by tracing my watermarked scale bar (already on image) using the Polygon tool
  • Select the Polygon tool to draw custom ROI
  • Add custom ROI to the ROI manager, rename it using the image name, and include a suffix to the name (e.g.,“_1”)
  • Keep the ROI on the image so that I can see the boundary line between the distinct regions when I’m drawing the remaining 7 so would need to include: roiManager(“Show All”)
  • Draw the remaining 7 polygons, adding them to the ROI manager, and renaming them following the naming syntax.
  • For each ROI, I need to a. run(“Crop”) b. setBackgroundColor(0,0,0) c. run(“Clear Outside”) d. run(“To Bounding Box”) e. saveAs(“Tiff”) and have it store in my output directory using the name of the original file with the suffix indicating the cropped region as a suffix)
  • The final products should be the .rois for each polygon boundary that I created and added to my ROI manager and TIFF files for each polygon boundary.*(see image of brain with boundaries denoted)

    I have a macro that allows me to crop and save images with custom naming syntax, but I’m struggling to modify it to do everything that I need. Here’s the macro that I’ve been using to achieve cropping and saving ROIs, but again, it’s not quite what I need.

    I’d love to hear your thoughts and get feedback! Thanks! (Question cross-posted on image.sc)

    //Use this macro for cropping hippocampal sub-regions

    regionsToDo = newArray(“_CA1_L”,“_CA1_R”, “_CA2_L.1”,“_CA2_R.1”,“_CA2_L.2”,“_CA2_R.2”, “_CA3_L”,“_CA3_R”, “_DG_L”,“_DG_R”);

    // Create anatomical ROIs within each image in a folder dir = getDirectory(“Choose directory to CROP ROIs from slices”); // prompts user to select the folder to be processed, stores the folder as the destination for saving list = getFileList(dir); // gives ImageJ a list of all files in the folder to work through print(list.length); // optional prints the number of files in the folder dir2 = getDirectory(“Choose a Directory for SAVING the ROIs”); // prompts user to select the folder where files will be SAVED

    // Function for cropping based on a region. This function is called in the main processing loop. function getRegionCrop(nameNoExtension, dirToSave, regionName) { t2 = nameNoExtension + regionName; //adds regionName to the original name of the image (“t” below). run(“Duplicate…”, “duplicate”); setTool(“line”); waitForUser(“Draw the scale bar”); run(“Set Scale…”, “known=10 unit=cm”); //edit this line to reflect actual size & units of scale bar setTool(“polygon”); waitForUser(‘Please Select the ’ + regionName + ’ ROI’);

    //if no selection is possible, need to skip a step if (selectionType() == 3 || selectionType() == 2) {regionSelected = true;} else {regionSelected = false;} //if selection is possible, crop out selection and bound it. if (regionSelected) { run(“Crop”); setBackgroundColor(0, 0, 0); //clear the stuff we don’t want. run(“Clear Outside”); //eliminate data outside of ROI run(“To Bounding Box”); saveAs(“Tiff”, dir2 + t2 + “_” + “Cropped” + “.tif”); } else { closeWindows=getBoolean(“Close window?”); if (closeWindows) { run(“Close”); } } run(“Close”); }

    // Close everything before we start. run (“Close All”);

    // main files processing loop for (f=0; f=1) { //use the different channels to illuminate cells and landmarks start = getTime(); //optional get start time to see how long a process will take. Goes with last line print time

    t=getTitle(); //gets the name of the image being processed s=lastIndexOf(t, '.'); //next three lines are code to strip off the .tif extension from the filename t=substring(t, 0,s); t=replace(t," ","_"); t=replace(t, "hippocampus_ROI", "_");

    // Pass the Array of anatomical regions to a cropping Function. Go through each region in the Array, for (thisRegion=0; thisRegion < regionsToDo.length; thisRegion++) { getRegionCrop(t, dir2, regionsToDo[thisRegion]); //getRegionCrop function defined above. getRegionCrop(nameNoExtension, dirToSave, regionName) } run(“Close”); //when done, close the image. } }

    More Rachel E Fanelli's questions See All
    Similar questions and discussions