ImageProcessor reduceColors(ImageProcessor ip) { MedianCut mc = new MedianCut((int[])ip.getPixels(), ip.getWidth(), ip.getHeight()); Image img = mc.convert(256); return(new ByteProcessor(img)); }
/** Converts an RGB image to 8-bits indexed color. 'nColors' must be greater than 1 and less than or equal to 256. */ public void convertRGBtoIndexedColor(int nColors) { if (type!=ImagePlus.COLOR_RGB) throw new IllegalArgumentException("Image must be RGB"); if (nColors<2) nColors = 2; if (nColors>256) nColors = 256; // get RGB pixels IJ.showProgress(0.1); IJ.showStatus("Grabbing pixels"); int width = imp.getWidth(); int height = imp.getHeight(); ImageProcessor ip = imp.getProcessor(); ip.snapshot(); int[] pixels = (int[])ip.getPixels(); imp.trimProcessor(); // convert to 8-bits long start = System.currentTimeMillis(); MedianCut mc = new MedianCut(pixels, width, height); ImageProcessor ip2 = mc.convertToByte(nColors); imp.setProcessor(null, ip2); }
/** Converts an RGB image to 8-bits indexed color. 'nColors' must be greater than 1 and less than or equal to 256. */ public void convertRGBtoIndexedColor(int nColors) { if (type!=ImagePlus.COLOR_RGB) throw new IllegalArgumentException("Image must be RGB"); if (nColors<2) nColors = 2; if (nColors>256) nColors = 256; // get RGB pixels IJ.showProgress(0.1); IJ.showStatus("Grabbing pixels"); int width = imp.getWidth(); int height = imp.getHeight(); ImageProcessor ip = imp.getProcessor(); ip.snapshot(); int[] pixels = (int[])ip.getPixels(); imp.trimProcessor(); // convert to 8-bits long start = System.currentTimeMillis(); MedianCut mc = new MedianCut(pixels, width, height); ImageProcessor ip2 = mc.convertToByte(nColors); imp.setProcessor(null, ip2); }
/** Converts the stack to 8-bits indexed color. 'nColors' must be greater than 1 and less than or equal to 256. */ public void convertToIndexedColor(int nColors) { if (type!=ImagePlus.COLOR_RGB) throw new IllegalArgumentException("RGB stack required"); ImageStack stack = imp.getStack(); int size = stack.getSize(); ImageProcessor montage = new ColorProcessor(width*size, height); for (int i=0; i<size; i++) montage.insert(stack.getProcessor(i+1), i*width, 0); MedianCut mc = new MedianCut((ColorProcessor)montage); montage = mc.convertToByte(nColors); ImageStack stack2 = new ImageStack(width, height); for (int i=0; i<size; i++) { montage.setRoi(i*width, 0, width, height); stack2.addSlice(null, montage.crop()); } imp.setStack(null, stack2); }
/** Converts the stack to 8-bits indexed color. 'nColors' must be greater than 1 and less than or equal to 256. */ public void convertToIndexedColor(int nColors) { if (type!=ImagePlus.COLOR_RGB) throw new IllegalArgumentException("RGB stack required"); ImageStack stack = imp.getStack(); int size = stack.getSize(); ImageProcessor montage = new ColorProcessor(width*size, height); for (int i=0; i<size; i++) montage.insert(stack.getProcessor(i+1), i*width, 0); MedianCut mc = new MedianCut((ColorProcessor)montage); montage = mc.convertToByte(nColors); ImageStack stack2 = new ImageStack(width, height); for (int i=0; i<size; i++) { montage.setRoi(i*width, 0, width, height); stack2.addSlice(null, montage.crop()); } imp.setStack(null, stack2); }
/** Does not respect local transform of the patches, this is intended for confocal stacks. */ public ImagePlus createColor256Copy() { final Rectangle box = patch[0].getBoundingBox(); final int width = box.width; final int height = box.height; Loader loader = patch[0].getProject().getLoader(); patch[0].getProject().getLoader().releaseToFit(4 * patch.length * width * height); // the montage, in RGB final ColorProcessor montage = new ColorProcessor(width*patch.length, height); for (int i=0; i<patch.length; i++) { montage.insert(this.stack.getProcessor(i+1), i*width, 0); } final MedianCut mc = new MedianCut(montage); loader.releaseToFit(patch.length * width * height); ImageProcessor m2 = mc.convertToByte(256); final ImageStack st = new ImageStack(width, height); for (int i=0; i<patch.length; i++) { m2.setRoi(i*width, 0, width, height); loader.releaseToFit(width * height); st.addSlice(null, m2.crop()); } ImagePlus imp = new ImagePlus("color256", st); imp.setCalibration(patch[0].getLayer().getParent().getCalibrationCopy()); //imp.getCalibration().pixelDepth = patch[0].getLayer().getThickness(); return imp; } }