TurboReg batch processing macro

Hi all,
I’m trying to use TurboReg in a macro to register two photon calcium imaging videos. I use an average projection of the first 100 frames (attached image 1) of a clip as the target image. When I register with batch mode in the interactive version, the clip registers perfectly. However, in macro mode for some reason the result clip becomes blurred in a lot of frames. Does anyone know why this happens, and how I can fix it? Thanks!

Here’s the section of my code for TurboReg registration:


function register(inputdir, outputdir, file){

setBatchMode(true);

nframe = parseInt(File.length(inputdir+file))/524288; //512x512 pixels 16-bit unsigned images- each image is 512 kB

print(“Registering “+inputdir+file+”: “+nframe+” frames”);
currtime = getTime();
print(logfile, currtime+": Registering “+inputdir+file+”: “+nframe+” frames");

run(“Raw…”, “open=”+inputdir+file+" image=[16-bit Unsigned] width=512 height=512 number="+nframe+" little-endian use");
rawfile = getTitle();
regstack = replace(rawfile, “.raw”, “_reg.tif”);
Stack.getDimensions(w, h, chn, sln, frn); //sln is number of frames in our case
run(“Z Project…”, “stop=100 projection=[Average Intensity]”);
avgimg = getTitle();
width = getWidth();
height = getHeight();

run(“Conversions…”, " ");
showProgress(1,sln);
for(k=1; k <=sln; k++) { //for each slice
selectWindow(rawfile);
setSlice(k);
run(“Duplicate…”, “title=currentFrame”);
run("TurboReg “,”-align "
+ "-window currentFrame "// Source
+ "0 0 511 511 " // No cropping.
+ "-window "+ avgimg + " "// Target
+ "0 0 511 511 "// No cropping.
// + "-rigidBody "
// Rigid body requires 3 landmarks
+ "-translation "// Translation requires 1 landmark
+ “256 256 256 256 "
//Landmark 1: translation
// + “256 80 256 80 "
//Landmark 2: angle
// + “256 432 256 432 "
//Landmark 3: angle
+ “-showOutput”);
selectWindow(“Output”);
rename(“outputimg”);
run(“Duplicate…”, “title=registered”);
run(“16-bit”);
if(k==1)
{
run(“Duplicate…”, “title=” + regstack);
selectWindow(“registered”);
close();
}
else{
run(“Concatenate…”, " title=” + regstack + " image1=” + regstack + " image2=registered image3=[-- None --] image4=[-- None --]”);
}
selectWindow(“outputimg”);
close();
selectWindow(“currentFrame”);
close();
showProgress(k+1,sln);
}
selectWindow(regstack);

if (!File.exists(outputdir))
File.makeDirectory(outputdir);
run(“Save”, “save=” + outputdir + regstack);
print("Saving to "+ outputdir + regstack);
close();
selectWindow(avgimg);
close();
selectWindow(rawfile);
close();

setBatchMode(false);
}

This is an average projection of 100 frames from the raw clip

First frame of raw clip (also what a frame in a clip registered in interactive mode looks like)

First frame of clip registered with macro (the white specks all look elongated)