Macro to measure cell intensity

Hello:

Background

I have been looking in the forum for a macro or a way to measure the mean intensity of a folder with several images.
I am stuck in the process to make the macro adding every roi measure into one table result. I have another piece of macro that saves singles cvs file but with hundred of images is taking a lot of time taking one by one to merge and analyze ( I do not know how to use R or Python to do that)

Sample image and/or macro code

macro "intensity" {

  path    = getDirectory("Choose a Directory");
  list    = getFileList(path);
  length  = list.length;

  File.makeDirectory(path + "Intensity/");

  //Split for analysis

  for (i=0; i<length; i++) {
             
    n=i+1;
    open(path+list[i]);
    img = File.nameWithoutExtension();
    selectWindow(img+".tiff");

run("Set Measurements...", "area mean redirect=None decimal=4");
run("StackReg", "transformation=Translation");
run("Subtract Background...", "rolling=300 stack");
run("Split Channels"); 
selectWindow("C3-" + list[ i ]);
rename("FarRED");
selectWindow("C1-" + list[ i ]);
rename("GFP");
run("Duplicate...", "title=mask");
    run("Threshold...");
    setAutoThreshold("Otsu dark");
    waitForUser("Imagen "+n+" de "+length,"Apply Threshold y click OK");
    run("Convert to Mask");
    run("Divide...", "value=255");
    saveAs("Tiff", path + "Intensity/mask 000" + i);    
imageCalculator("multiply create 32-bit", "FarRED", "mask 000"+i+".tif");
selectWindow("Result of FarRED");
  run("ROI Manager...");
    waitForUser("add ROI, Draw ROI, then hit OK");
    roiManager("Add");
    roiManager("Show All");
    roiManager("Multi Measure");
    roiManager("Save", path+"Intensity/" + IJ.pad(i,1) + "ROI.zip");
    
   // Now loop through each of the new results, and add the filename to the "Filename" column
    for (row = prevNumResults; row < nResults; row++)
    {
        setResult("Filename", row, filename);
    }
saveAs("results",outputFile);
// Show a dialog to allow user to save the results file
outputFile = File.openDialog("Save results file");
// Save the results data
saveAs("results",outputFile);
saveAs("Tiff", path+"Intensity/" + IJ.pad(i,1)+ "FarRED");
    run("Close All");

}

I uploaded 2 files in for testing in tif , although I will do this in .lsm file to save more time, I just can change the beggining of my macro to do that.

Analysis goals

  1. analyze fluorescence intensity with an ROI of every cell
    I tried adding analyze particle but it creates thousands of ROI instead of single cells.
    For that, I am just adjusting authotheshold and making ROI in every cell of interest after calculation.

  2. I want to have every ROI with the mean ,area value in a single cvs or xls file

  3. I Want to loop in a folder

Challenges

Part of the macro is doing the job but the export to result and ROI is giving me a problem.

I tried to check other macros in the forum to do the same but I could not find one that I can adapt to my files or with my current macro.

Any help I will really appreciate.
Thank you in advance

Hi @Pablo_M,

I noticed you started a new topic, which may help because the title is more to the point you try to solve. On the other hand, your other post also contains valuable information for others to help you, so it is handy if you had included a link to it.

Anyways, you summed up your analysis goals nicely but you are a bit vague on where you get stuck. If you use the Debugger, where do you get stuck, and what is the error message? Do the contents of the variables match you expectations? Posting this information would help better to help you; better than just stating

is giving me a problem.

It is easier for you to Debug if you would remove the first and last line (macro…{ and matching } and check your indents. Once the code works, just wrap the code in a function intensity(){} or macro "intensity"{}. block.

Proper indentation quickly reveals potential problems. Hint: line 53 } closes line 11’s {, not the first line’s {…

When you step through your code, you can inspect the variables and see how each command changes the content of a variable. soon you will find one that is not defined; filename in line 44 is one of them as far as I can tell. Would you have stepped your code, you would have found out.

And in lines 31 and 32 you might also want to use IJ.pad(), like you did in lines 39 and 51, instead of "mask 000"+i+ or do you have a specific purpose for the current naming scheme and always want to start that mask number with 000?

Here is the proper indented version of you macro:

Indented macro
macro "intensity" {

	path = getDirectory("Choose a Directory");
	list = getFileList(path);
	length = list.length;

	File.makeDirectory(path + "Intensity/");

	//Split for analysis

	for (i=0; i<length; i++) {
		n=i+1;
		open(path+list[i]);
		img = File.nameWithoutExtension();
		selectWindow(img+".tiff");

		run("Set Measurements...", "area mean redirect=None decimal=4");
		run("StackReg", "transformation=Translation");
		run("Subtract Background...", "rolling=300 stack");
		run("Split Channels"); 
		selectWindow("C3-" + list[ i ]);
		rename("FarRED");
		selectWindow("C1-" + list[ i ]);
		rename("GFP");
		run("Duplicate...", "title=mask");
		run("Threshold...");
		setAutoThreshold("Otsu dark");
		waitForUser("Imagen "+n+" de "+length,"Apply Threshold y click OK");
		run("Convert to Mask");
		run("Divide...", "value=255");
		saveAs("Tiff", path + "Intensity/mask 000" + i);		
		imageCalculator("multiply create 32-bit", "FarRED", "mask 000"+i+".tif");
		selectWindow("Result of FarRED");
		run("ROI Manager...");
		waitForUser("add ROI, Draw ROI, then hit OK");
		roiManager("Add");
		roiManager("Show All");
		roiManager("Multi Measure");
		roiManager("Save", path+"Intensity/" + IJ.pad(i,1) + "ROI.zip");
		
		 // Now loop through each of the new results, and add the filename to the "Filename" column
		for (row = prevNumResults; row < nResults; row++)
		{
			setResult("Filename", row, filename);
		}
		saveAs("results",outputFile);
		// Show a dialog to allow user to save the results file
		outputFile = File.openDialog("Save results file");
		// Save the results data
		saveAs("results",outputFile);
		saveAs("Tiff", path+"Intensity/" + IJ.pad(i,1)+ "FarRED");
		run("Close All");
	}

And here is the same version, with line numbers for discussion:

Samesame with line numbers
 1 macro "intensity" {
 2 
 3 	path = getDirectory("Choose a Directory");
 4 	list = getFileList(path);
 5 	length = list.length;
 6 
 7 	File.makeDirectory(path + "Intensity/");
 8 
 9 	//Split for analysis
10 
11 	for (i=0; i<length; i++) {
12 		n=i+1;
13 		open(path+list[i]);
14 		img = File.nameWithoutExtension();
15 		selectWindow(img+".tiff");
16 
17 		run("Set Measurements...", "area mean redirect=None decimal=4");
18 		run("StackReg", "transformation=Translation");
19 		run("Subtract Background...", "rolling=300 stack");
20 		run("Split Channels"); 
21 		selectWindow("C3-" + list[ i ]);
22 		rename("FarRED");
23 		selectWindow("C1-" + list[ i ]);
24 		rename("GFP");
25 		run("Duplicate...", "title=mask");
26 		run("Threshold...");
27 		setAutoThreshold("Otsu dark");
28 		waitForUser("Imagen "+n+" de "+length,"Apply Threshold y click OK");
29 		run("Convert to Mask");
30 		run("Divide...", "value=255");
31 		saveAs("Tiff", path + "Intensity/mask 000" + i);		
32 		imageCalculator("multiply create 32-bit", "FarRED", "mask 000"+i+".tif");
33 		selectWindow("Result of FarRED");
34 		run("ROI Manager...");
35 		waitForUser("add ROI, Draw ROI, then hit OK");
36 		roiManager("Add");
37 		roiManager("Show All");
38 		roiManager("Multi Measure");
39 		roiManager("Save", path+"Intensity/" + IJ.pad(i,1) + "ROI.zip");
40 		
41 		 // Now loop through each of the new results, and add the filename to the "Filename" column
42 		for (row = prevNumResults; row < nResults; row++)
43 		{
44 			setResult("Filename", row, filename);
45 		}
46 		saveAs("results",outputFile);
47 		// Show a dialog to allow user to save the results file
48 		outputFile = File.openDialog("Save results file");
49 		// Save the results data
50 		saveAs("results",outputFile);
51 		saveAs("Tiff", path+"Intensity/" + IJ.pad(i,1)+ "FarRED");
52 		run("Close All");
53 	}
1 Like

Debugger in ImageJ1 is in Plugins>New>Macro, then paste code in the window that opens. Debug is then a new menu in the menu bar.
For FIJI, the Debugger is in Plugins>Macros>Interactive Interpreter...

Thanks again for your precious comments, I am learning a lot. I will go through the debugger to see my mistakes.