//This is a macro to analyze signal intensity of the GFP and the RFP channel over time by using a mask generated from the RFP channel.



//Don't show pictures on the window until its done
	//setBatchMode(true);

//Obtain titile 
	title = getTitle();

//Split channels to GFP and RFP
	run("Split Channels");

//Prepare for counting pixels
	run("Set Scale...", "distance=1 known=1 pixel=1 unit=pix");

//Generate a stack on the GFP channel
	selectWindow("C1-" + title);
		run("Duplicate...", "title=GFP duplicate");
	selectWindow("C1-" + title);
		close();

//Generate stacks on the RFP channel and mask
	selectWindow("C2-" + title);
		run("Duplicate...", "title=RFP duplicate");
		run("Duplicate...", "title=mask duplicate");
	selectWindow("C2-" + title);
		close();

//making mask using one of the duplicated RFP image
	selectWindow("mask");

//Gaussian Blur 
		run("Gaussian Blur...", "sigma=2 stack");

//Sum projection of the mask stack
	selectWindow("mask");
		run("Z Project...", "projection=[Max Intensity] all");
	selectWindow("mask");
	close();
	
//Working with SUM_mask	
	selectWindow("MAX_mask");

//Make mask by "Default Dark" method
//run("Threshold...");
		run("Make Binary", "method=Default background=Default calculate black");

//Fill holes
		run("Fill Holes", "stack");

//Erode and Dilate
	run("Erode", "stack");
	run("Dilate", "stack");

//make mask as having only 0 or 1
		run("Divide...", "value=255.0000 stack");

//Creat sum projection of GFP and RFP images
	selectWindow("GFP");
		run("Z Project...", "projection=[Sum Slices] all");
	selectWindow("RFP");
		run("Z Project...", "projection=[Sum Slices] all");
	
//adding mask on GFP image and RFP image
	imageCalculator("Multiply create 32-bit stack", "SUM_GFP","MAX_mask");
	imageCalculator("Multiply create 32-bit stack", "SUM_RFP","MAX_mask");


//Closing GFP, RFP and mask stacks
	selectWindow("GFP");
		close;
	selectWindow("RFP");
		close;


//Measuring mean intensity for all the time points
	run("Set Measurements...", "mean redirect=None decimal=4");
	selectWindow("Result of SUM_GFP");
		slices=nSlices;

		for(i = 0; i < slices; i++) {
			currentslice = i + 1;
			selectWindow("Result of SUM_RFP");
				setSlice(currentslice);
				run("Measure");
			}
			
		for(i = 0; i < slices; i++) {
			currentslice = i + 1;
			selectWindow("Result of SUM_GFP");
				setSlice(currentslice);
				run("Measure");
			}
			
	selectWindow("Result of SUM_GFP");
		close();
	selectWindow("Result of SUM_RFP");
		close();
	selectWindow("SUM_RFP");
		close();
	selectWindow("SUM_GFP");
		close();
	selectWindow("MAX_mask");
		close();

	//setBatchMode("exit and display");