Skip to content

Instantly share code, notes, and snippets.

@miura
Created September 13, 2012 14:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save miura/3714672 to your computer and use it in GitHub Desktop.
Save miura/3714672 to your computer and use it in GitHub Desktop.
Using TurboReg from ImageJ macro, to output shifts.
var npos = 0; // this variable is better be global.
macro "CCS_APP" {
ipos = 1; // imaged position currently processed and "number of the file"
// npos = 0; // number of total imaged positions
ch = 0; // channel, standardmäßig auf 0!
frame = 0;
slice = 0;
noofcells = 4; // needs to be loaded from segmenting algorithm.
roicounter=-1; // counter for roiManager;
// -------------- Start of actual code ----------------
dir = getDirectory("Please choose a directory containing the .dv files.");
dv_file_counter(dir);
npos = 1; // this is just for the program not to run through all 4 files during test phase. Remove later.
for (ipos =1 ; ipos <= npos; ipos++) //Image importer loop.
{
run("Bio-Formats Importer",
"open=[" + dir + "/pos" + ipos + ".dv]"
+ " autoscale"
+ " color_mode=Grayscale"
+ " split_channels"
+ " view=Hyperstack"
+ " stack_order=XYCZT");
// channel with better overall signal should be chosen for processing!
// How to chose?
//Answer: I think the best would be to use virtual stack when you are opening the file via Bio-Formats.
measure_driftcorrect("pos" + ipos +".dv - C=0");
for (f = 0; f<2; f++){
apply_driftcorrect("pos" + ipos +".dv - C=" + f);
//close();
}
}
} // end Macro
// ------ function definitions -------
// functions could be outside the macro.
function dv_file_counter(dir)
{
list = getFileList(dir);
for (i = 0; i < list.length; i++)
if (endsWith(list[i], ".dv") == true)
npos++;
if (npos <= 0)
exit("No *.dv files in chosen directory.");
else
write(npos + " *.dv files found in chosen directory.");
} // end of function
function measure_driftcorrect(dcstack) // add batchmode=true arg? make arg for output of drift-corrected window?
{
setBatchMode(true);
if (isOpen(dcstack) != true) //check that target-window open
exit("The stack you want to correct for drift is not open!");
else
{
selectWindow(dcstack);
Stack.getDimensions(w, h, chn, sln, frn);
dcresultsx = newArray(frn+1);
dcresultsy = newArray(frn+1);
//go to central Z-postition
cenSl = sln/2; // dirty here! needs to be rounded up! cenSl: central slice
write("Moving to slice " + cenSl + " of " + sln);
Stack.setSlice(cenSl);
run("Reduce Dimensionality...", " frames keep"); // das neue Window heißt wie das alte, nut mit einem "-1" dahinter
dcstack_reduced = dcstack + "-1";
selectWindow(dcstack_reduced); // select new window with the reduced timepoint
Stack.setFrame(0); // set to first time frame - for StackReg as reference
write("Starting driftcorrect on position " + ipos + ", slide " + cenSl);
//selectWindow(dcstack_reduced);
//run("Duplicate...", "title=driftcorrected duplicate range=1-" + frn);
for (frame = 0; frame <= frn; frame++)
{
selectWindow(dcstack_reduced);
Stack.setFrame(frame);
run("Duplicate...", "title=sourceimage");
frame1 = "sourceimage"; // a frame in a sequence
selectWindow(dcstack_reduced);
Stack.setFrame(frame + 1); // select next frame.
run("Duplicate...", "title=targetimage");
frame2 = "targetimage"; // next frame in the sequence
selectWindow(frame1);
width = getWidth();
height = getWidth();
run("TurboReg ",
"-align "
+ "-window " + frame1 + " "// Source (window reference).
+ " 0 0 " + (width - 1) + " " + (height - 1)
+ " -window " + frame2 + " "// Target (window reference).
+ " 0 0 " + (width - 1) + " " + (height - 1)
+ " -translation"
+ " " + (width / 2) + " " + (height / 2)
+ " " + (width / 2) + " " + (height / 2)
+ " -hideOutput"
);
sourceX0 = getResult("sourceX", 0); // First line of the table.
sourceY0 = getResult("sourceY", 0);
targetX0 = getResult("targetX", 0);
targetY0 = getResult("targetY", 0);
xoffset = sourceX0 - targetX0; // calculate offsets in respect to previous image.
yoffset = sourceY0 - targetY0;
xoffsetsum = xoffsetsum + xoffset; // x and yoffsetsums: values each frame has to be aligned in respect to first frame.
yoffsetsum = yoffsetsum + yoffset;
dcresultsx[frame] = xoffsetsum; // saving results for each frame in arrays
dcresultsy[frame] = yoffsetsum;
print("Frame " + frame + " - x: " + xoffsetsum + " y: " + yoffsetsum);
/*
* selectWindow("driftcorrected");
* Stack.setFrame(frame);
* run("Translate...", "x=" + xoffsetsum + " y=" + yoffsetsum + " interpolation=None slice");
*/
selectWindow(frame1);
close();
selectWindow(frame2);
close();
} // closes for (frame = 0; frame <= driftcorfrn; frame++);
} //closes else
print("Done calculating drift in position " + ipos);
run("Clear Results"); // transferring results to results-table
for (i = 0; i < dcresultsx.length; i++)
{
setResult("Frame", i, i);
setResult("X-offsetsum", i, dcresultsx[i]);
setResult("Y-offsetsum", i, dcresultsy[i]);
}
updateResults();
selectWindow("Results");
saveAs("Results", dir + "/dirftcorPos" + ipos + ".xls");
// saves resultstable as a file.
} // end of function
function apply_driftcorrect (targetstack)
{
selectWindow(targetstack);
Stack.getDimensions(w, h, chn, sln, frn);
for (frame = 0; frame <= frn; frame++) // frame <= frn!!
{
corx = getResult("X-offsetsum", frame); // load corrected x-position from results-table
cory = getResult("Y-offsetsum", frame); // load corrected y-position
for (slice = 0; slice <= sln; slice++) // shift all slices of one frame
{
selectWindow(targetstack);
Stack.setPosition(0, slice, frame);
run("Translate...", "x=" + corx + " y=" + cory + " interpolation=None slice");
}
}
} // end of function
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment