A problem with writing a macro code for using calculator plus pluging, divide operation in image J


I wrote a code to divide some images that I have to background images with the same name as my original images. I have two problems with the code below:

  1. When I use this code, my outputs are exactly the same name of my original files, not that one I want and have written in this code. ( I want to have “_ subtracted” in the continuation of the name)

  2. When I divide my images one by one with calculator plus ( not macro) I get different results ( I mean the gray values I get from using macro and using calculator plus directly are different). What is the problem? my calculations are very sensitive to the gray value change.

macro "Batch calculate images [1]" {
LocationOfFiles = getDirectory("Select Folder");
LocationOfbackgrounds = getDirectory("Select Folder of backgrounds");
LocationOfSave = getDirectory("Select Save Location");
FileList1 = getFileList(LocationOfFiles);
FileList2 = getFileList(LocationOfbackgrounds);
NumberOfFiles = FileList2.length;
for (i=0; i<NumberOfFiles; i+=1) {
  FileName1 = FileList1[i];
  pathtofile1 = LocationOfFiles+FileName1;
  name1 = getTitle();
  FileName2 = FileList2[i];
  pathtofile2 = LocationOfbackgrounds+FileName2;
  name2 = getTitle();
  run("Calculator Plus", "i1="+name1+" i2="+name2+" operation=[Divide: i2 = (i1/i2) x k1 + k2] k1=255 k2=0 create");
  SaveName = replace(name1,"tiff","-subtracted.tiff");
  saveAs(".bmp", LocationOfSave+SaveName);

Maybe you are aware of this, but just in case: division with the image_calculator_plus and the image_calculator are not the same thing.
Have a look at the source code of IJ and the image_calculator_plus.

1 Like

But I used calculator plus for both my macro and manual analysis and got different results

Sorry I misunderstood your post.
Can you post an example with a shorter macro that shows the difference?
Are you sure that the FileList1 and FileList2 return the correct pairs of images?
Maybe it would be safer to get the first image list and then select the divisor image based on the name of the numerator image.