Snippets

Dave Mason Image Feature Measurement from http://forum.imagej.net/t/image-features-measurement-in-batch-mode-not-working/11284

Created by Dave Mason
//@ File (label = "Input directory", style = "directory") input
//@ File (label = "Output directory", style = "directory") output
//@ String (label = "File suffix", value = ".tif") suffix

//-- Call the initial function to get a list of files

processFolder(input);
print("Done");

//-- FUNCTIONS ---
    function processFolder(input) {
    	list = getFileList(input);
    	list = Array.sort(list);
    	for (i = 0; i < list.length; i++) {
    		if(endsWith(list[i], suffix))
//-- Call the file processing function
    			processFile(input, output, list[i]);
    	}
    }

    function processFile(input, output, file) {
    print("Processing: " + input + File.separator + file);
    
    //-- Open the file
    open(input+File.separator+file);
    title=getTitle();

    //-- Your code here -------------------
    //setAutoThreshold("Default");
    //setThreshold(245, 255);
	
		// Shape Analysis Macro for ImageJ
		// Copyright of Liu, E.J., Cashman, K.V., & Rust, A.C., (2015). Optimising Shape Analysis to quantify volcanic ash morphology. GeoResJ

		// INPUTS: this macro requires a binary image.

		// OUTPUTS: Particle area, Particle perimeter, Convex hull (CH) area, Convex hull (CH) perimeter, Solidity, Convexity, 
		// Concavity Index, Form Factor, Major axis (of best fit ellipse), Minor axis (of best fit ellipse), Axial ratio (minor/major axis), 
		// Bounding box width (along x axis), Bounding box height (along y axis), Feret diameter, Minimum Feret diameter. All mathematical 
		// definitions given in Table 2 of Liu et al., (2015).

		// Default variables: particle size range = 750-Infinity pixels per particle; 'exclude' edge-touching particles; 
		// 'include' holes in the particle area.

		{
		run("Set Measurements...", "area centroid perimeter bounding fit shape feret's redirect=None decimal=3");
		run("Analyze Particles...", "size=750-Infinity pixel circularity=0.00-1.00 show=Outlines exclude clear include record stack");


			n = nResults;
			area1 = newArray(n);
			length1 = newArray(n);
			area2 = newArray(n);
			length2 = newArray(n);
			xstart = newArray(n);
			ystart = newArray(n);
			ff1 = newArray(n);
			AR1 = newArray(n);
			round1 = newArray(n);
			BBX1 = newArray(n);
			BBY1 = newArray(n);
			major1 = newArray(n);
			minor1 = newArray(n);
			feret1 = newArray(n);
			minferet1 = newArray(n);
			solidity1 = newArray(n);
			for (i=0; i<n; i++) {
			  area1[i] = getResult("Area", i);
			  length1[i] = getResult("Perim.", i);
			  xstart[i] = getResult("XStart", i);
			  ystart[i] = getResult("YStart", i);
			ff1[i] = getResult("Circ.", i);
			AR1[i] = getResult("Aspect", i);
			round1[i] = getResult("Round", i);
			BBX1[i] = getResult("Width", i);
			BBY1[i] = getResult("Height", i);
			minor1[i] = getResult("Minor", i);
			major1[i] = getResult("Major", i);
			feret1[i] = getResult("Feret", i);
			minferet1[i] = getResult("MinFeret", i);
			}
			run("Clear Results");
			for (i=0; i<n; i++) {
			  doWand(xstart[i], ystart[i]);
			  run("Convex Hull");
			  run("Measure");
			  area2[i] = getResult("Area", i);
			  length2[i] = getResult("Perim.", i);
			}
		run("Clear Results");
			run("Select None");
			for (i=0; i<n; i++) {
			  setResult("Area", i, area1[i]);
			  setResult("Perim.", i, length1[i]);
			  setResult("CH Area", i, area2[i]);
			  setResult("CH Perim.", i, length2[i]);
			  setResult("Solidity", i, area1[i]/area2[i]);
			  setResult("Convexity", i, length2[i]/length1[i]);
			  setResult("Concavity Index", i, sqrt((pow(1-(area1[i]/area2[i]),2)+(pow(1-(length2[i]/length1[i]),2)))));
			  setResult("FormFactor", i, ff1[i]);
			  setResult("Major axis", i, major1[i]);
			  setResult("Minor axis", i, minor1[i]);
			  setResult("Axial ratio", i, minor1[i]/major1[i]);
			  setResult("BX width", i, BBX1[i]);
			  setResult("BY height", i, BBY1[i]);
			  setResult("Feret d", i, feret1[i]);
			  setResult("MinFeret d", i, minferet1[i]);
			}
			 updateResults();
		  }
		  

    //-- Save out the results table here:
    selectWindow("Results");
    saveAs("Results", output+File.separator+replace(file,suffix,"_results.csv"));
	//run("Read and Write Excel");

    //-- Save out your drawing here
    selectWindow("Drawing of "+title);
    saveAs("TIFF",output+File.separator+replace(file,suffix,"_drawing.tif"));
	
	//-- Close everything before the next image is opened
    close("*");
    }

Comments (0)

HTTPS SSH

You can clone a snippet to your computer for local editing. Learn more.