Skip to content

Instantly share code, notes, and snippets.

@lacan
Last active May 4, 2023 11:06
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lacan/45f865b5a38d7c3a96a7cd8b25923407 to your computer and use it in GitHub Desktop.
Save lacan/45f865b5a38d7c3a96a7cd8b25923407 to your computer and use it in GitHub Desktop.
[1D Full Width At Half Maximum] This computes the Full Width at Half Maximum FWHM in an image for anz line profile drawn on the image and added to the ROI manager #fiji #imageJ #BIOP #FWHM
/**
* Calculate 1D Full Width at half Maximum of a single line or a series of lines added to the ROI Manager
* Created by Olivier Burri and Romain Guiet, BioImaging & Optics Platform (BIOP)
* Last Update: 2020.10.21
*
* Due to the simple nature of this code, no copyright applies
*
* Installation
* ------------
* From within Fiji Please use "Plugins > Macros > Install" and select this file
*
* Use
* ---
* Open an image and draw a line ROI across a feature for which you would like to calculcate the FWHM
* Press F2 to run the process.
*/
macro "Get FWHM [F2]" {
nR = roiManager("count");
image_name = getTitle();
if (selectionType() > 0 || nR > 0) {
if (nR > 0 ) {
for (i = 0; i < nR; i++) {
selectImage(image_name);
roiManager("select", i);
doFWHM();
}
} else {
doFWHM();
}
} else showMessage("Please draw a line.");
// Companion function to make the math for FWHM
function doFWHM(){
// Gather useful variables for final output. Title and Calibration
image_name = getTitle();
getVoxelSize( voxel_width, voxel_height, voxel_depth, voxel_unit );
// In case the ROI has a name, it is useful to add it to the results table
roi_name = Roi.getName;
// This line actually gets the 1D line profile of the selection. Note that if you have set a "Line Tickness"
// this one is also taken into account to provide an average value along the thickness of the line.
y = getProfile();
// Produce the X coordinates, but uncalibrated
x = Array.getSequence( lengthOf(y) );
// Use the Built-in Gaussian model from the ImageJ Curve Fitter (Gaussian with offset)
Fit.doFit("Gaussian", x, y);
// Display the plot for our ROI
Fit.plot;
rename( "FWHM "+roi_name );
// Fit parameters as follows: y = a + (b - a) exp( -(x - c^2)/(2*d^2) )
d = Fit.p(3); // parameter d of gaussian, related to the FWHM, see http://fr.wikipedia.org/wiki/Largeur_%C3%A0_mi-hauteur
rSquared = Fit.rSquared ;
FWHM = (2 * sqrt( 2 * log(2) ) ) * d;
nR = nResults;
// Prepare an out put in the form of a Results Table
setResult( "Label",nR, image_name );
setResult( "ROI", nR, roi_name );
setResult( "FWHM ("+voxel_unit+")", nR, FWHM * voxel_height ); // This step actually calibrates the FWHM
setResult( "rSquared",nR,rSquared );
updateResults();
selectWindow("Results");
}
@NicoKiaru
Copy link

I've got a -1 index array error with the nResults-1 indices in line 67 and 68.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment