Search code examples
pythonjythonimagej

Combine channels in ImageJ Jython?


I have two channels from an image stack which I've split like so:

red_c, green_c = ChannelSplitter.split(imp)

And now I want to combine them horizontally:

combined_img = StackCombiner.combineHorizontally(green_c, red_c)

This throws an error that says 3 arguments were expected, but only 2 provided. But from the documentation it says combineHorizontally(ImageStack stack1, ImageStack stack2)

Why is this not working?


EDIT: solved it. Turns out the correct way of writing it is

combined = StackCombiner().combineHorizontally(grn_stack, red_stack)

Why this needs an extra () but ChannelSplitter doesn't is a mystery to me. They're both imported from ij.plugin. Can somebody shed light on this?


Solution

  • solved it.

    Glad that you found it. For the future, questions like this one are still a good fit for the ImageJ forum (where you seem to have an account as well), particularly when you're asking about specifics of the ImageJ API.

    Why this needs an extra () but ChannelSplitter doesn't is a mystery to me.

    ImageJ is a Java application, and in your Jython script you're actually calling the Java API of StackCombiner. The call

    StackCombiner.combineHorizontally(green_c, red_c)
    

    would work if combineHorizontally was a static method of StackCombiner, but as it isn't, it needs a new StackCombiner object being instantiated first.

    In Java, you'd have to write:

    new StackCombiner().combineHorizontally(a,b)
    

    In Python, you don't need a new keyword, but you still need to use the constructor:

    StackCombiner().combineHorizontally(a,b)
    

    In contrast, the ChannelSplitter.split(ImagePlus) method is static, so you can use it without instantiating an object.