After completing this lesson, learners should be able to:
Avoid code duplication using functions.
Understand the skeleton of a function.
Make the script more efficient.
Motivation
When a series of steps(i.e. algorithm) that has to be performed on an image or a set of images should be executed more than once, or when the script gets too long and repetitive, it is more efficient to write this series as modules called “functions”.
This essentially means that you can reuse parts of code instead of rewriting it. A function is a block that has a specific name and can be called with inputs and can return values.
Concept map
graph TD
IV("Input values") --> F("Function")
F -->|generates| OV("Output values")
F -->|contains| RC("Reusable code")
Figure
Components and working of a function within a script.
Activities
Open an image stack
Enter the slice numbers for which you want to calculate mean intensity.
Run a function to extract the specified slices, display them and calculate their mean intensity.
Display the mean intensity value for each specified slice.
Show activity for:
ImageJ Macro
run("Close All");// close all opened windowsrun("MRI Stack");// load the example MRI image stackimageName=getTitle();//get the image title//select first slice, calculate its mean intensity and display itselectWindow(imageName);sliceNumber=1;meanIntensitySlice1=calculateSliceMeanIntensity(sliceNumber);print("Slice#"+sliceNumber+" has mean intensity: "+meanIntensitySlice1);//select last slice, calculate its mean intensity and display itselectWindow(imageName);sliceNumber=nSlices;meanIntensitySlice2=calculateSliceMeanIntensity(sliceNumber);print("Slice#"+sliceNumber+" has mean intensity: "+meanIntensitySlice2);// a function that calculates mean intensity of a specified slice in a stack and displays itfunctioncalculateSliceMeanIntensity(sliceNumber){setSlice(sliceNumber);run("Duplicate...","title=slice"+sliceNumber);sliceName="slice"+sliceNumber;selectWindow(sliceName);getStatistics(area,mean);returnmean;}
Exercises
Show exercise/solution for:
Exercise1 - ImageJ Macro
Use “function_display_slice.ijm”: and display mean intensity values of slices without returning any values from the function.
Select two different slice indices of your choice .
Modify the function “calculateSliceMeanIntensity” in such a way that it returns nothing and displays mean intensity of each individual slice.
Solution
run("Close All"); // close all opened windows
run("MRI Stack"); // load the example MRI image stack
imageName = getTitle(); //get the image title
//select first slice,calculate its mean intensity and display it
selectWindow(imageName);
sliceNumber = 5;
calculateSliceMeanIntensity(sliceNumber);
//select last slice,calculate its mean intensity and display it
selectWindow(imageName);
sliceNumber = 10;
calculateSliceMeanIntensity(sliceNumber);
// a function that calculates mean intensity of a specified slice in a stack and displays it
function calculateSliceMeanIntensity(sliceNumber)
{
setSlice(sliceNumber);
run("Duplicate...", "title=slice"+sliceNumber);
sliceName = "slice"+sliceNumber;
selectWindow(sliceName);
run("8-bit");
getStatistics(area, mean);
print("Slice#"+ sliceNumber + " has mean intensity: " + mean);
}
Exercise2 - ImageJ Macro
Use “cc_counting_after_closing.ijm”: and do the following tasks: