Image using Threshold Method

I am brand new to ImageJ (this is my first day) and I am trying to do some basic analysis. I am trying to take a picture of a bird and make everything below a certain threshold, 100, black and everything else white. However, my end picture is not changing at all and I have no idea why. Thanks for helping out a newbie! This is what my code looks like.

public static void main(String[] args) {
	ImagePlus imgToCheck = new ImagePlus("bird.jpg");
        ImageProcessor imgProcessor = imgToCheck.getProcessor();
	imgProcessor = imgProcessor.convertToByteProcessor();
	System.out.println("bitDepth " + imgProcessor.getBitDepth()); //returns bitDepth 8
	imgProcessor.threshold(132);
	FileSaver fs = new FileSaver(imgToCheck);
	fs.saveAsJpeg("test.jpg");
}

Welcome to the forum, @sandiegosurfer!
If it’s only for some basic analysis, I wonder why you start writing Java code instead of first processing your image(s) using the well-documented graphical user interface, and then maybe advance to one of the scripting languages. The recorder makes it easy to get the correct script code for almost any menu command you’re running from the ImageJ window.


Regarding your posted code, it should probably be sufficient to call:

imgToCheck.setProcessor(imgProcessor);

after getting the new ImageProcessor for the converted bit depth, but there are certainly better (and shorter) options to save an image as Jpeg (which would automatically converted to 8-bit upon saving).

If you provide I bit more context, I’m sure someone on this forum will have more advice on how to achieve your goal.

1 Like

That worked perfect, thanks a ton! And i’ll be sure to check out the GUI and scripting languages, but I am really only using ImageJ because I needed to do image analysis for another Java project and it seemed like this was the easiest way.