Last active
January 21, 2025 11:41
-
-
Save petebankhead/f39d4158c6c52ff10590ff6763ef1218 to your computer and use it in GitHub Desktop.
QuPath script to export a maximum projection of an image using ImageJ, optionally separating all the channels
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* QuPath script to export a maximum projection of an image using ImageJ, | |
* optionally separating all the channels. | |
* | |
* If run within a project, this will save the output in a 'projections' | |
* subdirectory of the project. | |
* Otherwise, it will just show the max projection. | |
* | |
* Written for QuPath v0.5.1 (may work with other versions). | |
* | |
* This script assumes that the image isn't too big to fit in memory. | |
*/ | |
import ij.IJ | |
import ij.plugin.ChannelSplitter | |
import ij.plugin.ZProjector | |
import qupath.lib.regions.RegionRequest | |
import static qupath.lib.scripting.QP.* | |
import qupath.imagej.tools.IJTools | |
boolean showImages = true // Set to true if you want to show the images in ImageJ | |
boolean saveToProject = false // Set to true if you want to save the projections in the current project | |
boolean splitChannels = true // Set to true if you want to split the channels of the projection | |
double downsample = 1.0 // 1 for full resolution | |
// Get the ImageJ stack | |
var server = getCurrentServer() | |
var request = RegionRequest.createInstance(server, downsample) | |
var imp = IJTools.extractHyperstack(server, request) | |
// Create the max projection | |
var impMax = ZProjector.run(imp, "max") | |
if (showImages) { | |
// If we don't have a project, we don't know which file name to use... so just show the max projection | |
if (splitChannels) { | |
for (var impC : ChannelSplitter.split(impMax)) { | |
impC.show() | |
} | |
} else { | |
impMax.show() | |
} | |
} | |
if (saveToProject && getProject() != null) { | |
// Create a subdirectory in the project to store the output | |
var dirOutput = buildPathInProject("projections") | |
mkdirs(dirOutput) | |
// Get the image name - we hope it's unique if running for the whole project... | |
var name = getCurrentImageNameWithoutExtension() | |
// Optionally split the channels and save each separately | |
if (splitChannels) { | |
int channel = 1 | |
// Save each channel separately | |
for (var impC : ChannelSplitter.split(imp)) { | |
var pathOutput = buildFilePath(dirOutput, "${name}-max-C${channel}.tif") | |
IJ.saveAsTiff(impC, pathOutput) | |
channel++ | |
} | |
} else { | |
// Save the max projection as a TIFF | |
var pathOutput = buildFilePath(dirOutput, "${name}-max.tif") | |
IJ.saveAsTiff(impMax, pathOutput) | |
println "Saved max projection to $pathOutput" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment