2014-10_flymacro.ijm

/*
this macro was written for:
FIJI ImageJ 1.47i 
by Emily Jane Dennis 
October, 2014

to automatically analyze videos taken of individual Drosophila melanogaster walking around on 3d printed agarose dishes and determine how much time they spend on one side of the petri dish or the other

the macro takes each image, thresholds it, and counts how many times a fly is on one side- then inverts the selection and counts the other side

requires a folder of images to be analyzed

to edit: check out commented sections, but particularly pay attention to the proper coordinates of the circle

good luck!
*/

//ask user to find the image stack
	directory = getDirectory("Choose a Directory");
	run("Image Sequence...", "open=[" + directory + "]");
//set a variable title that is the title of your imported image stack
	title = getTitle();
	setTool("line");
	waitForUser("select center line angle to rotate image by choosing a point on LEFT and then on the RIGHT of it");
{getLine(x1, y1, x2, y2, lineWidth);
      getPixelSize(unit, width, height, depth);
      x1*=width; y1*=height; x2*=width; y2*=height; 
      angle = getAngle(x1, y1, x2, y2);


function getAngle(x1, y1, x2, y2) {
      q1=0; q2orq3=2; q4=3; //quadrant
      dx = x2-x1;
      dy = y1-y2;
      if (dx!=0)
          angle = atan(dy/dx);
      else {
          if (dy>=0)
              angle = PI/2;
          else
              angle = -PI/2;
      }
      angle = (180/PI)*angle;
      if (dx>=0 && dy>=0)
           quadrant = q1;
      else if (dx<0)
          quadrant = q2orq3;
      else
          quadrant = q4;
      if (quadrant==q2orq3)
          angle = angle+180.0;
      else if (quadrant==q4)
          angle = angle+360.0;
      return angle;
  }
	run("Rotate... ", "angle=" + angle + " grid=1 interpolation=Bilinear stack");
//select the dish and remove the background
//setTool("oval");
	makeOval(253, 113, 237, 234);
	run("Make Inverse");
	run("Fill", "stack");
//threshold the image so only the dark animal is 'seen' by the program
//run("Threshold...");
	setThreshold(0, 50);
//select a rectangular area that contains the bottom half of the petri dish, count # times fly is on that side, then save the results
//setTool("rectangle");
	makeRectangle(238, 224, 294, 161);
	run("Analyze Particles...", "size=10-400 circularity=0.00-1.00 show=[Overlay Outlines] display summarize stack");
	saveAs("Results", directory + title + "bottom.xls");

/*
this section selects the opposite side, if you don't want to count the center line, make this a rectangular selection by commenting out the code from run("Make Inverse") and run("Analyze Particles..." until saveAs() and un-comment out the below code:
*/

//uncomment here to select a rectangle instead of the inverse
//setTool("rectangle");
//	makeRectangle(212, 83, 296, 144);
//	run("Analyze Particles...", "size=10-400 circularity=0.00-1.00 show=[Overlay Outlines] display summarize stack");
	run("Make Inverse");
	run("Analyze Particles...", "size=10-400 circularity=0.00-1.00 show=[Overlay Outlines] display clear summarize stack");

//comment out above if you prefer the rectangle	
	saveAs("Results", directory + title + "top.xls");
	run("Select All");
	run("Analyze Particles...", "size=10-400 circularity=0.00-1.00 show=[Overlay Outlines] stack");
	saveAs("Jpeg", directory + title + "summary.jpg");
run("Close");
run("Close");
run("Close");
close()