Using colocalisation threshold in Java

Tags: #<Tag:0x00007fb882c7f6b8> #<Tag:0x00007fb882c7f320>


Hi Daniel,
I saw you are one of the maintainers of the Coloc2 plugin.
I am trying to run some of the plugin’s functionality via a java class.
I have a problem with running the Colocalisation_Threshold’s run method.
By looking at the method’s signature I can see it gets a string as a method, but what is supposed to be written in that string?
I tried to search a documentation or a forum solution to my problem, but haven’t found an answer yet.

I have managed to run the plugin as a macro on imagej, but I don’t want to rely on the macro as its functionality is quite limited (just saving an excel file is hell).
Thanks in advance,



Hi @hilasha2
Joe would you prefer the output formatted?
Comma separated key value pairs.?
I didn’t think it was specifically excel format… If it is and I agree yhat’s not open enough, tell us what would work for you.

Not sure the plug in is meant to be driven like you try to use it. What isn’t it doing that you need or vice versa?


Thank you all for the quick reply @chalkie666, @etarena!!
I wasn’t in the lab today, but tomorrow I will be and I’ll re-post my question publicly on the main forum with my code samples.
As for now, I’ll answer your, @chalkie666, questions (w/o the codes though):
I was tasked to do an automation of colocalization calculations on several TIFF files located in the same directory.
In order to do so, I thought about either writing a Java code and/or an ImageJ’s macro script.
I have struggled to run the colocalization’s “run” method both on the macro code and the Java code.
My main problem was not knowing which input arguments I should pass to the “run” method. After seeing several examples, I have realized what I should do with the macro, and it worked.
However, I still have a problem with the Java code.

Later on I save the results of the colocalization calculation in an excel file.
While the macro script currently works, I still prefer working with the Java code. The main reason is that I’ll have to do String parsing of the TIFF files’ titles (the images’ names). Also, it is easier to write to an excel file with Java than with ImageJ’s macros.
I saw that someone wrote an excel macro for ImageJ but it doesn’t work on my macro.

I will add my codes tomorrow as I don’t have them with me currently.


ImageJ’s macro:

dir = getDirectory("Choose a Directory");
files = getFileList(dir);
 for(i = 0; i + 2 <  files.length; i = i + 3){
    open(dir + files[i]);
    imp1 = getTitle();
    open(dir + files[i+2]);
    imp2 = getTitle();
    run("Colocalisation Threshold",  "channel_1=[imp1] channel_2=[imp2]  <None> [Red : Green] use include") ;
    run("Close All");

dir = getDirectory("Choose a Directory");
saveAs("results", dir+"results.xslx");

Java’s code:

import ij.IJ;
import ij.ImagePlus;

public class Colocalisation_Impl {
    public static void main(String args[]) {
        String path = IJ.getDir("Choose a Directory");
        File dir = new File(path);
        File[] filesNames = dir.listFiles();
        int numFiles = filesNames.length;
        for(int i = 0; i + 2 < numFiles;  i += 3) {
            ImagePlus imp1 = new ImagePlus(filesNames[i].toString());
            String imp1Title = imp1.getTitle();
            ImagePlus imp2 = new ImagePlus(filesNames[i+2].toString());
            String imp2Title = imp2.getTitle();
            Colocalisation_Threshold coloc = new Colocalisation_Threshold();
  "channel_1=[imp1Title] channel_2=[imp2Title]");
            //Save ij.log
            //Clear ij.log


Some clarifications regarding the Java code:

  • I put ij.jar in the dependencies of the project.
  • I copied the classes Colocalisation_Threshold.class, and Colocalisation_Test.class into the src path where Colocalisation_Impl is.

Later on I’ll want to parse the image’s titles and I believe it would be easier doing so with Java.
Also, is there a better way of saving the files in an excel file? I’ve tried using different methods, but what I wrote in the macro above is the only one that works for me.
I tried to follow these instructions, but it doesn’t work:




A new release of Coloc 2 added some more scripting functionality:

And here is an older Forum post with some example code inside to show how to get individual measurements from Coloc 2 via scripting, etc.:

And finally - there is an example of how to script programmatic execution of Coloc 2 here. Just go to Script Editor > Templates > Examples > Colocalisation (Groovy).

Hope this helps! Not sure if it exactly answers your question at the moment… but is informative none-the-less.

eta :slight_smile:

ALSO: I usually save my results as .tsv files … perhaps give it a try?


Thank you for your reply! I will try to implement what you responded and update with results.


Thank you all for your comments! Unfortunately, due to time constraints, I might not code in Java. However, if I do get back to this project, I will update my results on this post.