// QuPath v0.7.0
/**
* This exports the selected annotation and its ROI to ImageJ,
* clears outside, and saves the output in a folder
* called swan within your QuPath project.
*/
import ij.IJ
import qupath.lib.regions.RegionRequest
import qupath.imagej.tools.IJTools
def name = getCurrentImageNameWithoutExtension()
// Create output folder
outputPath = buildFilePath(PROJECT_BASE_DIR, "swan")
mkdirs(outputPath)
def imageData = getCurrentImageData()
def server = imageData.getServer()
def downsample = 1 // if the image is large, it could be enlarged
def anno = getSelectedObject()
def roi = anno.getROI()
// Request the region
def request = RegionRequest.createInstance(server.getPath(), downsample, roi)
// Extract the region as an ImageJ ImagePlus
def imp = IJTools.convertToImagePlus(server, request).getImage()
// Convert the QuPath ROI into an ImageJ
def ijRoi = IJTools.convertToIJRoi(roi, request)
imp.setRoi(ijRoi)
IJ.setBackgroundColor(255, 255, 255)
IJ.run(imp, "Clear Outside", "")
def nameExportSwan = name + "_swan" + ".png"
// Export the image with IJ
pathExport = buildFilePath(outputPath, nameExportSwan)
IJ.saveAs(imp, "png", pathExport)
println "Done! Image saved at: " + outputPath