/** Returns Lab in 3 byte arrays. */ public static void getLab(ImageProcessor ip, byte[] L, byte[] a, byte[] b) { ColorSpaceConverter converter = new ColorSpaceConverter(); int[] pixels = (int[])ip.getPixels(); for (int i=0; i<pixels.length; i++) { double[] values = converter.RGBtoLAB(pixels[i]); int L1 = (int) (values[0] * 2.55); int a1 = (int) (Math.floor((1.0625 * values[1] + 128) + 0.5)); int b1 = (int) (Math.floor((1.0625 * values[2] + 128) + 0.5)); L[i] = (byte)((int)(L1<0?0:(L1>255?255:L1)) & 0xff); a[i] = (byte)((int)(a1<0?0:(a1>255?255:a1)) & 0xff); b[i] = (byte)((int)(b1<0?0:(b1>255?255:b1)) & 0xff); } }
/** Returns Lab in 3 byte arrays. */ public static void getLab(ImageProcessor ip, byte[] L, byte[] a, byte[] b) { ColorSpaceConverter converter = new ColorSpaceConverter(); int[] pixels = (int[])ip.getPixels(); for (int i=0; i<pixels.length; i++) { double[] values = converter.RGBtoLAB(pixels[i]); int L1 = (int) (values[0] * 2.55); int a1 = (int) (Math.floor((1.0625 * values[1] + 128) + 0.5)); int b1 = (int) (Math.floor((1.0625 * values[2] + 128) + 0.5)); L[i] = (byte)((int)(L1<0?0:(L1>255?255:L1)) & 0xff); a[i] = (byte)((int)(a1<0?0:(a1>255?255:a1)) & 0xff); b[i] = (byte)((int)(b1<0?0:(b1>255?255:b1)) & 0xff); } }
/** Converts an RGB image into a Lab stack. */ public ImagePlus RGBToLab(ImagePlus img) { ColorProcessor cp = (ColorProcessor)img.getProcessor(); ColorSpaceConverter converter = new ColorSpaceConverter(); int[] pixels = (int[])cp.getPixels(); int w = cp.getWidth(); int h = cp.getHeight(); ImageStack stack = new ImageStack(w,h); FloatProcessor L = new FloatProcessor(w,h); FloatProcessor a = new FloatProcessor(w,h); FloatProcessor b = new FloatProcessor(w,h); stack.addSlice("L*",L); stack.addSlice("a*",a); stack.addSlice("b*",b); for (int i=0; i<pixels.length; i++) { double[] values = converter.RGBtoLAB(pixels[i]); L.setf(i,(float)values[0]); a.setf(i,(float)values[1]); b.setf(i,(float)values[2]); } ImagePlus img2 = new ImagePlus("Lab_"+img.getTitle(),stack); CompositeImage img3 = new CompositeImage(img2,IJ.GRAYSCALE); img3.resetDisplayRanges(); return img3; }
/** Converts an RGB image into a Lab stack. */ public ImagePlus RGBToLab(ImagePlus img) { ColorProcessor cp = (ColorProcessor)img.getProcessor(); ColorSpaceConverter converter = new ColorSpaceConverter(); int[] pixels = (int[])cp.getPixels(); int w = cp.getWidth(); int h = cp.getHeight(); ImageStack stack = new ImageStack(w,h); FloatProcessor L = new FloatProcessor(w,h); FloatProcessor a = new FloatProcessor(w,h); FloatProcessor b = new FloatProcessor(w,h); stack.addSlice("L*",L); stack.addSlice("a*",a); stack.addSlice("b*",b); for (int i=0; i<pixels.length; i++) { double[] values = converter.RGBtoLAB(pixels[i]); L.setf(i,(float)values[0]); a.setf(i,(float)values[1]); b.setf(i,(float)values[2]); } ImagePlus img2 = new ImagePlus("Lab_"+img.getTitle(),stack); CompositeImage img3 = new CompositeImage(img2,IJ.GRAYSCALE); img3.resetDisplayRanges(); return img3; }