Error in Redirect command in Analyaze>Set Measurement

Hi all,

I have a macro that works fine for counting small foci and biq plaques (with other settings for size and circularity).
However, in my summary table I don’t get the true ‘mean’ and ‘median’ values. From what I have read this is because in the Analyze > Set Measurement tool I need to redirect it to the image being processed. I cannot figure out how to do this, so now it’s set to redirect=None which obviously doesn’t give me the correct value (at least I could already get correct results for “count” etc)

This is my script:

title = getTitle();
run("Split Channels");
selectWindow(title + " (green)")
run("Set Measurements...", "area mean standard min median display **redirect =None** decimal=2");
setAutoThreshold("Default dark");
//run("Threshold...");
setThreshold(35, 255);
//setThreshold(35, 255);
setOption("BlackBackground", true);
run("Convert to Mask");
run("Analyze Particles...", "size=100-Infinity pixel display exclude clear include summarize add")

Thank you for your time and help!

1 Like

You may want to look at posts like this:


Also, it helps to format your scripts as code rather than as unformatted text, using the </> button in the format bar.

Dear Marlies,
The problem in your code is that the run(“Convert o Mask”) line doesn’t create a new image, but alters the existing image. So, by the time you try to measure your particles, they are all white on a black background. Also, redirect doesn’t seem to work for measurements carried out as part of the Analyze Particles… command.

Below is a slightly modified version of your code that rather than splitting the channels, duplicates channel 2. It then uses that channel to create your ROIs and uses the ‘Multi Measure’ command in the ROI manager to carry out the measurements on channel 2 of the original image (similar to the post mentioned above).

Hope this helps,
Volko

title = getTitle();
run("Duplicate...", "title="+title+" (green) channels=2");
//selectWindow(title + " (green)")
run("Set Measurements...", "area mean standard min median display redirect =none decimal=2");
setAutoThreshold("Default dark");
//run(“Threshold…”);
setThreshold(35, 255);
//setThreshold(35, 255);
setOption("BlackBackground", true);
run("Convert to Mask");
run("Analyze Particles...", "size=100-Infinity pixel exclude clear include add")
selectWindow(title);
Stack.setChannel(2);
roiManager("Measure");  //Could also use "multi-measure" if you wanted to measure all channels
run("Summarize");

1 Like

Dear Volko,

I think I get the idea but there’s still something wrong with the code.
Now I get the error: “No RGB image expected in line 5” => this is because I need the black and white Image for tresholding I think?
If I add run (“Split Channels”) between the first and second line however, I get an error in line 14 “No stack images”

Maybe I can move this to after the set threshold command? :
// run(“Set Measurements…”, “area mean standard min median display redirect =none decimal=2”);

thank you!

Hi @Volko
Sorry to bother you again! Do you perhaps know how I can adjust the script?

Many thanks!

Hi @Marlies,
I tested the code that I posted previously with a sample RGB image from the ImageJ list and it seemed to be working fine. Could you post a sample image and the code that you are trying to use as that may help to figure out why it is not working for you.
Cheers,
Volko

Hi @Volko

I get this error message
Memory * 124MB of 12114MB (1%)
nImages() * 2
getTitle() * “ARPE19-IFNy-7dpi-Scene-01-B1-B01-Image”
title * “ARPE19-IFNy-7dpi-Scene-01-B1-B01-Image Export-01_EGFP.tif”


Error: Non-RGB image expected in line 5:
setAutoThreshold ( “Default dark” <)> ;

ARPE19-IFNy-7dpi-Scene-01-B1-B01-Image Export-01_EGFP.tif (8.5 MB)

The script I am using:

title = getTitle();
run("Duplicate...", "title="+title+" (green) channels=2");
//selectWindow(title + " (green)")
run("Set Measurements...", "area mean standard min median display redirect =none decimal=2");
setAutoThreshold("Default dark");
//run(“Threshold…”);
setThreshold(35, 255);
//setThreshold(35, 255);
setOption("BlackBackground", true);
run("Convert to Mask");
run("Analyze Particles...", "size=100-Infinity pixel exclude clear include add")
selectWindow(title);
Stack.setChannel(2);
roiManager("Measure");
run("Summarize");

This is the previous one which still works for “count” but does not give the true values for mean etc.

title = getTitle();
run("Split Channels");
selectWindow(title + " (green)")
run("Set Measurements...", "area mean standard min median display redirect =None decimal=2");
setAutoThreshold("Default dark");
//run("Threshold...");
setThreshold(35, 255);
//setThreshold(35, 255);
setOption("BlackBackground", true);
run("Convert to Mask");
run("Analyze Particles...", "size=100-Infinity pixel display exclude clear include summarize add");

I thought I still needed to add the
run (‘Split Channels"); command
So I tried to merge it in between the second en third line of "your’ script but this doesn’t work.

Cheers,
Marlies

@Marlies you may want to share a sample image, even if it is something like a negative control, just so that the file format/structure (channels, etc) is apparent. I am not sure how one of your channels could be an RGB image still, but maybe something weird is happening there.

Hi @Research_Associate
I think I’ve uploaded an image in the previous message, but here it is again. ARPE19-IFNy-7dpi-Scene-01-B1-B01-Image Export-01_EGFP.tif (8.5 MB)

1 Like

Sorry, you did! My mistake.

It looks like almost all of the information is in the green channel. Does it work to run
run("8-bit");

So something like this:

title = getTitle();
run("Duplicate...", "title="+title+" (green) channels=2");
run("8-bit");
run("Set Measurements...", "area mean standard min median display redirect =none decimal=2");
setAutoThreshold("Default dark");
//run(“Threshold…”);
setThreshold(35, 255);
//setThreshold(35, 255);
setOption("BlackBackground", true);
run("Convert to Mask");
run("Analyze Particles...", "size=100-Infinity pixel exclude clear include add")
selectWindow(title);
//Stack.setChannel(2);
roiManager("Measure");  //Could also use "multi-measure" if you wanted to measure all channels
run("Summarize");

I am not great with ImageJ scripting at the moment, so I kind of hacked together what I think was the intent. I had to comment out the Stack.setChannel since the image was not a stack at that point. If you have other information in your other channels (red/blue) at some later point, it might be important to do this more precisely, by pulling out only the green channel from the RGB.

Hi Marlies,

I see why the macro I posted earlier didn’t work for your images. I assumed that your images where 3 channel images (i.e. a stack of three images, one for each channel). However, your images are RGB images. So, in order for the macro to work, we need to convert your RGB images to 3 channel images, which can be accessed independently (what you tried with the ‘Split Channels’. Your approach would also work, but you would need to create a second copy of the channel you are interested in as the thresholding changes all the image values to either 0 or 255).
Below is my modified macro code with an added run(“Make Composite”) to convert your RGB images to 3 channel images.
This should now work.
Good luck,
Volko

title = getTitle();
run("Make Composite");     //convert RGB image to 3 channel image
run("Duplicate...", "title="+title+" (green) channels=2");  //duplicate the green channel
run("Set Measurements...", "area mean standard min median display redirect =none decimal=2");  //set required measurements
setAutoThreshold("Default dark");   //set threshold parameters
setOption("BlackBackground", true);
setThreshold(35, 255);              //and threshold values
run("Convert to Mask");				//apply threshold
run("Analyze Particles...", "size=100-Infinity pixel exclude clear include add")    //find white particles on thresholded image, add them to ROI manager
selectWindow(title);		//return focus to original image
Stack.setChannel(2);		//set channel for measurement
roiManager("Measure");  	//measure all ROIs; could also use "multi-measure" if you wanted to measure all channels
run("Summarize");			//create summary statistics for results table

Hi @Volko
I very much appreciate your help.
I am however still stumbling upon a problem with this script as the measurements are not summarized / added to the result table.
If I add // run(“Measure”) // at the end of the script, I get the results in the Result table.
However, if I do batch processing with this script, the results are not added to the result table but instead “overwritten”.
Any suggestions on how to overcome this?

Also, if I look at the output pictures I don’t get the masked pictures but the original pictures in the three channels Red, Green, Blue. Is this correct?

Thank you again for your time!

Marlies

Hi Marlies,
The macro only creates a mask for the green channel as that is the one that you wanted to use to create the ROIs. A b/w image of that mask is created and displayed. The macro then goes back to the original images and carries out the measurements for each ROI (I have added a line to show ROIs superimposed on the original image). That was the problem in your original code as you were trying to measure the intensity on the b/w image of the mask.

The last four lines of the results table show the summary data (Mean, Min, Max and SD) for each column for each image. The results table is cleared when you analyse a new image (if you wanted to keep it for each image, you could save it separately). I have now added some extra code to the macro to copy the mean intensity and area together with the image name to a summary table.

Looking at your image, I also noticed a few things:

  • The size calibration of your image is completely out - I very much doubt that the scale is 300 pixels/inch

  • There is no signal in the red channel, and the red and blue signal is perfectly correlated. So, I assume that these are actually originally just grey scale images, which were coloured green and then saved as RGB images. If I am correct, I would strongly suggest to save the original images as greyscale images and then apply an appropriate LUT in ImageJ to give them the desired colour.

  • It looks like the image was stitched together from multiple images and there appear to be some stitching artefacts (see lines indicated by arrows in screenshot below). This may not matter that much for your analysis, but I thought I mention it.
    image

Cheers,
Volko

title = getTitle();
run("Make Composite");     //convert RGB image to 3 channel image
run("Duplicate...", "title="+title+" (green) channels=2");  //duplicate the green channel
run("Set Measurements...", "area mean standard min median display redirect =none decimal=2");  //set required measurements
setAutoThreshold("Default dark");   //set threshold parameters
setOption("BlackBackground", true);
setThreshold(35, 255);              //and threshold values
run("Convert to Mask");				//apply threshold
run("Analyze Particles...", "size=100-Infinity pixel exclude clear include add")    //find white particles on thresholded image, add them to ROI manager
selectWindow(title);		//return focus to original image
Stack.setChannel(2);		//set channel for measurement
roiManager("Show All");		//show ROIs on original image
roiManager("Measure");  	//measure all ROIs; could also use "multi-measure" if you wanted to measure all channels
run("Summarize");			//create summary statistics for results table

meanInt=getResult("Mean",nResults-4);	//read average intensity value from results table
meanArea=getResult("Area",nResults-4)	//read average area value from results table
if(!isOpen("Summary Table")){			//check whether a summary table exists
	Table.create("Summary Table");		//if it doesn't, create one
}else{
	selectWindow("Summary Table");		//if it does, select it
};
Table.set("Image",Table.size, title);			//write image name to new row in summary table
Table.set("Mean Area",Table.size-1, meanArea);	//write mean area value to summary table
Table.set("Mean Int",Table.size-1, meanInt);	//write mean intensity to summary table


2 Likes

Thank you @Volko

  • Images were taken in brightfield and GFP, but I was trying to do the analysis on the GFP image only (channels were already split with ZEN software) so this doesn’t make much sense. I now did as you suggested and converted the GFP image into 8-bit.
  • I did stitching and adjusted the scale

It is working now so thank you!