Created
January 19, 2023 19:05
-
-
Save petebankhead/b729f1ac11d08485d3e671fd7de92b5d to your computer and use it in GitHub Desktop.
QuPath script to extract a hyperstack + overlay and send it to ImageJ (written for v0.4.2)
This file contains 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 extract a stack/hyperstack + overlay and send it to ImageJ. | |
* | |
* This will either extract a hyperstack for the bounding box of the current selected object, | |
* or for the entire image if no object is selected. | |
* | |
* Written for https://forum.image.sc/t/qupath-how-to-send-multiple-annotations-containing-more-annotations-to-imagej-with-all-z-slices-and-re-name/76107 | |
* | |
* Tested (to an extent...) with QuPath v0.4.2 | |
* | |
* Note that if you transfer the ROIs to the ROI Manager in ImageJ, | |
* you may need to choose 'More>>' then 'Options' and turn on 'Associate "Show All" ROIs with slices' | |
* | |
* @author Pete Bankhead | |
*/ | |
import ij.gui.Overlay | |
import qupath.imagej.gui.IJExtension | |
import qupath.imagej.tools.IJTools | |
import qupath.lib.regions.RegionRequest | |
import static qupath.lib.gui.scripting.QPEx.* | |
def imageData = getCurrentImageData() | |
def server = imageData.getServer() | |
def hierarchy = imageData.getHierarchy() | |
// Define resolution for hyperstack | |
double downsample = 1.0 | |
def selected = hierarchy.getSelectionModel().getSelectedObject() | |
RegionRequest request | |
if (!selected) { | |
println "No object selected, I'll try to extract everything" | |
request = RegionRequest.createInstance(server, downsample) | |
} else { | |
println "I'll try to select a hyperstack for the selected object" | |
request = RegionRequest.createInstance(server.getPath(), downsample, selected.getROI()) | |
} | |
// Extract the hyperstack | |
def imp = IJTools.extractHyperstack(server, request) | |
// Extract the overlay - using the currently display settings from the viewer | |
// (This ties the script to a viewer - something different is needed to run headless) | |
def overlayOptions = getQuPath().getViewer().getOverlayOptions() | |
def overlay = new Overlay() | |
// Loop through z-slices and timepoints (planes) | |
for (int t = 0; t < server.nTimepoints(); t++) { | |
request = request.updateT(t) | |
for (int z = 0; z < server.nZSlices(); z++) { | |
request = request.updateZ(z) | |
// Extract an overlay for the current image plane & transfer to the main overlay | |
def overlayTemp = IJExtension.extractOverlay(hierarchy, request, overlayOptions, p -> p != selected) | |
for (def r : overlayTemp.toArray()) { | |
r.setPosition(0, z+1, t+1) | |
overlay.add(r) | |
} | |
} | |
} | |
imp.setOverlay(overlay) | |
imp.show() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment