// //---------------------------- ImageJ Barrel/Pincushion Correction Macro Tool ------------------------------- var barrelV=170430; // ImageJ macro corrects images and stacks for barrel/pincushion distortion using SplineDeformationGenerator // SplineDeformationGenerator only works on greyscale images; colour images will be converted to greyscale // // Install ImageJ with Java from https://imagej.nih.gov/ij/download.html and update it from its Help menu // Copy this "Barrel Distortion.txt" file to your ImageJ\macros\toolsets folder and click Help/Refresh Menus // Copy the jar file from "http://imagej.net/Spline_Deformation_Generator\" to ImageJ\Plugins & restart ImageJ // // (1) Click the >> icon in the ImageJ toolbar and select the Barrel Distortion tool // (2) Right-click the Barrel Distortion tool icon that appears and set the K1 and K2 distortion parameters // (3) Left-click the Barrel Distortion tool icon to correct images and eventually save the corrected versions // The K1/K2 parameters must be determined by trial and error by correcting the distorted image of a regular grid // Try K1=0.06 K2=0.0 with the example image stack "http://neutronoptics.com/downloads/Bstack.tif" // Email if you have questions or suggestions // var K1 = call("ij.Prefs.get", "captureTool.K1","0.06"); //K1 for lens correction var K2 = call("ij.Prefs.get", "captureTool.K2","0.0"); //K2 for lens correction var Correction=""; //Correction is plugin macro 'NeutronOptics Distortion (right click to set options, left click to execute) Action Tool - T0d18(Tgd18)T4h22+' //String defining the icon { setBatchMode(true); List.setCommands; //Check if Spline Correction installed if (List.get("SplineDeformationGenerator ")!="") //Name of original spline correction Correction="SplineDeformationGenerator "; //Use original Spline Correction if (List.get("Spline Deformation Generator")!="") //Name of latest spline correction Correction="Spline Deformation Generator"; //Use latest Spline Correction if(Correction=="") //Spline correction not found {showMessage("Please copy \"http://imagej.net/Spline_Deformation_Generator\" jar file to Plugins"); exit;} imageDir=getInfo("image.directory"); //Get the orig image directory if(imageDir=="") {run("Save"); imageDir=getInfo("image.directory");}; //Save the image if not saved fileName=getInfo("image.filename"); //Get the orig file name iExt=indexOf(fileName, ".")+1; //Get image file extension imageForm=substring(fileName, iExt); //Determine image format File.makeDirectory(imageDir+"\\tifs"); //Make temporary tiffs dir run("16-bit"); //Convert to greyscale saveAs("TIF",imageDir+"\\tifs\\temp.tif"); //Correction only works with TIF n=nSlices; //Number of slices if a stack close("*"); //Close all images for (i=1; i1) run("Images to Stack", "name='Corrected-"+fileName+"' title=[] use keep");//Create stack with open slices close("temp*"); //Close all corrected slices if(n==1) open(imageDir+"\\tifs\\temp1.tif"); //Re-open single corrected image setBatchMode(false); //Show original & corrected images open(imageDir+fileName); //Re-display original image if(n==1) selectWindow("temp1.tif"); else selectWindow("Corrected-"+fileName); //Select corrected image for (i=1; i