Thresholding live data

Hello everyone,

New to ImageJ/MicroManager and trying to find a way to perform thresholding on live data (and after that perform other functions like Analyze Particles and calculating Distribution). So far, thresholding, analyzing particles and calculating distribution for a stack of images works fine, however, can’t find a way to perform thresholding on live data so that I could perform the same analysis of particles and get the distribution. When I try to threshold live data, the recording stops and returns only one image, although there should be more of them as the recording was on for a while. Am I missing something or thresholding live data is something that is not possible on micro manager/ImageJ? Any suggestions/advice welcome! Thank you.

1 Like

Hi Sen_mat,

This can be done in multiple ways, but there is no super-easy, ready-made solution. Micro-Manager 2.0-gamma has an “On-the-Fly Image Processing Pipeline”. Processing pipeline plugins are handed each image that goes through MM, and can do with it what they like (and produce output that can be the same as the input or altered). Such a plugin could do the live thresholding and output the particle analysis that you are after.

Currently, there are no analysis-only Image Processing plugins, but you could model code on the ImageFlipper plugin: https://github.com/micro-manager/micro-manager/tree/master/plugins/ImageFlipper. Of course, this is only a feasible approach if you feel comfortable writing plugin code in Java. If not, can you elaborate a bit what tools you do like to use?

2 Likes

Hi nicost,

Thank you for your reply. At first I thought, that maybe it would be possible to write a BeanShell script that would take each image from the ongoing recording and set threshold on each one of them; However, when I try running Threshold plugin or just Ij.setAutoThreshold(imp, “Default”) on a snapped image, ImageJ warns that no threshold has been set (although the same script works fine when I use a single image imported into ImageJ). I’m comfortable using Java, therefore will look into the model code you’ve posted. Thanks again.

Update. I made some preliminary work on the plugin. However, thresholding doesn’t quite work. When I set the threshold on the Image Processor object, and later check whether it has been set correctly (with getMinThreshold() and getMaxThreshold()) it shows the correct values. However, the image that is displayed (live or just snapped) on micro manager doesn’t look thresholded and also, when I open ImageJ->Image->Show Info… it says that the image is not thresholded. Am I missing something here? Any help and advice would be appreciated. Thank you. I’ll add the code snippet.

public static Image transformImage(Studio studio, Image image) {

        ImageProcessor proc = studio.data().ij().createProcessor(image);
        setDefaultThreshold(proc);

        double minThreshold = proc.getMinThreshold();
        double maxThreshold = proc.getMaxThreshold();

        // Add metadata that indicates the performed transformations on the image
        PropertyMap.PropertyMapBuilder builder;
        PropertyMap userData = image.getMetadata().getUserData();
        if (userData != null) {
            builder = userData.copy();
        }
        else {
            builder = studio.data().getPropertyMapBuilder();
        }
        builder.putDouble("MinThreshold", minThreshold);
        builder.putDouble("MaxThreshold", maxThreshold);
        Metadata newMetadata = image.getMetadata().copy().userData(builder.build()).build();
        Image result = studio.data().ij().createImage(proc, image.getCoords(), newMetadata);

        return result;
    }

private static void setDefaultThreshold(ImageProcessor proc) {
        if (proc.getMinThreshold() == ImageProcessor.NO_THRESHOLD) {
            ImagePlus img = new ImagePlus("snap", proc);
        
            ImageStatistics statistics = new StackStatistics(img);
            if (statistics.min == 0) {
                statistics.min = 5;
            }

            proc.setThreshold(statistics.min, statistics.max, RED_LUT);
        }
    }