Auto threshold not working in image



I want to threshold an image but it doesn’t seem to work in Java Eclipse. Here’s a snippet of the code:

ImagePlus[] channelsRGB = ChannelSplitter.split(duplicate);
ImagePlus Gchannel = channelsRGB[1];
IJ.setAutoThreshold(Gchannel, "Triangle dark");

The error displayed is as follows:

A thresholded image or 8-bit binary image is required. Threshold levels can be set using the Image->Adjust->Threshold tool.

How do I fix this?



What is your input image? Is it an RGB? Because I believe only 8- and 16-bit images are supported for this function. So you can convert it first to a RGB Stack or split channels first… anyone else on this one?

Just double-check its properties… you can attach it here too for us to test out.



Hi etarena,

The image ‘duplicate’ is an RGB image. ‘ChannelSplitter.split’ does split the image into different channels i.e. R, G, and B plane. You could take any color image but the error persists.

Thanks and Regards,


@kbimagej -

This is the code that I ran and it’s working for me…

import ij.IJ;
import ij.ImageJ;
import ij.ImagePlus;
import ij.plugin.ChannelSplitter;

public class Main {

public static void main(String[] args) {
		// start ImageJ
		new ImageJ();

		// open the FluorescentCells sample (to test single slice images)
		ImagePlus image = IJ.openImage("");
		ImagePlus[] imageChannels = ChannelSplitter.split(image);
		ImagePlus Gchannel = imageChannels[1];
		IJ.setAutoThreshold(Gchannel, "Triangle dark");

Again - I wasn’t able to test your image with this… I did the “Lena” sample image that comes with Fiji - and it worked just fine. Would you be able to share your image file?




The Auto Threshold worked. Since I am using ‘Auto Threshold’ as an external plugin, I had to call it using a Class object of wildcard type. Here’s the working code snippet:

Class<?> ATclassobj = Auto_Threshold.class;
IJ.runPlugIn(Gchannel, ATclassobj.getName(), "method=Otsu");

Many Thanks,