After completing this lesson, learners should be able to:
Save measurements as a table
Save ROIs
Save output label mask
Motivation
There are multiple situations in which you need to save the different types of output you can generate with your image analysis pipelines. For example, you may want to save your results as measurement tables for further analysis in other software (e.g. RStudio, MS EXCEL, ..). It can also be important to save the regions of interest (ROIs) that were used for particular measurements, so that you can look back at them for reference, or to use them for visualization purposes. In addition to ROIs, images can be saved as label masks to store the information on different regions. Finally, you may also want to save your entire script containing particular settings or parameters that you used, so that you can re-run the analysis with the exact same settings or compare it with the results obtained using different settings.
Concept map
graph TD
II("Input image") --> IA("Image analysis process")
IA --> RS("Measurements table")
IA --> ROI("ROIs")
IA --> PM("Label mask")
Figure
Image analysis processes can yield several outputs, such as a) label masks, b) measurement tables, or c) ROI sets.
Perform measurements on the different foreground objects, generating a ROI set, label image, and measurements table.
Specify an output directory (for example ‘C:\\Users\\username\\Desktop’ on Windows (note double “\” because a single “\” is interpreted as an escape character) or ‘/Users/username/Desktop/’ on MacOS).
Save the measurements table, ROI set and label image to the specified output directory.
Show activity for:
ImageJ Macro
// This macro uses the particle analyzer to measure features of shapes.// Different outputs are saved: ROIs, results table, and label mask.// specify an output directoryoutputDir=FIXME// specify size parameters for object selectionminSize=0maxSize=1000// close any pre-existing output you do not want in your saving resultsroiManager("reset");// clear any pre-existing ROIsrun("Clear Results");// clear any pre-existing resultsrun("Close All");// close any open images// Set measurements and run particle analyzer on binary shapes imagerun("Set Measurements...","area centroid center perimeter bounding redirect=None decimal=3")// set desired measurementsopen("https://github.com/NEUBIAS/training-resources/raw/master/image_data/xy_8bit_binary_randomshapes.tif");// open binary image with random shapesrun("Analyze Particles...","size=&minSize-&maxSize show=[Count Masks] display add")// run the particle analyzerrun("glasbey")// Save the outputroiManager("Save",outputDir+File.separator+"shapes_ROIset_macro.zip");// save rois to output directory. saveAs("Results",outputDir+File.separator+"shapes_results_macro.txt");// save results file to output directorysaveAs("Tiff",outputDir+File.separator+"shapes_labels_macro.tif");// save label mask to output directory
ImageJ Jython
# import classes
fromijimportIJ,ImagePlus,WindowManagerfromij.ioimportFileSaverfromij.plugin.filterimportParticleAnalyzerfromij.plugin.frameimportRoiManagerfromij.measureimportResultsTable,Measurementsfromij.processimportImageProcessorimportos# Specify an output directory
outputDir=FIXME# Specify size parameters for object selection
min_size=0max_size=1000# Initialize Roi Manager and empty results table, close other open images
rm=RoiManager().getInstance()rm.reset()IJ.run("Close All")# Open binary shapes image
shapes=IJ.openImage("https://github.com/NEUBIAS/training-resources/raw/master/image_data/xy_8bit_binary_randomshapes.tif")# Configure and run particle analyzer
results=ResultsTable()# construct empty resultstable
pa=ParticleAnalyzer((ParticleAnalyzer.ADD_TO_MANAGER+ParticleAnalyzer.SHOW_ROI_MASKS),(Measurements.AREA+Measurements.CENTROID+Measurements.CENTER_OF_MASS+Measurements.PERIMETER+Measurements.RECT),results,min_size,max_size,0,1)pa.analyze(shapes)# run the particle analyzer on the image
results.show("Results")# Save results, label mask, and ROIs
results.save(os.path.join(outputDir,"shapes_results_jython.txt"))# save results table
labelMask=WindowManager.getImage("Count Masks of xy_8bit_binary_randomshapes.tif")IJ.run(labelMask,"Glasbey","")# set glasbey LUT
FileSaver(labelMask).saveAsTiff(os.path.join(outputDir,"shapes_labels_jython.tif"))# save the label mask
rm.runCommand("Save",os.path.join(outputDir,"shapes_ROIset_jython.zip"))# save the ROIs
Exercises
Show exercise/solution for:
ImageJ Macro
Adapt the code from the activity such that you:
Specify an output directory in the beginning
Save the results table as comma-separated data table instead of text-delimited data.
Save the output label image in a different image format (e.g. PNG, JPEG). Is this a good format for label images?
Solution
// This macro uses the particle analyzer to measure features of shapes.// Different outputs are saved: ROIs, results table, and label mask.// specify an output directoryoutputDir=FIXME// (e.g. 'C:\\Users\\username\\Desktop' or 'C:/Users/username/Desktop' on Windows, or '/Users/username/Desktop/' on MacOS)// specify size parameters for object selectionminSize=0maxSize=1000// close any pre-existing output you do not want in your saving resultsroiManager("reset");// clear any pre-existing ROIsrun("Clear Results");// clear any pre-existing resultsrun("Close All");// close any open images// Set measurements and run particle analyzer on binary shapes imagerun("Set Measurements...","area centroid center perimeter bounding redirect=None decimal=3")// set desired measurementsopen("https://github.com/NEUBIAS/training-resources/raw/master/image_data/xy_8bit_binary_randomshapes.tif");// open binary image with random shapesrun("Analyze Particles...","size=&minSize-&maxSize show=[Count Masks] display add")// run the particle analyzer>run("glasbey")// Save the resultsroiManager("Save",outputDir+"/shapes_ROIset_macro.zip");// save rois to output directorysaveAs("Results",outputDir+"/shapes_results_macro.csv");// save results file to output directorysaveAs("Png",outputDir+"/shapes_labels_macro.png");// save label mask to output directory
ImageJ Jython
Adapt the code from the activity such that you:
Specify an output directory in the beginning
Save the results table as comma-separated data table instead of text-delimited data.
Save the output label image in a different image format (e.g. PNG, JPEG). Is this a good format for label images?
Solution
# import classes
fromijimportIJ,ImagePlus,WindowManagerfromij.ioimportFileSaverfromij.plugin.filterimportParticleAnalyzerfromij.plugin.frameimportRoiManagerfromij.measureimportResultsTable,Measurementsfromij.processimportImageProcessorimportos# Specify an output directory
outputDir=FIXME# (e.g. r'C:\Users\username\Desktop', 'C:\\Users\\username\\Desktop' or 'C:/Users/username/Desktop' on Windows or '/Users/username/Desktop/' on MacOS)
# Specify size parameters for object selection
min_size=0max_size=1000# Initialize Roi Manager and empty results table, close other open images
rm=RoiManager().getInstance()rm.reset()IJ.run("Close All")# Open binary shapes image
shapes=IJ.openImage("https://github.com/NEUBIAS/training-resources/raw/master/image_data/xy_8bit_binary_randomshapes.tif")# Configure and run particle analyzer
results=ResultsTable()# construct empty resultstable
pa=ParticleAnalyzer((ParticleAnalyzer.ADD_TO_MANAGER+ParticleAnalyzer.SHOW_ROI_MASKS),(Measurements.AREA+Measurements.CENTROID+Measurements.CENTER_OF_MASS+Measurements.PERIMETER+Measurements.RECT),results,min_size,max_size,0,1)pa.analyze(shapes)# run the particle analyzer on the image
results.show("Results")# Save results, label mask, and ROIs
results.save(os.path.join(outputDir,"shapes_results_jython.csv"))# save results table
labelMask=WindowManager.getImage("Count Masks of xy_8bit_binary_randomshapes.tif")IJ.run(labelMask,"Glasbey","")# set glasbey LUT
FileSaver(labelMask).saveAsTiff(os.path.join(outputDir,"shapes_labels_jython.png"))# save the label mask
rm.runCommand("Save",os.path.join(outputDir,"shapes_ROIset_jython.zip"))# save the ROIs
Assessment
Fill in the blanks
Three commonly saved data output types include ___ .
In order to safely re-open your data or open it in different software, you need to save in an __ file format.
Solution
Three commonly saved data output types include results tables, ROI sets, and label masks.
In order to safely re-open your data or open it in different software, you need to save in an interoperable file format.
True or False
Label masks should be saved in JPEG format.
Tab-delimited text is a decent output format for results tables.
Solution
Label masks should be saved in JPEG format. False (JPEG compression results in loss of the unique label values in the image)
Tab-delimited text is a decent output format for results tables. True (this is generally more stable in other software than for example comma-delimited data)