Accessing active image data

I’m wondering how best to access active image data from within a plugin’s run() method. All of the examples I’ve found on imagej-tutorials use images opened with hard coded file paths; however, most plugins (in the wild) seem to operate on whatever image is currently active. After searching around in the javadocs, I found that I could do something like this:

@Parameter
private DatasetService datasetService;

@Parameter
private Dataset im;

@Parameter(type = ItemIO.OUTPUT)
private String metadata;

@Override
public void run() {
    final List<Dataset> mylist  = datasetService.getDatasets();
    im = mylist.get(0);
    metadata = im.getName();
}

Unfortunately, it is not clear to me if I can assume that getDatasets() has the active image at index 0. Is this the method most plugins use? Should I perform this operation some other way?

On a semi-related note… does the community prefer quick questions (like this) to be posted on the forum or on Gitter? I like that Gitter seems faster and less formal, but I’ve noticed that most questions seem to get redirected to the forum. I’m happy to continue posting questions here if the community prefers to have the information in a more accessible location.

Thanks!

No need to query the DatasetService here. As you defined a Parameter here:

… the active image is already available in your Dataset im and you don’t need to assign it in your code. (Actually, it is the ActiveDatasetPreprocessor that takes care of doing this when your plugin is run interactively from the GUI.)

So this should work:

@Parameter
private Dataset im;

@Parameter(type = ItemIO.OUTPUT)
private String metadata;

@Override
public void run() {
    metadata = im.getName();
}

See also the example-imagej-command project.


Unless it’s for really small (and development-related) questions, I’d always prefer the forum, as it is much more accessible to others who might have the same question in the future. (And if I don’t like your formatting, I [as all moderators] can edit it here on the forum, whereas I can’t on gitter :wink: )

1 Like

Thanks, Jan, this is extremely helpful. I came across the GaussFiltering example in my travels, but I completely missed this detail. If I get a chance, I will submit a pull request to add something like this to imagej-tutorials (just in case someone else gets similarly confused). While I’m at it, I can also update the depreciated net.imagej.Main.launch(args) in the maven-projects if it’s useful.

Thanks! I’ll keep posting my questions here as I work my way towards imagej proficiency.

This is a win win for me–it’s prettier and I didn’t have to do anything. I'll try not to abuse this.

Thanks!

1 Like