Skip to content

Instantly share code, notes, and snippets.

@tferr
Created November 17, 2014 20:27
Show Gist options
  • Save tferr/2490bf1c4028fbbbddea to your computer and use it in GitHub Desktop.
Save tferr/2490bf1c4028fbbbddea to your computer and use it in GitHub Desktop.
t-test between two ImageJ1 images
/*
* IJ BAR: https://github.com/tferr/Scripts#scripts
*
* Performs a t-test between two open images (http://thread.gmane.org/gmane.comp.java.imagej/35266) using
* commons-math:
* http://commons.apache.org/proper/commons-math/userguide/stat.html
* http://commons.apache.org/proper/commons-math/apidocs/org/apache/commons/math3/stat/inference/TTest.html
*
*/
import ij.IJ;
import ij.WindowManager;
import org.apache.commons.math3.stat.inference.TTest;
import org.apache.commons.math3.stat.StatUtils;
/*
* Casts any array type (byte[], short[], float[], int[]) into double[].
* This is required as we do not know which image types we'll be dealing with
*/
double[] toDoubleArray(array) {
double[] dArray = new double[array.length];
for (i=0; i<array.length; i++) {
dArray[i] = (double) array[i];
}
return dArray;
}
int[] ids = WindowManager.getIDList();
if (ids==null || ids.length<2) {
IJ.error("At least 2 images required");
return;
}
img1 = WindowManager.getImage(ids[0]);
img2 = WindowManager.getImage(ids[1]);
sample1 = toDoubleArray(img1.getProcessor().getPixels());
sample2 = toDoubleArray(img2.getProcessor().getPixels());
pValue = new TTest().tTest(sample1, sample2);
s1variance = StatUtils.variance(sample1);
s2variance = StatUtils.variance(sample2);
IJ.log("\n*** Two-sample, two-tailed t-test ***");
IJ.log(""+ img1.getTitle() +" vs "+ img2.getTitle());
IJ.log("p-value: "+ pValue);
IJ.log("Image 1 variance: "+ s1variance);
IJ.log("Image 2 variance: "+ s2variance);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment