Get Annotations back from ImageJ macro

Hi all,

I have a script in QuPath which calls imageJ to run a macro. The macro works in ImageJ/Fiji and gives me what I want. Even running the ImageJ macro runner works and gives me back the segmentation. However, running it in the script doesn’t add any annotations to my image.


params      = new ImageJMacroRunner(getQuPath()).getParameterList()

// Change the value of a parameter, using the JSON to identify the key
params.getParameters().get('downsampleFactor').setValue(fibers_downsample)
params.getParameters().get('sendROI').setValue(false)
params.getParameters().get('sendOverlay').setValue(false)
params.getParameters().get('getOverlay').setValue(false)
if (!getQuPath().getClass().getPackage()?.getImplementationVersion()){
    params.getParameters().get('getOverlayAs').setValue('Annotations')
}
params.getParameters().get('getROI').setValue(true)
params.getParameters().get('clearObjects').setValue(false)

macro = 'original = getImageID();run("Duplicate...", "title=fibers duplicate channels=3");run("Duplicate...", "title=smooth");'+
'run("Duplicate...", "title=muscle");run("Gaussian Blur...", "sigma=50");setAutoThreshold("Triangle dark");//run("Threshold...");'+
'setOption("BlackBackground", true);run("Convert to Mask");run("Create Selection");selectWindow("smooth");'+
'run("Gaussian Blur...", "sigma=20");'+
'imageCalculator("Divide create", "fibers","smooth");selectWindow("Result of fibers");run("Restore Selection");'+
'run("Clear Outside");setAutoThreshold("IsoData dark");run("Convert to Mask");run("Create Selection");'+
'run("Colors...", "foreground=white background=black selection=white");'+
'run("Properties...", "channels=1 slices=1 frames=1 unit=um pixel_width='+pixelSize+' pixel_height='+pixelSize+' voxel_depth=1");'+
'selectImage(original);run("Restore Selection");'

ImageJMacroRunner.runMacro(params, imageData, null, annotation, macro)}

Thanks for the help.

When you send an image from QuPath to ImageJ, you have access to some custom ImageJ plugins to send ROIs back to QuPath. By adding calls to these to the macro is it possible to solve the problem?

I tried adding run("Send ROI to QuPath"); or run("Send Overlay to QuPath") to the macro but it didn’t work…

The thing is that this one works and I have no idea what’s different…

macro ='original = getImageID();run("Duplicate...", "title=X3t4Y6lEt duplicate");'+
'weights=newArray('+fibers_weightList+');run("Stack to Images");name=getTitle();'+
'baseName = substring(name, 0, lengthOf(name)-1);'+
'for (i=0; i<'+fibers_weights.size()+';'+
'i++){currentImage = baseName+(i+1);selectWindow(currentImage);'+
'run("Multiply...", "value="+weights[i]);}'+
'run("Images to Stack", "name=Stack title=[X3t4Y6lEt] use");'+
'run("Z Project...", "projection=[Sum Slices]");'+
'run("Gaussian Blur...", "sigma='+fibers_sigma+'");'+
'setAutoThreshold("Huang");run("Convert to Mask");'+
'run("Create Selection");run("Colors...", "foreground=white background=black selection=white");'+
'run("Properties...", "channels=1 slices=1 frames=1 unit=um pixel_width='+pixelSize+' pixel_height='+pixelSize+' voxel_depth=1");'+
'selectImage(original);run("Restore Selection");'
if (!getQuPath().getClass().getPackage()?.getImplementationVersion()){
    params.getParameters().get('getOverlayAs').setValue('Annotations')
}

This line was intended to distinguish between 0.1.2 and 0.1.3, where the macro runner changed slightly. I have not played with how 0.2.0 might handle this, but I do remember that it was important when sending the annotations back into QuPath.
It could be that the if statement is failing (negative), but the line within the if statement is necessary in 0.2.0.

I tried removing it and only keeping the line in between but it doesn’t work :confused:

Is params.getParameters().get('getROI').setValue(true) supposed to correspond to the Create Annotations from ImageJ ROI in the ImageJ macro runner GUI ? Because that works using the GUI but not through the script :confused:

Something like that. I would need to do something like println(params.getParameters()) or something like that and sort through what the options should be currently. Are you working off of 0.2.0.m2?

Oh, wait, I skimmed through and missed that the other, similar macro was returning results. That sounds like more of a macro issue, which I probably won’t be able to troubleshoot too well until I get home and have a reasonable image to test it on.

I don’t see any immediate problems, but you are doing a lot more image duplication in the macro that doesn’t work within the QuPath macro, and I am wondering if some targeting might end up getting confused.

This is what I get, so it should work.
INFO: [title1:Setup, downsampleFactor:Downsample factor: 1.0, sendROI:Send ROI to ImageJ: false, sendOverlay:Send overlay to ImageJ: false, doParallel:Do parallel processing (experimental): false, title2:Results, clearObjects:Clear current child objects: false, getROI:Create annotation from ImageJ ROI: true, getOverlay:Get objects from ImageJ overlay: false, getOverlayAs:Get objects as: Detections]

Ah yes indeed, I was wondering about that, should I try and close the other ones ? There is some duplications in the working one as well…

The params all look fine, especially since they are working with the shorter script. I guess you are able to send your whole image to ImageJ with a downsample factor of 1 when you are using the macro runner?

And as far as closing other images, maaaaybe. Like I said, I am not great with ImageJ macro coding, and only played with that enough to get it working. It wasn’t really designed to be edit-friendly :slight_smile:

Unfortunately no :frowning:

Thanks to @Research_Associate we figured out that the comments in the macro was the culprit. If you remove it, then it works !

 macro = 'original = getImageID();run("Duplicate...", "title=fibers duplicate channels=3");run("Duplicate...", "title=smooth");'+
'run("Duplicate...", "title=muscle");run("Gaussian Blur...", "sigma=50");setAutoThreshold("Triangle dark");'+
'setOption("BlackBackground", true);run("Convert to Mask");run("Create Selection");selectWindow("smooth");'+
'run("Gaussian Blur...", "sigma=20");'+
'imageCalculator("Divide create", "fibers","smooth");selectWindow("Result of fibers");run("Restore Selection");'+
'run("Clear Outside");setAutoThreshold("IsoData dark");run("Convert to Mask");run("Create Selection");'+
'run("Colors...", "foreground=white background=black selection=white");'+
'run("Properties...", "channels=1 slices=1 frames=1 unit=um pixel_width='+pixelSize+' pixel_height='+pixelSize+' voxel_depth=1");'+
'selectImage(original);run("Restore Selection");'