Windowless execution of elastix registration in ImageJ macro

Hi all,

I’m having a minor yet annoying issue with the Elastix Wrapper for ImageJ. I’m using it to co-align histology sections with different stainings by registering the masks and then transforming the images accordingly.

The call to Elastix looks as follows:

function Register(FixedImage, MovingImage, Outdir){
	run("Elastix", "elastixdirectory=Y:\\PIP\\Projekte\\Programme\\Elastix "+
		"tmpdir="+Outdir+ " " +
		"fixedimagefile="+ FixedImage + " "+
		"movingimagefile="+ MovingImage+ " "+
		"transformationtype=Similarity "+
		"bsplinegridspacing=50,50,50 "+
		"numiterations=1000 "+
		"numspatialsamples=30000 "+
		"gaussiansmoothingsigmas=10,10,10 "+
		"transformationoutputfile="+ Outdir + "trafo.txt "+
		"outputmodality=None "+
		"usefixedmask=false "+
		"usemovingmask=false "+
		"useinitialtransformation=false "+
		"elastixparameters=Default "+
		"finalresampler=FinalLinearInterpolator "+

Now, when I execute this function, this window pops up:Unbenannt
Here, I have to enter the filenames manually, for some reason they are not taken from the macro call. Any ideas on how to circumvent this?

Thanks in advance!

1 Like

I’m having the same issue! Will see if I can figure it…

Hi @JohnH

Figured it out :slight_smile: I ended up with a solution that seemed much simpler to me (and works completely headless).

  1. You run your registration once through Fiji, which generates you an elastix parameter file that you can use later-on.
  2. You call elastix not as an ImageJ plugin, but externally. In my script, this is how it looks:
// Call elastix
exec(elastix_path    + " "+  
    "-f " +  fixed_image_path +
    " -m " +  moving_image_path +
    " -out "+ output_dir      + " "+
    " -p " + parameter_file);

elastix_path is the path that points to the elastix executable on your PC.
parameter_file is the previously generated elastix parameter file.

Depending on what you set in your parameter file (especially the WriteResultImage property), this will always create a well-designed output-file that you can then load and process further.

Since you register masks of different stainings, you’ll probably not want to save the output images (write (WriteResultImage “false”) in your elastix parameter file for this). Instead, elastix will create a text-file with the transformation parameters, that you can then apply to your original (unmasked) image with transformix. You can call transformix in the same way from a macro:

// call transformix
exec(transformix_path + " " +  
    "-tp " + trafo_file + " " +
    "-out "+ output_dir + " " +
    "-in " + input_image + ".tif");

trafo_file is the file previously created by elastix (usually called TransformParameters.0.txt)


That is brilliant! Thanks for sharing.

I had the same problem and finally figured out how to call the Elastix wrapper from within an IJ macro.
The trick is to declare ALL the parameters in the command line (even those that are not used/empty !), otherwise the wrapper will stop and ask for the parameters. You have to use the correct syntax of course. Also, don’t forget single quotes as the example below. For “empty” parameters, use two single quotes.

run(“Elastix”, “elastixdirectory=‘path to elastix directory’, tmpdir=‘path to output dir’, fixedimagefile=‘path to fixed image’, movingimagefile=‘path to moving image’, transformationtype=‘Affine’, bsplinegridspacing=‘50,50,50’, numiterations=‘1000’, numspatialsamples=‘3000’, gaussiansmoothingsigmas=‘10,10,10’, transformationoutputfile=’/path to transformation file’, outputmodality=‘Save transformed images in working directory as Tiff’, usemovingmask=‘false’, usefixedmask=‘false’, useinitialtransformation=‘false’, initialtransformationfile=’’, fixedmaskfile=’’, movingmaskfile=’’, elastixparameters=‘Default’, finalresampler=‘FinalLinearInterpolator’, multichannelweights=‘1.0,3.0,1.0,1.0,1.0,1.0’”);

1 Like

I can confirm that this worked perfectly, your solution was extremely helpful!