CellAndSpotDetection_Batch.ijm 2.02 KB
Newer Older
Christian Tischer's avatar
Christian Tischer committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
dirIn = getDirectory("Choose input directory!");
imFileExtension = getString("What is the ending of your image files?",".tif");
files = getFileList(dirIn);

setBatchMode(true); 
		
for(i=0; i<lengthOf(files); i++) {

	if(endsWith(files[i],imFileExtension)) { 
		
		closeEverything();
		
		print("Working on: "+dirIn+files[i]);
		
		open(dirIn+files[i]); // opens the image
		rename("inputImage"); 
		
		///////////////////////////////////////////
		// Copy and paste your macro code BELOW! //
		/////////////////////////////////////////// 
		
		// find cells
		selectWindow("inputImage"); 
		run("Duplicate...", "title=GaussianBlur"); 	
		run("Gaussian Blur...", "sigma=10");
		run("Find Maxima...", "noise=50 output=[Segmented Particles]");
		rename("Cells");
		selectWindow("GaussianBlur");
		setThreshold(230, 100000); // changed: set upper threshold super high
		setOption("BlackBackground", false);
		run("Convert to Mask");
		rename("Foreground");
		imageCalculator("AND create", "Foreground","Cells");
		run("Analyze Particles...", "size=500-Infinity exclude add"); // changed: put minimum cell size higher
		
		// find spots
		selectWindow("inputImage"); 
		run("Duplicate...", "title=median_bg");
		run("Median...", "radius=5");
		imageCalculator("Subtract create", "inputImage", "median_bg");
		run("Find Maxima...", "noise=50 output=[Single Points]");
		run("Divide...", "value=255");
		rename("spots.tif");
		
		// measure spots and save results
		selectWindow("spots.tif");
		roiManager("Measure");
		saveAs("Results", dirIn+files[i]+"--spot_count.csv"); // changed: used dirIn to construct path
		
	
		///////////////////////////////////////////
		// Copy and paste your macro code ABOVE! //
		///////////////////////////////////////////
		 	
	}

}

///////////////
// functions //
///////////////

function closeEverything() {
	
  // closes all image windows 
  close("*");

  // closes all non-image windows
  list = getList("window.titles"); 
  for (i=0; i<list.length; i++) { 
	winame = list[i]; 
    selectWindow(winame); 
    run("Close"); 
  } 
	
}