Assign own color LUT in ImageJ

Hi @Bio7 @Wayne @imagejan @NicoKiaru @haesleinhuepf ,

I would like to assign my own LUT to an image, using java.

I figured out how to make my own LUT, but when I do this…

imp.setLUT( myLUT );

…I get an image of type 8-bit color.

However, I just want to have a normal 8-bit image with a LUT stored in its metadata; such as in the one attached: blobs_00–labelMask.tif (65.2 KB)

Do you know how to achieve this?

Hi Christian,

Can you try this ?

imp.setC(myChannel);
imp.getProcessor().setLut(myLUT);

Let us know.

Best,

Nico

Also this converts it to an 8-bit color image for me.

Found this code @Alex_H

Looking into it right now…

…but it is the same issue. I think this kind of code is more for actually displaying a LUT as an image. And not for setting a LUT to an image…

You can load and display a LUT on the current image with:

import ij.plugin.LutLoader;
import ij.plugin.PlugIn;
public class My_Plugin implements PlugIn {

	public void run(String arg) {
		LutLoader lut=new LutLoader();
		lut.run("C:/MySavedLut.lut");
		
	}
}

As described in the LutLoader API:

https://imagej.nih.gov/ij/developer/api/ij/plugin/LutLoader.html

According to the ImagePlus API it should also work with imp.setLut when a LUT object has been loaded before (ImagePlus instead of ImageProcessor setLut API!):

https://imagej.nih.gov/ij/developer/api/ij/ImagePlus.html#setLut-ij.process.LUT-

So this works for me, too:

import ij.WindowManager;
import ij.io.Opener;
import ij.plugin.PlugIn;
import ij.process.LUT;

public class My_Plugin implements PlugIn {

	public void run(String arg) {
		
		LUT lut=Opener.openLut("C:/MySaved.lut");
		WindowManager.getCurrentImage().setLut(lut);
		
	}
}

In the last example you can also create and use a LUT object instead of a file.

https://imagej.nih.gov/ij/developer/api/ij/process/LUT.html

1 Like

I do not understand what I am doing wrong.
I think I do the same but for me it converts it to an 8-bit color image.

import ij.IJ;
import ij.ImagePlus;
import ij.process.LUT;
import net.imagej.ImageJ;

import java.awt.*;

public class TestLut
{

	public static void main( String[] args )
	{

		new ImageJ().ui().showUI();

        final ImagePlus imagePlus =
				IJ.openImage("http://wsr.imagej.net/images/blobs.gif");

		imagePlus.setLut( createClassLabelLUT( ) );

		imagePlus.show();
	}


	public static LUT createClassLabelLUT( )
	{
		final byte[] red = new byte[ 256 ];
		final byte[] green = new byte[ 256 ];
		final byte[] blue = new byte[ 256 ];

		for ( int iClass = 0; iClass < classColors.length; iClass++ )
		{
			red[ iClass + 1 ] = ( byte ) ( classColors[ iClass ].getRed() );
			green[ iClass + 1 ] = ( byte ) ( classColors[ iClass ].getGreen() );
			blue[ iClass + 1 ] = ( byte ) ( classColors[ iClass ].getBlue() );
		}

		return new LUT( red, green, blue );

	}


	public static Color[] classColors = new Color[]{
			Color.gray,
			Color.green,
			Color.red,
			Color.blue,
			Color.cyan,
			Color.pink,
			Color.white,
			Color.magenta,
			Color.orange,
			Color.black,
			Color.yellow,
			Color.gray,
			Color.green,
			Color.red,
			Color.blue,
			Color.cyan,
			Color.pink,
			Color.white,
			Color.magenta,
			Color.orange,
			Color.black
	};
}

It works if you first load and display the image and then apply the LUT:

final ImagePlus imagePlus =IJ.openImage("http://wsr.imagej.net/images/blobs.gif");
imagePlus.show();
imagePlus.setLut(createClassLabelLUT());

If I do not display the image first I get an Indexed Color image as you described!

1 Like

Wow!
Bug or feature? :wink:
At least not useable for me like this because I need to run this headless…