-
Notifications
You must be signed in to change notification settings - Fork 1
/
0.4.0 Export Binary Masks.groovy
40 lines (31 loc) · 1.6 KB
/
0.4.0 Export Binary Masks.groovy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
import qupath.imagej.tools.IJTools
import qupath.lib.gui.images.servers.RenderedImageServer
import qupath.lib.gui.viewer.overlays.HierarchyOverlay
import qupath.lib.regions.RegionRequest
import static qupath.lib.gui.scripting.QPEx.*
def viewer = getCurrentViewer()
def imageData = getCurrentImageData()
// Define output path (relative to project)
def outputDir = buildFilePath(PROJECT_BASE_DIR, 'export')
mkdirs(outputDir)
def name = GeneralTools.getNameWithoutExtension(imageData.getServer().getMetadata().getName())
def path = buildFilePath(outputDir, name + "-he.png")
def pathMask = buildFilePath(outputDir, name + "-mask.png")
// Define how much to downsample during export (may be required for large images)
double downsample = 40
def server = new RenderedImageServer.Builder(imageData)
.downsamples(downsample)
//.layers(new HierarchyOverlay(viewer.getImageRegionStore(), viewer.getOverlayOptions(), imageData))
.build()
// Create an ImageServer where the pixels are derived from annotations
def labelServer = new LabeledImageServer.Builder(imageData)
.backgroundLabel(255, ColorTools.WHITE) // Specify background label (usually 0 or 255)
.downsample(downsample) // Choose server resolution; this should match the resolution at which tiles are exported
.addLabel('Lung', 1) // Choose output labels (the order matters!)
.addLabel('Tumor', 2)
.multichannelOutput(false) // If true, each label refers to the channel of a multichannel binary image (required for multiclass probability)
.build()
// Write the image
writeImage(server, path)
writeImage(labelServer, pathMask)
print("Done")