Modified ratio calculation of two ROIs within one image

Dear list,

I am looking for a way to calculate the intensity ratio out of two ROIs within one image (see exemplary image below). As my goal will be to calculate the polarization anisotropy out of those two ROIs, I moreover don’t want to calculate only a usual ratio like r=I1/I2 but rather a modified ratio like r=(I1-I2)/(I1+2*I2) with I1 and I2 being the pixel intensities of both ROIs. As I wish to do this while live recording using MicroManager, I want to avoid to just duplicate both ROIs as single images and doing the calculation afterwards.
So far, I’ve tried using the Ratio Plus plugin but this seems to not be able to calculate a ratio out of two ROIs instead of out of two images. Also, I couldn’t find a possibilty to modify the ratio-equation the way I wish to.
I hope any of you might have some helpful tips on this issue.

what you want may be best achieved by a little macro. You may use the macro recoder for some steps.

Furthermore it would be handy if you know the coordinates of both ROIs!

If you don’t want to write an ImageJ-plugin but still need high speed processing with an ImageJ-macro, then you willl have to copy at least the contents of one ROI to another image frame and then use the appropriate operations of the “Image Calculator”.



For your formula

By suboptimally assuming currently that you would draw the ROIs initially manual the following macro might do the job.

//first create the two ROIs you want to compare and add them to the ROI manager
roiManager("select", 0);
getStatistics(area1, mean1);
roiManager("select", 1);
getStatistics(area2, mean2);

r = (mean1 - mean2) / (mean1 + (2*mean2));

The ROIs could also be defined automatically but this might take more time than you want it to take since you mentioned that you need this on the fly.

This macro does it only once on one image. If you need this to run until you stop it we could try the following:

while(!isKeyDown("space")) {
	roiManager("select", 0);
	getStatistics(area1, mean1);
	roiManager("select", 1);
	getStatistics(area2, mean2);
	r = (mean1 - mean2) / (mean1 + (2*mean2));

This ends calculating your ratio as soon as you press “space”

@anon96376101 you can use “@name” to reference people and notify them that there is something specifically awaiting their attention.

Note that this topic is cross posted with the ImageJ mailing list.

@anon96376101 can you explain the deficiencies and/or necessary changes to @biovoxxel’s macro?

Thanks so far.
But yes, @anon96376101, you are right, I wasn’t looking for the mean values.
Also, I just noticed that I didn’t especially mention before that I would like to receive an image - like the Ratio Plus plugin will give it - showing the ratio. At some point, it would be good for me to have the possibility to actually record the ratio-images as a stack also.
Do you think it will be possible at all to write a macro that will do that or do you know of any existing plugins that can do something like that?
Thank you!
Lara (no noname any more :wink: )

1 Like

@Herbie I agree, that besides it was not directly stated that @lgentemann wants the mean value the question might be interpreted in both ways and as such my macro was a first suggestion assuming she needs a ratio based on the mean value. Obviously that would not solve the need for an output image with individual pixel ratios using her formula.

@Herbie and just as a comment to the comment… according to some netiquette it would also be nice to rather simply explain the reason why something is not solving a question the intended way instead of just putting something down as not useful while inferring that others would not carefully read or trying to properly suggest something.

btw: If you do not like to use avatars, one solution is sometimes to look up user names by clicking on the avatars icon and adressing them that way with the stated name if wished so.

1 Like

Dear Lara,

back to the initial topic.

A crucial question is still hanging:
"[…] it would be handy if you know the coordinates of both ROIs"

Are these coordinates available?

Did you have a look at the “Image Calculator”-functionality yet?

"Do you think it will be possible at all to write a macro that will do that […]"
Of course!



Hi @lgentemann,

referring to the Image Calculator as mentioned by @anon96376101 the following small macro (which should first only serve as a potential guide to get it close to what you need in the end) will give you at least one of your ratio images for now. However, so far this needs two already defined ROIs in the ROI manager. Since ROI definition is up to you, I did not include this already. Therefore, it would be good to extend it by the automatic definition of your ROIs and as @anon96376101 suggested, therefore you need to have a defined coordinate and the intended ROI size. This is critical to not calculate any shifted ratio.

original = getTitle();

roiManager("select", 0);
run("Duplicate...", "title=roi1");
roi1 = getTitle();

roiManager("select", 1);
run("Duplicate...", "title=roi2");
roi2 = getTitle();

//A = (I1 - I2)
imageCalculator("Subtract create 32-bit", roi1, roi2);
result1 = getTitle();

//B = 2*I2
run("Multiply...", "value=2");

//C = I1 + 2*I2
imageCalculator("Add 32-bit", roi1, roi2);

// A / C
imageCalculator("Divide 32-bit", result1, roi1);

Hope it helps a little as a starting point.

P.S. the “Image Expression Parser” in Fiji would also be a possibility but I fear it is not macro recordable