input=getDirectory("Please choose directory with T1w and/or T2w images. Must be titled appropriately.");
ref_voxel=newArray(0.1,0.1,0.1);
ref_canvas=newArray(260,350,260);
if(File.exists(input+"T1w_cor/")){
File.openSequence(input+"T1w_cor/");
scale_to_ref(ref_voxel,ref_canvas);
if(File.exists(input+"T2w_cor/")){
File.openSequence(input+"T2w_cor/");
scale_to_ref(ref_voxel,ref_canvas);
if(File.exists(input+"T2w_sag/")){
File.openSequence(input+"T2w_sag/");
scale_to_ref(ref_voxel,ref_canvas);
if(File.exists(input+"T1w_ax/")){
File.openSequence(input+"T1w_ax/");
scale_to_ref(ref_voxel,ref_canvas);
if(File.exists(input+"T2w_ax/")){
File.openSequence(input+"T2w_ax/");
scale_to_ref(ref_voxel,ref_canvas);
//T1w 0020,0032 Image Position (Patient): 10.962077\7.420000\19.931641
//T2w 0020,0032 Image Position (Patient): 10.539853\7.510000\19.931406
//z: 4 slices "deeper" in T1W
//Pixel dimensions: T1W has more X-pixels but fewer Z-slices. Same Y-pixels
//Sizing: T1W has physically more of the mouse in Y (appear to line up at top though)
//T2W CLAIMS to be 0.110x, 0.117Y (insignicant)
//T1W CLAIMS to 0.1058X,0.1367Y.
//DELETE first 4 slices of T2W--BOTTOM seems to be ref. coordinate
//Ref: TOP of Y, BOTTOM of Z
function scale_to_ref(ref_voxel,ref_canvas){
// mouseName=0010,0010 Patient's Name: DE1689
meta_array=split(meta,"\n");
//for loop which applies for each image set, SCANS metadata
//orientation of the image, i.e. ax, cor or sag, but there may be some slight angles/adjustments so not always perf 90 degrees offset
//first 3 columns give you row values
//sagittal: "0020,0037 Image Orientation (Patient): 0.30\-0.95\-0.00\0.00\0.00\-1.00"
//coronal"0020,0037 Image Orientation (Patient): -0.95\-0.30\-0.00\0.00\-0.00\-1.00"
if(startsWith(meta_array[i],"0020,0037 Image Orientation (Patient)")){
data=split(meta_array[i],":\\");
print("---here is the data output-----");
//patient position - THIS MIGHT BE THE MACHINE COORDINATES? : //axial example: 0020,0032 Image Position (Patient): 12.99\15.29\8.42; //coronal example: 0020,0032 Image Position (Patient): 14.80\-4.74\13.69 ; saggital example: 0020,0032 Image Position (Patient): -3.29\15.04\20.92
if(startsWith(meta_array[i],"0020,0032 Image Position (Patient)")){
data=split(meta_array[i],":\\");
x_position=parseInt(data[1]);
y_position=parseInt(data[2]);
z_position=parseInt(data[3]);
if(startsWith(meta_array[i],"0018,0088 Spacing Between Slices")){ //Slice Thickness???
data=split(meta_array[i],":");
z_scaling=parseFloat(data[1]);
if( startsWith(meta_array[i],"0028,0030 Pixel Spacing")){
data=split(meta_array[i],":\\");
y_scaling=parseFloat(data[1]);
x_scaling=parseFloat(data[2]);
if(startsWith(meta_array[i],"0018,1310 Acquisition Matrix")){
print(name,meta_array[i]);
if(startsWith(meta_array[i],"0010,0010 Patient's Name")){
print(name,meta_array[i]);
//PatientID=meta_array[i]]
if(startsWith(meta_array[i],"0008,0012 Instance Creation Date")){
print(name,meta_array[i]);
setVoxelSize(x_scaling,y_scaling,z_scaling,"mm");
run("TransformJ Scale", "x-factor="+x_scaling/ref_voxel[0]+" y-factor="+y_scaling/ref_voxel[1]+" z-factor="+z_scaling/ref_voxel[2]+" interpolation=[Quintic B-Spline] preserve");
//axial matrix = "-1,-0,-0,0,-1,0"
//sag matrix = "0,-1,-0,0,0,-1"
//cor matrix = "-1,-0,-0,0,-0,-1"negative zeros are NOT maintained by the parseInt function above;
if(matches(rowX+","+rowY+","+rowZ+","+colX+","+colY+","+colZ, "0,-1,0,0,0,-1")){ //
run("TransformJ Turn", "z-angle=0 y-angle=90 x-angle=0"); //y 90 for sag
//add little regostration marker in corner
setColor(255, 165, 0); // Orange
run("Overlay Options...", "stroke=none fill=orange draw");
//run("Flatten", "stack");
if(matches(rowX+","+rowY+","+rowZ+","+colX+","+colY+","+colZ, "-1,0,0,0,-1,0")){
run("TransformJ Turn", "z-angle=0 y-angle=0 x-angle=270"); // z 0; y90 looks sag now... , x =90? right
setColor(255, 165, 0); // Orange
run("Overlay Options...", "stroke=none fill=orange draw");
//run("Flatten", "stack");
//cor rotate or just add label
if(matches(rowX+","+rowY+","+rowZ+","+colX+","+colY+","+colZ, "-1,-0,-0,0,-0,-1")){
// //run("TransformJ Turn", "z-angle=0 y-angle=0 x-angle=0"); // no rotation as it is here - if you need to, adjust code in this line
setColor(255, 165, 0); // Orange
run("Overlay Options...", "stroke=none fill=orange draw");
//run("Flatten", "stack");
run("Canvas Size...", "width="+ref_canvas[0]+" height="+ref_canvas[1]+" position=Top-Center zero");
setSlice(round(nSlices/2));
setMetadata("Info", meta);
//add little marker in corner
makeText("R, H", 215, 20);
setColor(255, 165, 0); // Orange
run("Overlay Options...", "stroke=none fill=orange draw");
//flatten so the label follows any further stack manipulations
//waitForUser("keep registration label?", "flatten?");