Search code examples
javargbgrayscaleimagej8-bit

Java for ImageJ. How to convert an image from RGB to 8 bit using code?


I am currently working on Connected Component Labelling. This is a process which takes an image and tells you how many separate objects are in the Image.

My problem is that at the very start I need to be able to take any image (specifically RGB value) and convert it into 8-bit.

EDIT: as in literally considered an 8bit, where the image is no longer recognised as RGB. Not an 8bit image that is recognised as an RGB.

Is there a way using code to automatically do this without having to go into the toolbar and convert it "manually"?

To clarify, I am programming for ImageJ using Java.

If anyone is willing to help me, I would be happy to provide them with the code I have so far where I am making a coloured image grey scale and then making it binary. My problem is that after the changes the image is still considered RGB, even though the image is essentially 8 bit.

Thanks

EDIT: I was looking at the code provided to me earlier and it doesn't seem to solve my problem. I am quite literally just wanting to make the little 'tick' that's next to RGB, be next to 8-bit instead. I have already done all the actual conversion on my own, its just still recognised as RGB image.


Solution

  • Please try:

    import ij.ImagePlus;
    import ij.process.ImageConverter;
    
    // ...
    
    ImagePlus imp = IJ.getImage();
    ImageConverter ic = new ImageConverter(imp);
    ic.convertToGray8();
    imp.updateAndDraw();