Skip to content

Instantly share code, notes, and snippets.

@omsai
Created March 13, 2012 15:51
Show Gist options
  • Save omsai/2029530 to your computer and use it in GitHub Desktop.
Save omsai/2029530 to your computer and use it in GitHub Desktop.
ImageJ macro - Stack Profile Plot
/*
Calculate specimen width in pixels using a line profile
Requires:
- Single channel image stack
- Line profile drawn on image
Output:
- Bitmap image stack of profile plots of original stack (not usable data!)
- Text file on desktop with numeric data with format:
ImageJ macro - Stack profile Plot
Total image slices = 56
Pixels in profile plot = 40
Rows of Plot Intensities:
110, 126, 108, 107, ...
108, 111, 115, 105, ...
112, 103, 102, 99, ...
...
Modified from http://imagej.nih.gov/ij/macros/StackProfilePlot.txt
Pariksheet Nanda <pariksheet.nanda@gmail.com>
*/
macro "Stack profile Plot" {
Y_MIN = 0;
Y_MAX = 16383;
// sanity check
if (nSlices==1)
exit("Stack required");
setBatchMode(true);
saveSettings();
run("Profile Plot Options...",
"width=400 height=400 minimum="+Y_MIN+" maximum="+Y_MAX+" fixed"
);
// prepare stacks
original_stack = getImageID;
plot_stack = 0;
total_slices = nSlices;
// save to file
home = getDirectory("home"); // Returns the path to users home directory.
if (home == "")
exit("No desktop directory available");
temp_file = home + "Desktop/" + getInfo("image.filename") + "_profiles.txt";
f = File.open(temp_file);
print(f, "ImageJ macro - Stack profile Plot");
print(f, "Total image slices = " + nSlices);
// cycle through image leaves
for (slice = 1; slice < total_slices; slice++) {
showProgress(slice, total_slices);
selectImage(original_stack);
setSlice(slice);
// get plot data
value = getProfile();
positions = lengthOf(value);
// write slice plot to file
if (slice == 1) {
print(f, "Pixels in profile plot = " + positions);
print(f, "");
print(f, "Rows of Plot Intensities:");
}
file_str = toString(value[0]);
for (pixel = 1; pixel < positions; pixel++)
file_str += ", " + value[pixel];
print(f, file_str);
// copy bitmap of line profile to plot stack
run("Plot Profile");
run("Copy");
w = getWidth; h = getHeight;
close();
if (plot_stack==0) {
newImage("Plots", "8-bit", w, h, 1);
plot_stack = getImageID;
} else {
selectImage(plot_stack);
run("Add Slice");
}
run("Paste");
}
setSlice(1);
setBatchMode(false);
restoreSettings();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment