//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 then make mask by "Default Dark" method
//run("Threshold...");
		run("Gaussian Blur...", "sigma=1 stack");
		run("Make Binary", "method=RenyiEntropy background=Dark calculate black");
		
//make mask as having only 0 or 1
		run("Divide...", "value=255.0000 stack");

//adding mask on GFP image
	imageCalculator("Multiply create 32-bit stack", "GFP","mask");

//adding mask on RFP image
	imageCalculator("Multiply create 32-bit stack", "RFP","mask");

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

//Sum projection of the masked-GFP stack
	selectWindow("Result of GFP");
		run("Z Project...", "projection=[Sum Slices] all");
	selectWindow("Result of GFP");
		//close();

//Sum projection of the masked-RFP stack
	selectWindow("Result of RFP");
		run("Z Project...", "projection=[Sum Slices] all");
	selectWindow("Result of RFP");
		//close();

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

		for(i = 0; i < slices; i++) {
			currentslice = i + 1;
			selectWindow("SUM_Result of RFP");
				setSlice(currentslice);
				run("Measure");
			}
			
		for(i = 0; i < slices; i++) {
			currentslice = i + 1;
			selectWindow("SUM_Result of GFP");
				setSlice(currentslice);
				run("Measure");
			}
			
	selectWindow("SUM_Result of GFP");
		close();
	selectWindow("SUM_Result of RFP");
		close();

	//setBatchMode("exit and display");