 # Ratiometric images - how to modify intensities based on calibration curve data

Hi,

I have collected ratiometric data and I have created a divided image. I now need to modify the intensities in the image based on a calibration curve equation:

y = a((xˆb)+c); where a, b and c are constants I have values for; x is this current pixel intensity; and y will be the new modified intensity.

I am very new to imageJ - can someone please give me a simple explanation of how I can do this.

Jo

Hi jamjojo,
welcome to the forum. Asking here is a good start.

Beside the forum and a lot of other very good information sources the ImageJ User Guide is important to know.

What you are looking for is described in 29.9.19 Macro…

The function can be found in the ImageJ menu
Process/Math/Macro

There you can simply type in your formular, e.g.:
v=1.3*((v^2.9)+3.4)

If you will use this formular many a time then it will good to create a macro:

``````run("Macro...", "code=[v=1.3*((v^2.9)+3.4)]");
``````

The macro can be written a bit more sophisticated as

``````a = 1.3;
b = 2.9;
c = 3.4;
run("Macro...", "code=[v=" +a+ "*((v^" +b+ ")+"  +c+ ")]");
``````

Hope this helps.

Another way is given in this older thread of the mailing list where you find an macro example which you can easily adapt if you need more flexibility:

http://imagej.1557.x6.nabble.com/Calibrating-density-and-creating-calibrated-image-td3692091.html

Dear phaub,

thank you for your quick reply. I realised that I had posted the wrong equation (I should have rearranged to solve for X). it should have been:

x = ((a/y)-c)^(1/b) or v=((a/v)-c)^(1/b)

I have used the macro - thanks for pointing this out - however the pixel values output in the new image are too high. I have checked the equation by hand and it is good - is there any obvious issues with the equation I have input (shown above)? I am currently trying to modify a macro to see if this resolves things.

thanks

jo

Hi Bio7,

thanks for the advice on the macro. I am giving this a go now. I have modified the one in the link you sent but I am stuck trying to find where I can load it in and execute it from inside imagej. any advice here would be great.

thanks

jo

Create a new macro `Plugins->New->Macro` and paste your code.

Execute the macro in the editor menu with: `Macros->Run Macro`

See:

https://imagej.nih.gov/ij/docs/guide/146-14.html#sub:Macros-ExtendingIJ

thanks -I can run the script now but it is giving me the same issues as when I ran the formula through Process/Math/Macro.

the experiment is a ratiometric measurement. If I take the average pixel value from a box region before I run the macro, I get say “1.7”. This would manually (based on the equation below) be converted to about “5.3”. But when I run the macro and measure the same box region again I am getting “410397894”. Do you have any ideas what I may be doing wrong?

many thanks!

macro being used:

macro “Calibration” {
// a…c are the coeficients of the calibration reverse sigmoidal.
a=60556740
b=17246700
c=10

w1 = getWidth(); h1 = getHeight();
for (i=0; i<w1; i++) {
for (j=0;j<h1; j++) {
vpixel=getPixel(i,j);
cpixel=((a/vpixel) - b)^(1/c);
// Negative values are equaled to 0
if (cpixel<0) cpixel=0;
setPixel(i,j,cpixel);

};
showProgress((i/w1));
};
print(“Calibration curve applied”);
};

Use Math.pow() instead of `^`

For Math functions, see:

https://imagej.nih.gov/ij/developer/macro/functions.html#M

Math.pow(base, exponent)
Returns the value of base raised to the power of exponent .

Same as:

pow(base, exponent)
Returns the value of base raised to the power of exponent .

1 Like

@Bio7 is right.

``````v=pow((60556740/v)-17246700, 0.1)
``````

should work.
(Note: image type: 32bit)

thank you @Bio7 and @phaub. this is very helpful and I can get the function to run. the numbers are now close but still off a little (numbers should range between 4.8 to 6.8 but instead are all between about 6 and 7). Is this an issue with pow? I have read online that it can introduce errors.

thanks

Well, this is hard to analyze without a reproducible example. So if you post an example for the miscalculation (an image from the box region) it would help.

Normally two very frequent errors are rounding errors in the calculation path or the deficit of
floating point arithmetics, see:

https://docs.oracle.com/cd/E19957-01/806-3568/ncg_goldberg.html

I have attached a region of the original image and another image highlighting 2 boxed regions within this showing the miscalculation. thanks again for any help you can provideexample-region.tif (516.4 KB) example.tif (2.6 MB)

Unfortunately you didn’t store the *tif with the required selection (ROI) to reproduce the miscalculation.

Please store the image with the ROI (store with current selection - and reopen it for control) and then calculate the summary for the ROI for posting it here so that we can compare the results on a different computer.

Also you boxes (selections before and after calculation - e.g., box 1 and box 2) have different areas! Please make sure you use the same selections (ROI’s).

It might be helpful to use the ROI Manager for reproducibility:

Dear Bio7,

sorry for the delay in responding to your previous message. I have save 2 different ROIs before and after running the macro (I think this is what you requested). I have attached these along with a CSV for reading from these ROIs. I have also added another column at the end of the CSV where I have calculated the expected value after running the macro by hand.

please let me know if you need anything else.

thanks

Results.csv (202 Bytes) ROI1-after.tif (16.3 KB) ROI1-before.tif (16.3 KB) ROI2-after.tif (16.3 KB) ROI2-before.tif (16.3 KB)

No, this is not what I requested.

You only sent images without a saved selection. Instead, you sent the original image and then the cropped results.

You have to save the original image with a selection (has to be active) in ImageJ.

Or:

It might be helpful to use the ROI Manager for reproducibility:

https://imagej.nih.gov/ij/docs/guide/146-30.html#sub:ROI-Manager…

The ROI’s of the ROI Manager can be stored, too. Please consult the documentation.
There are also some basic YouTube Videos available.

my apologies. I have now attached a region of the image I am analysing and also 3 ROI files from within this image. I have also attached the image with ROI boxed regions annotated. I hope this is correct.

many thanks

image+roi.zip (1.4 MB)

Now you have to measure the ROI’s before and after (as you’ve done in a previous post) so that we can compare the results since my results (before) are different so it seems the ROI’s are now different.

Also send the image information of the original image (Press ‘i’).

thanks - I have now attached the ROI measurements before and after implementing the macro. I have also attached the macro I am running and the image information.

image-info.zip (2.6 KB)

It seems you now measured a completely different ROI. The csv-results you sent here are not from the ROI’s from the post before (image+roi.zip - area= 3172).

You must sent all data (images, ROI’s, results, expected values from manuel calculation).

But apart from this I found your original equation different from the macro equation.

In one of the first post your equation was:

x = ((a/y)-c)^(1/b) or v=((a/v)-c)^(1/b)

which does not reflect the macro code equation.

So please control the equation first (you can post it, too). You might have swapped something.