Skip to content

Instantly share code, notes, and snippets.

@imagejan
Created October 27, 2016 10:32
Show Gist options
  • Save imagejan/e92f7323890e454eca937e4202861a96 to your computer and use it in GitHub Desktop.
Save imagejan/e92f7323890e454eca937e4202861a96 to your computer and use it in GitHub Desktop.
A javascript to measure a grid-like spot pattern in ImageJ
importClass(Packages.ij.IJ);
importClass(Packages.ij.measure.Measurements);
importClass(Packages.ij.measure.ResultsTable);
importClass(Packages.ij.plugin.filter.ParticleAnalyzer);
importClass(Packages.ij.plugin.frame.RoiManager);
importClass(Packages.java.lang.Double);
var diameter = 6000.0;
function findMin(rt, colName) {
var min = 0;
var currMin = null;
for (var j = 0; j < rt.getCounter(); j++) {
val = rt.getValue(colName, j);
if ((currMin == null) || (currMin > val)) {
currMin = val;
min = j;
}
}
return min;
}
var imp = IJ.getImage();
var width = imp.getWidth() * imp.getCalibration().pixelWidth;
var height = imp.getHeight() * imp.getCalibration().pixelHeight;
IJ.run(imp, "Select None", "");
IJ.setAutoThreshold(imp, "Otsu dark");
//IJ.run("Set Measurements...", " center redirect=None decimal=3");
//IJ.run(imp, "Analyze Particles...", " display clear");
var rt2 = new ResultsTable();
var pa = new ParticleAnalyzer(ParticleAnalyzer.SHOW_NONE, Measurements.CENTER_OF_MASS, rt2, 0, Double.POSITIVE_INFINITY); // try rt = null?
pa.analyze(imp);
//rt = ResultsTable.getResultsTable();
for (var i=0; i<rt2.getCounter(); i++) {
var xm = rt2.getValue("XM", i);
var ym = rt2.getValue("YM", i);
rt2.setValue("upperLeft", i, Math.pow(xm, 2) + Math.pow(ym, 2));
rt2.setValue("upperRight", i, Math.pow(width - xm, 2) + Math.pow(ym, 2));
rt2.setValue("lowerLeft", i, Math.pow(xm, 2) + Math.pow(height - ym, 2));
}
var a1 = findMin(rt2, "upperLeft");
var a24 = findMin(rt2, "upperRight");
var f1 = findMin(rt2, "lowerLeft");
var rm = RoiManager.getInstance();
if (rm==null) var rm = new RoiManager();
//rt = ResultsTable.getResultsTable();
//if (rt==null) rt = new ResultsTable();
rm.runCommand("Deselect");
if (rm.getCount() > 0) rm.runCommand("Delete");
rm.runCommand("Show All with labels");
rm.runCommand("Show All");
var xCenterA1 = rt2.getValue("XM", a1);
var yCenterA1 = rt2.getValue("YM", a1);
var xCenterA24 = rt2.getValue("XM", a24);
var yCenterA24 = rt2.getValue("YM", a24);
var xCenterF1 = rt2.getValue("XM", f1);
var yCenterF1 = rt2.getValue("YM", f1);
var colArray = [0,1,2,3, 4.5,5.5,6.5,7.5, 9,10,11,12, 13.5,14.5,15.5,16.5, 18,19,20,21, 22.5,23.5,24.5,25.5];
var rowArray = [0, 1.5,2.5,3.5,4.5, 6.0];
var colHeaders = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24];
var rowHeaders = ["A", "B", "C", "D", "E", "F"];
var colStepX = (xCenterA24 - xCenterA1) / 25.5;
var colStepY = (yCenterA24 - yCenterA1) / 25.5;
var rowStepX = (xCenterF1 - xCenterA1) / 6.0;
var rowStepY = (yCenterF1 - yCenterA1) / 6.0;
IJ.run("Set Measurements...", " mean integrated display redirect=None decimal=3");
for (row = 0; row < rowArray.length; row++) {
for (col = 0; col < colArray.length; col++) {
x = xCenterA1 + colArray[col] * colStepX + rowArray[row] * rowStepX;
y = yCenterA1 + colArray[col] * colStepY + rowArray[row] * rowStepY;
IJ.run(imp, "Specify...", "width=" + diameter + " height=" + diameter + " x=" + x + " y=" + y + " oval constrain centered scaled");
roi = imp.getRoi();
roi.setName("" + rowHeaders[row] + colHeaders[col]);
rm.addRoi(roi);
//IJ.run(imp, "Measure", "");
//rt.addLabel("" + rowHeaders[row] + colHeaders[col]);
}
}
//rt.show("Results");
rm.runCommand("Deselect");
rm.runCommand("Measure");
IJ.run("Labels...", "color=white font=8 show use draw");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment