Coloc 2 troubles implementing from java



Hi all,

I am trying to automatically get a correlation output for all the data in an ImageStack.
A quick way to do that seems to be using the coloc 2 plugin.

So what i do is go through the images in the stack. Create new imagePlus objects
load those via the coloc_2 initialiser

// code snippet
Coloc_2 c2 = new Coloc_2();
ImagePlus i1 = new ImagePlus("1",imp.getImageStack().getProcessor(i+1));
ImagePlus i2 = new ImagePlus("1",imp.getImageStack().getProcessor(j+1));
c2.initializeSettings(i1,i2,0,0, false, false, false, false, false, false, false, true, false, false, false, 3,10);
// end of smippet

I then run the code as follows:

Img im1 =ImagePlusAdapter.convertFloat(i1);
Img im2 =ImagePlusAdapter.convertFloat(i2);
try {
} catch (Exception e){

However i get all the results back as seperate windows where i actually just want to read part of the data.
Any ideas on how to do this?




So… I’m not exactly sure what you want in the end… but perhaps these older forum posts will be of use to you. They go over how to access the individual measures, etc:

Perhaps you can be a bit more specific though regarding

What ‘part’ of the data do you want?



I only want to read out the pearson correlation coefficient actually. And thanks for the links, however i do not know how to implement the groovy script. Nonetheless i have found a quick work around by reading in the values from the log. However i would have preferred to be able to run coloc2 without the windows popping up

Anybody has any ideas about that?


You can use the AnalysisResults object returned by the colocalise() method.

The following Groovy script illustrates this (run it from the script editor with two images open, after choosing Language > Groovy):

#@ ImagePlus imp1
#@ ImagePlus imp2

import sc.fiji.coloc.Coloc_2
import net.imglib2.img.display.imagej.ImageJFunctions

c2 = new Coloc_2()

c2.initializeSettings(imp1,imp2,0,0, false, false, false, false, false, false, false, true, false, false, false, 3,10)
i1 = ImageJFunctions.wrapFloat(imp1)
i2 = ImageJFunctions.wrapFloat(imp2)

pearsons = null

result = c2.colocalise(i1, i2, null, null, null)
result.values().each {
	if ("Pearson's R value (no threshold)")) {
		pearsons = it.number
	// println "${}: ${it.number}"

println pearsons

I didn’t find a way to suppress showing the result windows though. Maybe @etarena or @chalkie666 can comment on this.


@imagejan & @Joost_Willemse1

As far as I know… there is no way to ‘suppress’ the windows opening… based on the design of Coloc2 - them opening is tied to the actual calculation of the coloc measures. This is why we are trying to move to those algorithms into Ops … hopefully, the new Colocalize plugin will be coming soon.



Here is an example using the new colocalization ops:

#@ OpService ops
#@ Dataset image1
#@ Dataset image2
#@output double singlePearsons
#@output double pValue
#@output double colocValue
#@output double[] colocValuesArray

// A single Pearsons calculation between two images.
singlePearsons ="coloc.pearsons", image1, image2)

// Now let's compute a p-value of Pearsons over random image shufflings.
// This is much more robust statistically.

// The number of random shufflings for the p-value calculation.
nrRandomizations = 100

// Trim image edges to ensure they divide evenly into blocks.
int[] blockSize = new int[image1.numDimensions()]
for (d = 0; d < blockSize.length; d++) {
	blockSize[d] = Math.sqrt(image1.dimension(d)) as int
import net.imagej.ops.coloc.ShuffledView
import net.imglib2.RandomAccessibleInterval
trimmedImage1 = ShuffledView.cropAtCenter(image1, blockSize)
trimmedImage2 = ShuffledView.cropAtCenter(image2, blockSize)

// Run the Pearsons op a bunch of times, and compute p-value.
colocOp = ops.op("coloc.pearsons", image1, image2)
pValueResult = new net.imagej.ops.coloc.pValue.PValueResult()"coloc.pValue", pValueResult, image1, image2, colocOp, nrRandomizations)

// Harvest the results.
pValue = pValueResult.getPValue()
colocValue = pValueResult.getColocValue()
colocValuesArray = pValueResult.getColocValuesArray()

@etarena put a lot of effort into migrating the existing algorithms from Coloc 2 into ImageJ Ops. The algorithms are the same, so the results should be comparable.

The coloc.pValue op is available with imagej-ops-0.43.0, which is released, but not yet available from the ImageJ Updater. Hopefully I’ll manage to get everything rolled out before I2K, but I cannot promise it. Also keep in mind that all of this is still 0.x, meaning that API is subject to change, and algorithm correctness is not thoroughly battle-tested yet.

Colocalization Highligter