-1

I want to write a macro for Image J using Java I have an error in the first line when trying to use the images from the specific folder, I don´t know how to solve it.

Source code:

for (File file : new File("E:\\Ghent\\magnel\\Bjorn\\cap 1").listFiles()) {

  if (file.isFile() && file.getName().toLowerCase().contains("cap")) {
    //imageCalculator("Subtract create", "cap run_0001.fits","DCBB.fits");
    imageCalculator("Subtract create",file.getName(),"DC.fits")
    //run("Image Calculator...", "image1=[cap run_0001.fits] operation=Subtract image2=DCBB.fits create");

    imageCalculator("Divide create", "Result of cap","DEN");
    //run("Image Calculator...", "image1=[Result of cap] operation=Divide image2=[DEN] create");
    saveAs(file.getName() . "_AD", "C:\\Users\\Nati\\Desktop\\prueba macro\\"_AD".tif");
Mateusz Sroka
  • 2,057
  • 1
  • 15
  • 19
NOBDBBB
  • 1
  • 2
  • What is the error that you are getting?Also, see if file.getAbsolutePath() works for you – Limit Jan 25 '16 at 14:00
  • This appears when I try to run the macro in ImageJ´.´ expected in line 1 – NOBDBBB Jan 25 '16 at 14:23
  • @NOBDBBB Not very expert in ImageJ, but can strings be concatenated as they are in `saveAs(file.getName() . "_AD", "C:\\Users\\Nati\\Desktop\\prueba macro\\"_AD".tif");` ? – Linuslabo Jan 25 '16 at 15:22

2 Answers2

0

The problem should be the use of \. Try with / or \\.

e.g.

new File("E:/Ghent/magnel/Bjorn/cap 1")

Backslash is a special character in Java and many other languages, as you can read in java docs.

Linuslabo
  • 1,350
  • 20
  • 30
0

You are mixing Java and ImageJ1 macro language in your code.

The error occurs because you are trying to run the code as an ImageJ macro, but the first line contains Java syntax.

You have two options:

  • Convert your file system logic into IJ1 macro as well. See here for an example how to process a folder with files.
  • Compile and run your code as a Java plugin. You can use the Macro recorder in Java mode to get the required commands, e.g. for the Image Calculator:

    ImageCalculator ic = new ImageCalculator();
    ImagePlus imp1 = IJ.openImage(file.getName());
    ImagePlus imp2 = WindowManager.getImage("DC.fits");
    ImagePlus imp3 = ic.run("Subtract create", imp1, imp2); 
    
Jan Eglinger
  • 3,876
  • 1
  • 22
  • 49