The functionality of "Image > Color > Split Channels" in code

I am trying to use the functionality of “Image > Color > Split Channels” menu for an image. Here is the code I’ve come up with:

ChannelSplitter splitter = new ChannelSplitter();
ImagePlus[] channels = splitter.split(imp);

This gives me three ImagePlus objects which seem to be the red, green and blue channels of the original image. I just want to make sure that this is exactly equivalent to “Image > Color > Split Channels”, because when I compare the results, the images that I get using splitter.split are a little different. So I thought maybe I should use the getChannel function of the ChannelSplitter class instead. The documentation of getChannel is poor though and I don’t know how I should use it.

You do not need to write new ChannelSplitter() since the ChannelSplitter.split(ImagePlus) method is static. You can simply write:

ImagePlus[] channels = ChannelSplitter.split(imp);

I learned this by using the Recorder set to Java mode.

Check the ChannelSplitter source code. The run method is what happens when you run the plugin; the split method is only part of the equation. It looks like if your image is RGB, then instead the splitRGB method gets called… maybe that’s your culprit.

2 Likes