Saving a mask with Bio-formats

Hi all,

It’s probably a stupid question but I’m getting a weird behaviour in one of my script.

Here is the part that is working:

imp1_mask = imp1_bg_removed.duplicate()
middle_slice = int(imp1_mask.getNSlices() / 2)
imp1_mask.setSlice(middle_slice)
IJ.setAutoThreshold(imp1_mask, "Otsu dark stack");
ImProc = imp1_mask.getProcessor()
lower_thr = ImProc.getMinThreshold() - 20
upper_thr = ImProc.getMaxThreshold()
IJ.log("lower : " + str(lower_thr) + " ; upper : " + str(upper_thr))
IJ.setThreshold(imp1_mask, lower_thr, upper_thr);
#imp1_mask = imp1_bg_removed.createMask() 
IJ.run(imp1_mask, "Convert to Mask", "background=Dark black")
imp1_bg_removed.show()

This correctly shows me the thresholded version of my image. However when I do BFExport(imp1_mask, os.path.join(intermediate_results_path,filename + "_DAPI.tif")) it saves the non thresholded image.

Here is my function:

def BFExport(imp, savepath):
    """Export images using Bio-Formats
    
    Parameters
    ----------
    imp : ImagePlus
        ImagePlus to save
    savepath : str
        Path where to save the file
    """

    print('Savepath: ', savepath)

    paramstring = "outfile=[" + savepath + "] " + "windowless=true compression=Uncompressed saveROI=false"
    plugin = LociExporter()
    plugin.arg = paramstring
    exporter = Exporter(plugin, imp)
    exporter.run() 

    return None

Any ideas why ?

Thanks !

Hi @lguerard, I believe this is a bug in the Bio-Formats Exporter which is not saving the applied settings. I have created a GitHub Issue for this: https://github.com/ome/bioformats/issues/3520

As it is a standard Tiff file it might be worth trying to save using the built in ImageJ macro functions rather than the Bio-Formats Exporter and hopefully the settings will be retained.

Hi @dgault,

Thanks for your reply ! True that I could actually save it with the built in Tiff exporter, that worked.

Just to be sure I understand correctly, threshold values for a mask is a settings in Bio-Formats ? It’s not creating a new image which is a binary mask ? Thanks for the explanation :slight_smile:

Sorry for the confusion, there is an Mask shape for ROIs within the OME Model (https://docs.openmicroscopy.org/ome-model/6.0.1/developers/roi.html) but in this scenario Bio-Formats would be creating a new image.

I originally had assumed that this would have been the same issue that we had seen previously when exporting an image after applying LUT, but having gone through it again in this scenario it should actually have worked OK. I managed to test the sample code you listed above and it seemed to save the modified duplicate image, was it imp1_mask you were exporting or imp1_bg_removed?

Below is the code I used for testing:

from loci.plugins import BF
from ij import IJ
from loci.plugins import LociExporter
from loci.plugins.out import Exporter
base_id = id = "/Path/To/FileName";
id = base_id + ".tiff";
imps = BF.openImagePlus(id);
imp1_bg_removed = imps[0];
imp1_mask = imp1_bg_removed.duplicate();
middle_slice = int(imp1_mask.getNSlices() / 2);
imp1_mask.setSlice(middle_slice);
IJ.setAutoThreshold(imp1_mask, "Otsu dark stack");
ImProc = imp1_mask.getProcessor();
lower_thr = ImProc.getMinThreshold() - 20;
upper_thr = ImProc.getMaxThreshold();
IJ.log("lower : " + str(lower_thr) + " ; upper : " + str(upper_thr));
IJ.setThreshold(imp1_mask, lower_thr, upper_thr);
IJ.run(imp1_mask, "Convert to Mask", "background=Dark black");
imp1_bg_removed.show();
imp1_mask.show();

savepath= base_id + "_DAPI.tiff";
paramstring = "outfile=[" + savepath + "] " + "windowless=true compression=Uncompressed saveROI=false";
plugin = LociExporter();
plugin.arg = paramstring;
exporter = Exporter(plugin, imp1_mask);
exporter.run();