Apr 27, 2026

Reorder interleaved FLAIR slices (ImageJ macro) V.2

Reorder interleaved FLAIR slices (ImageJ macro)
  • 1Fred Hutchinson Cancer Center
Icon indicating open access to content
QR code linking to this content
Protocol CitationElena Carlson 2026. Reorder interleaved FLAIR slices (ImageJ macro). protocols.io https://dx.doi.org/10.17504/protocols.io.14egn5k8zg5d/v2Version created by Elena Carlson
License: This is an open access  protocol  distributed under the terms of the  Creative Commons Attribution License,  which permits unrestricted use, distribution, and reproduction in any medium, provided the original author and source are credited
Protocol status: Working
We use this protocol and it's working
Created: April 27, 2026
Last Modified: April 27, 2026
Protocol  Integer ID: 315820
Keywords: FLAIR, MRI, 7T MRI, slice order for flair mri data, flair mri data, slice order, correct anatomical order, true anatomical order, dicom filename, interleaved batch, flair slice, imagej macro, reorder, order, batch, acquisition order, slice, logs the dicom filename, dicom, mri data, new stack in true anatomical order, batches of slice, dicom filename for each slice, standard imagej, batches in order, weighted scan, use with standard imagej, datasets such as flair data, reordered stack, flair data, contrast enhancement to the middle slice, imagej, macro, scan, fiji macro
Funders Acknowledgements:
National Institutes of Health
Grant ID: P30 CA015704
National Institutes of Health
Grant ID: S10OD26919
Abstract
This protocol describes an ImageJ/Fiji macro that reorders interleaved multi‑slice MRI data acquired as 3 interleaved batches of slices (e.g., from a 7T MR Solutions MRI system) for datasets such as FLAIR data where the scan may need to be acquired in batches in order to get physiologically accurate data.

The macro works on any 24‑slice stack (e.g., FLAIR, T2‑weighted, T1-weighted scans), splits the stack into three equal substacks, and re‑interleaves the slices to produce a new stack in true anatomical order. The macro logs the DICOM filename for each slice during reordering, applies contrast enhancement to the middle slice of the reordered stack, and prompts the user to choose a save folder, exporting the result as  Reordered_<Original File Name>.tif.

The protocol is designed for use with standard ImageJ/Fiji installations and does not require additional plugins. Users are asked to cite the repository and this corresponding protocol when using the macro in published work.
Attachments
Materials
FIJI / ImageJ 1.54g
Data reslicing of FLAIR datasets (acquired in multislice configuration)
assumes Multibatch slice data that is not "in order" anatomically.

This code also assumes that a dataset titled "FLAIR" is open but this code can be modified to work for any multislice datasets (T2w, etc_)


Graphical Abstract

Open your anatomically out-of-order stacks in ImageJ / Fiji
Drag and drop the "Flair-multislice-reordering.ijm" into the ImageJ tool bar
To get to the macro menu/ Script Editor, follow these steps:

On Windows / Linux: Alt + Shift + N
On macOS: Option + Shift + N
Open the Script Editor and select language IJ1 Macro.
Paste the macro below and click Run.
Code is pasted below - however I recommend downloading the zip file attached here OR checking the latest version of the github repo and downloading the .ijm file directly.




// Reorder 24-slice FLAIR stack from interleaved to anatomical order
//assumes user already has open dataset(s) that they wish to process
// User selects which open dataset to process

titles = getList("image.titles");
if (titles.length == 0)
exit("No image windows are open.");

Dialog.create("Choose dataset");
Dialog.addImageChoice("Dataset");
Dialog.show();
origTitle = Dialog.getImageChoice();

selectWindow(origTitle);
run("Select All"); // selects the whole image

if (nSlices != 24) {
print("ERROR: nSlices = " + nSlices + " (expected 24).");
exit("Expected 24 slices.");
}
print("? Selected dataset: \"" + origTitle + "\"");

// Make 1–8
selectWindow(origTitle);
run("Make Substack...", "slices=1-8");
rename("Batch1");

// Make 9–16
selectWindow(origTitle);
run("Make Substack...", "slices=9-16");
rename("Batch2");

// Make 17–24
selectWindow(origTitle);
run("Make Substack...", "slices=17-24");
rename("Batch3");

// Create a new 24-slice stack for the reordered data
w = getWidth();
h = getHeight();
newImage("Reordered", "32-bit black", w, h, 24);
print("? Created 24-slice reordered stack.");

// Interleave with logging
print("Begin interleaving 3 × 8 slices; DICOM filenames:");
for (slice = 1; slice <= 8; slice++) {
for (batch = 1; batch <= 3; batch++) {
batchName = "Batch" + batch;

selectWindow(batchName);
setSlice(slice);
run("Copy");

dicomFile = getInfo("image.filename");
if (dicomFile == "") dicomFile = "(no filename)";
print(" ? Copy [Slice " + slice + " from " + batchName + " = " + dicomFile + "]");

selectWindow("Reordered");
setSlice((slice - 1) * 3 + batch);
run("Paste");
}
}
print("? Reordering done; check window 'Reordered'.");
// adjust contrast
selectWindow("Reordered");
mid = floor(nSlices/2) + 1;
setSlice(mid);
run("Enhance Contrast", "saturated=0.35");

// save data prompt
// Ask user for save folder
saveLocation = getDirectory("Where do you want to save the reordered stack?");

// Build filename: Reordered_.tif
outName = "Reordered_" + origTitle + ".tif";
savePath = saveLocation + outName;

selectWindow("Reordered");
saveAs("Tiff", savePath);
print("? Saved to: " + savePath);

//delete or comment out the below section if you would like to troubleshoot a reordering issue and make sure the batches are being split properly
if (isOpen("Batch1")) { selectWindow("Batch1"); close(); }
if (isOpen("Batch2")) { selectWindow("Batch2"); close(); }
if (isOpen("Batch3")) { selectWindow("Batch3"); close(); }


Expected result
Reordered: 24‑slice stack in anatomical order, contrast adjusted.

Data output should already be saved to the file location you chose.

Acknowledgements
This research was supported by the Preclinical Imaging Shared Resource (RRID:SCR_022616 ) of the Fred Hutch/University of Washington/Seattle Children’s Cancer Consortium (P30 CA015704) and the 3T/7T MRI Shared Instrumentation Grant NIH S10OD26919. .

Graphical Abstract created in https://BioRender.com