How can i change a color of an image programmatically?

Tags: #<Tag:0x00007fb87d540848> #<Tag:0x00007fb87d540708> #<Tag:0x00007fb87d5405c8> #<Tag:0x00007fb87d540488>


Suppose i have a stack of 30 images and i want to change color of some of the image using LUTs

         LUT overlayLUT;
        // Create overlay LUT
                byte[] red = new byte[ 256 ];
                byte[] green = new byte[ 256 ];
                byte[] blue = new byte[ 256 ];

        public void setLut(List<Color> colors ){
                int i=0;
                for(Color color: colors){
                        red[i] = (byte) color.getRed();
                        green[i] = (byte) color.getGreen();
                        blue[i] = (byte) color.getBlue();
                overlayLUT = new LUT(red, green, blue);

ImagePlus t= IJ.openImage("/home/mg/Downloads/tifs/image.tif");;

I am still getting black color images at index 5 and 15?


Lookup tables (LUTs) always apply to a full stack (in case of simple stacks) or to a single channel (in case of composite images). If you want to change the LUT for just some specific slices of your 30-image stack, you’ll have to convert your stack to be a hyperstack with 30 channels.

In case you have a CompositeImage, you can use setLuts(LUT[] luts) method to define the color tables for all channels. Here’s an illustrative Groovy script (run it from the script editor after choosing Language > Groovy) that should be easy to convert into Java if required:

#@ ImagePlus imp

import ij.process.LUT
import java.awt.Color
import ij.CompositeImage

if (imp instanceof CompositeImage) {
	luts = imp.getLuts()
	luts[0] = LUT.createLutFromColor(Color.CYAN)
	luts[1] = LUT.createLutFromColor(Color.MAGENTA)

For the record: you asked (almost) the same question on

Other people reading up on this topic might be interested to check over there as well, so please always transparently link when cross-posting.