//Global Variables
var orig = ""; 			//stores name of picture

// for createTable()
var title1 = 0;			//Name of Results Table
var f=0;					//Result Table


// for folderChoice() 
var filecount = 0;		// how many files are there?
var filestoprocess = newArray(1000000);		//file names to process

resetImageJ();

createTable(); // creates the results table
folderChoice(); // run it first time

start = getTime();
filestoprocess = Array.trim(filestoprocess, filecount);
		
for (z=0; z<filestoprocess.length; z++) {

	roiManager("reset");
	
	showProgress(z/filestoprocess.length);

	open(filestoprocess[z]);

	ori = getTitle();

	// get image dimensions
	hgt = getHeight();
	wth = getWidth();

	//Selecting nothing just in case
	run("Select None");

	// set line tool
	setTool(4);     //Line Tool = 4

	// get user input
	message= "Line Selection Required\n Please create a break line";
	waitForUser(message);
	getLine(x1, y1, x2, y2, lineWidth);

	dx = x2-x1; // change in x
	dy = y2-y1; // change in y

	if (dx == 0) {
		// slope is infinity
		// y intercept is impossible
		// x is always the same
		xAty0point = x1;
		xAtyMaxpoint =  x1;
	} else {
		// calculate slope and intercept
		slope = dy / dx;
		intercept = slope * x1 - y1;
		// calculate x at y=0 and y=imgheight
		xAty0point = abs((0-intercept) / slope); // -y = 0
		xAtyMaxpoint =  ((hgt+intercept) / slope); // -y = hgt
	}

	// make polygon to the right of main line
	makePolygon(xAty0point, 0, xAtyMaxpoint, hgt, wth, hgt, wth, 0);

	// add to manager and its inverse
	roiManager("Add");
	run("Make Inverse");
	roiManager("Add");
	run("Make Inverse");

	// ROI[0] should be right side of line
	// ROI[1] should be left side
	run("Select None");

	// duplicate image
	run("Duplicate...", "title=Right");

	// fill in left side of line with blue and segment on right side
	roiManager("Select", 1);
	setForegroundColor(0, 0, 255);
	run("Fill", "slice");
	run("Select None");

	// find "black" pixels 
	selectWindow("Right");
	findblackpixels(); 
	run("Create Selection");
	roiManager("Add");
	selectWindow("Right");
	run("Close");

	// fill in right side and focus on left
	selectWindow(ori);
	run("Duplicate...", "title=Left");
	roiManager("Select", 0);
	setForegroundColor(0, 0, 255);
	run("Fill", "slice");
	run("Select None");

	// find "black" pixels 
	findblackpixels(); 
	run("Create Selection");
	roiManager("Add");
	selectWindow("Left");
	run("Close");

	// get measurements
	selectWindow(ori);
	run("Select None");
	roiManager("Select", 2); // Right side
	run("Measure");
	run("Select None");
	roiManager("Select", 3); // Left side
	run("Measure");
	run("Select None");
	
	//Print results for each wing
	print(f, ori + "\t" + getResult("Area", 0) + "\t" + getResult("Area", 1));
	
	// clear results table for next image
	run("Clear Results");
	
	//reset ROIs for netting/stripes/shell diagnostics
	roiManager("reset");
						
	// close unnecessary image windows
	run("Close All"); 

	selectWindow("Results Table (results in px)");
	saveAs("Text", "Results.temp"); // saves after every file to prevent data loss
	
} // end of image loop	
			
exit("Finished!");	

///////////////////////////////////////
function resetImageJ() {
	run("Options...", "iterations=1 count=1 edm=Overwrite");
	run("Line Width...", "line=1");
	run("Colors...", "foreground=black background=white selection=yellow");
	run("Clear Results");
	run("Close All");
	print("\\Clear");
	run("ROI Manager...");
	run("Input/Output...", "jpeg=75 gif=-1 file=.csv use_file copy_row save_column save_row");
	run("Set Measurements...", "area mean standard modal min centroid center perimeter bounding fit shape feret's integrated median skewness kurtosis area_fraction stack redirect=None decimal=3");
}
///////////////////////////////////////

///////////////////////////////////////////////////////
function folderChoice() {

	path = getDirectory("Choose a folder of images to analyze"); 

	filelist = getFileList(path);
	for (z=0; z<filelist.length; z++) {
		if (endsWith(filelist[z],"JPG")) {
			filestoprocess[filecount] = filelist[z];
			filecount++;
		}
		if (endsWith(filelist[z],"jpg")) {
			filestoprocess[filecount] = filelist[z];
			filecount++;
		}
	}
var count = (count+1);

}// end of Folderchoice Function
///////////////////////////////////////////////////////

////////////////////////////////////////
function createTable() {

// creates a custom results table or clears the current open if open
  title1 = "Results Table (results in px)";
  title2 = "["+title1+"]";
  f = title2;
  if (isOpen(title1))
     print(f, "\\Clear");
  else
     run("Table...", "name="+title2+" width=800 height=200");
print(f, "\\Headings:File\tWingBlackAreaRight\tWingBlackAreaLeft");
} // end of function Createtable()
//////////////////////////////////////////

///////////////////////////////////////
function findblackpixels() {
	// Color Thresholder 1.48v
	// Autogenerated macro, single images only!
	min=newArray(3);
	max=newArray(3);
	filter=newArray(3);
	a=getTitle();
	run("HSB Stack");
	run("Convert Stack to Images");
	selectWindow("Hue");
	rename("0");
	selectWindow("Saturation");
	rename("1");
	selectWindow("Brightness");
	rename("2");
	min[0]=0;
	max[0]=75;
	filter[0]="pass";
	min[1]=0;
	max[1]=255;
	filter[1]="pass";
	min[2]=0;
	max[2]=150;
	filter[2]="pass";
	for (i=0;i<3;i++){
	  selectWindow(""+i);
	  setThreshold(min[i], max[i]);
	  run("Convert to Mask");
	  if (filter[i]=="stop")  run("Invert");
	}
	imageCalculator("AND create", "0","1");
	imageCalculator("AND create", "Result of 0","2");
	for (i=0;i<3;i++){
	  selectWindow(""+i);
	  close();
	}
	selectWindow("Result of 0");
	close();
	selectWindow("Result of Result of 0");
	rename(a);
	// Colour Thresholding-------------
}
///////////////////////////////////////

--
ImageJ mailing list: http://imagej.nih.gov/ij/list.html