ij.process.ColorProcessor Java Examples

The following examples show how to use ij.process.ColorProcessor. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: MorphologyTest.java    From MorphoLibJ with GNU Lesser General Public License v3.0 6 votes vote down vote up
/**
 * Tests closing can be run on an RGB image.
 */
@Test
public void testClosing_Square_RGB() {
	String fileName = getClass().getResource("/files/peppers-crop.png").getFile();
	ImagePlus imagePlus = IJ.openImage(fileName);
	assertNotNull(imagePlus);
	ColorProcessor image = (ColorProcessor) imagePlus.getProcessor();
	
	Strel strel = SquareStrel.fromDiameter(5);
	ColorProcessor result = (ColorProcessor) Morphology.closing(image, strel);
	assertNotNull(result);
	
	// Check that result is greater than or equal to the original image
	int width = image.getWidth();
	int height = image.getHeight();
	int[] rgb0 = new int[3];
	int[] rgb = new int[3];
	for (int y = 0; y < height; y++) {
		for (int x = 0; x < width; x++) {
			image.getPixel(x, y, rgb0);
			result.getPixel(x, y, rgb);
			for (int c = 0; c < 3; c++)
				assertTrue(rgb[c] >= rgb0[c]);
		}
	}
}
 
Example #2
Source File: Util.java    From TrakEM2 with GNU General Public License v3.0 6 votes vote down vote up
/**
 * <p>Transfer and ARGB AWT image into a FloatProcessor with its grey values
 * and a FloatProcessor with its alpha values as [0...1].</p>
 * 
 * <p><em>Note</em>, this method currently relies on how ImageJ reuses the
 * pixels of an AWT image as generated by {@link Loader#getFlatAWTImage(ini.trakem2.display.Layer, java.awt.Rectangle, double, int, int, Class, java.util.List, boolean, java.awt.Color, ini.trakem2.display.Displayable) Loader.getFlatAWTImage(...)}
 * for creating a ColorProcessor.  This may change in the future as have
 * many things in the past.  This method is then the place to fix it. 
 * 
 * @param input
 * @param output
 * @param alpha
 */
final static public void imageToFloatAndMask( final Image input, final FloatProcessor output, final FloatProcessor alpha )
{
	final ColorProcessor cp = new ColorProcessor( input );
	final int[] inputPixels = ( int[] )cp.getPixels();
	for ( int i = 0; i < inputPixels.length; ++i )
	{
		final int argb = inputPixels[ i ];
		final int a = ( argb >> 24 ) & 0xff;
		final int r = ( argb >> 16 ) & 0xff;
		final int g = ( argb >> 8 ) & 0xff;
		final int b = argb & 0xff;
		
		final float v = ( r + g + b ) / ( float )3;
		final float w = a / ( float )255;
		
		output.setf( i, v );
		alpha.setf( i, w );
	}
}
 
Example #3
Source File: ExportBestFlatImage.java    From TrakEM2 with GNU General Public License v3.0 6 votes vote down vote up
public Pair<ColorProcessor, ByteProcessor> makeFlatColorImage()
{
	if ( canUseAWTImage() ) { // less than 0.5 GB array size
		final ColorProcessor cp = new ColorProcessor( createAWTImage( ImagePlus.COLOR_RGB ) );
		final ByteProcessor alpha = new ByteProcessor( cp.getWidth(), cp.getHeight(), cp.getChannel( 4 ) );
		return new Pair<ColorProcessor, ByteProcessor>( cp, alpha );
	}

	if ( !isSmallerThan2GB() ) {
		Utils.log("Cannot create an image larger than 2 GB.");
		return null;
	}

	if ( loader.isMipMapsRegenerationEnabled() )
	{
		return ExportARGB.makeFlatImageARGBFromMipMaps( patches, finalBox, 0, scale );
	}

	// No mipmaps: create an image as large as possible, then downsample it
	final Pair<ColorProcessor, ByteProcessor> pair = ExportARGB.makeFlatImageARGBFromOriginals( patches, finalBox, 0, scaleUP );

	final double sigma = computeSigma( pair.a.getWidth(), pair.a.getHeight());
	new GaussianBlur().blurGaussian( pair.a, sigma, sigma, 0.0002 );
	new GaussianBlur().blurGaussian( pair.b, sigma, sigma, 0.0002 );
	return pair;
}
 
Example #4
Source File: ValueToNoise.java    From render with GNU General Public License v2.0 6 votes vote down vote up
@Override
public ImageProcessor process(final ImageProcessor ip,
                              final double scale) {
    try {
        if (FloatProcessor.class.isInstance(ip)) {
            if (Double.isNaN(value))
                processFloatNaN((FloatProcessor) ip, min, max);
            else
                processFloat((FloatProcessor) ip, (float) value, min, max);
        } else {
            if (ColorProcessor.class.isInstance(ip))
                processColor((ColorProcessor) ip, (int) Math.round(value),
                        (int) Math.round(min), (int) Math.round(max));
            else
                processGray(ip, (int) Math.round(value),
                        (int) Math.round(min), (int) Math.round(max));
        }
    } catch (final Exception e) {
        e.printStackTrace();
    }
    return ip;
}
 
Example #5
Source File: MatImagePlusConverter.java    From IJ-OpenCV with GNU General Public License v3.0 6 votes vote down vote up
private static ColorProcessor makeColorProcessor(Mat mat) {
    if (mat.type() != opencv_core.CV_8UC3) {
        throw new IllegalArgumentException("wrong Mat type: " + mat.type());
    }
    final int w = mat.cols();
    final int h = mat.rows();
    byte[] pixels = new byte[w * h * (int) mat.elemSize()];
    mat.data().get(pixels);
    // convert byte array to int-encoded RGB values
    ColorProcessor cp = new ColorProcessor(w, h);
    int[] iData = (int[]) cp.getPixels();
    for (int i = 0; i < w * h; i++) {
        int red = pixels[i * 3 + 0] & 0xff;
        int grn = pixels[i * 3 + 1] & 0xff;
        int blu = pixels[i * 3 + 2] & 0xff;
        iData[i] = (red << 16) | (grn << 8) | blu;
    }
    return cp;
}
 
Example #6
Source File: ValueToNoise.java    From render with GNU General Public License v2.0 6 votes vote down vote up
private static void processColor(final ColorProcessor ip,
                                 final int value,
                                 final int min,
                                 final int max) {
    final int scale = max - min + 1;
    final Random rnd = new Random();
    final int n = ip.getWidth() * ip.getHeight();
    for (int i = 0; i < n; ++i) {
        final int v = ip.get(i) & 0x00ffffff;
        if (v == value) {
            final int r = rnd.nextInt(scale) + min;
            final int g = rnd.nextInt(scale) + min;
            final int b = rnd.nextInt(scale) + min;

            ip.set(i, (((((0xff << 8) | r) << 8) | g) << 8) | b);
        }
    }
}
 
Example #7
Source File: Distortion_Correction.java    From TrakEM2 with GNU General Public License v3.0 6 votes vote down vote up
void visualizePoints( final List< List< PointMatch > > inliers)
{
	final ColorProcessor ip = new ColorProcessor(nlt.getWidth(), nlt.getHeight());
	ip.setColor(Color.red);

	ip.setLineWidth(5);
	for (int i=0; i < inliers.size(); i++){
		for (int j=0; j < inliers.get(i).size(); j++){
			final double[] tmp1 = inliers.get(i).get(j).getP1().getW();
			final double[] tmp2 = inliers.get(i).get(j).getP2().getL();
			ip.setColor(Color.red);
			ip.drawDot((int) tmp2[0], (int) tmp2[1]);
			ip.setColor(Color.blue);
			ip.drawDot((int) tmp1[0], (int) tmp1[1]);
		}
	}

	final ImagePlus points = new ImagePlus("Corresponding Points after correction", ip);
	points.show();
}
 
Example #8
Source File: ImagePlusMatConverter.java    From IJ-OpenCV with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Dispatcher method. Duplicates {@link ImageProcessor} to the corresponding
 * OpenCV image of type {@link Mat}. TODO: Could be coded more elegantly ;-)
 *
 * @param ip The {@link ImageProcessor} to be converted
 * @return The OpenCV image (of type {@link Mat})
 */
public static Mat toMat(ImageProcessor ip) {
    Mat mat = null;
    if (ip instanceof ByteProcessor) {
        mat = toMat((ByteProcessor) ip);
    } else if (ip instanceof ColorProcessor) {
        mat = toMat((ColorProcessor) ip);
    } else if (ip instanceof ShortProcessor) {
        mat = toMat((ShortProcessor) ip);
    } else if (ip instanceof FloatProcessor) {
        mat = toMat((FloatProcessor) ip);
    } else {
        throw new IllegalArgumentException("cannot convert to Mat: " + ip);
    }
    return mat;
}
 
Example #9
Source File: BorderManager.java    From MorphoLibJ with GNU Lesser General Public License v3.0 6 votes vote down vote up
public BorderManager createBorderManager(ImageProcessor image) {
	switch((Type) this) {
	case REPLICATED:
		return new ReplicatedBorder(image);
	case PERIODIC:
		return new PeriodicBorder(image);
	case MIRRORED:
		return new MirroringBorder(image);
	case BLACK:
		return new ConstantBorder(image, 0);
	case WHITE:
		return new ConstantBorder(image, 0xFFFFFF);
	case GRAY:
		if (image instanceof ColorProcessor)
			return new ConstantBorder(image, 0x7F7F7F);
		if (image instanceof ShortProcessor)
			return new ConstantBorder(image, 0x007FFF);
		return new ConstantBorder(image, 127);
	default:
		throw new RuntimeException("Unknown border manager for type "  + this);
	}
}
 
Example #10
Source File: ColorImages.java    From MorphoLibJ with GNU Lesser General Public License v3.0 6 votes vote down vote up
/**
 * Prints the content of the input color image on the console. This can be
 * used for debugging (small) images.
 * 
 * @param image
 *            the color image to display on the console
 */
public static final void print(ColorProcessor image) 
{
	for (int y = 0; y < image.getHeight(); y++)
	{
		for (int x = 0; x < image.getWidth(); x++)
		{
			int intCode = image.get(x, y);
			int r = (intCode & 0xFF0000) >> 16;
			int g = (intCode & 0xFF00) >> 8;
			int b = (intCode & 0xFF);
			System.out.print(String.format("(%3d,%3d,%3d) ", r, g, b));
		}
		System.out.println("");
	}
}
 
Example #11
Source File: Picture.java    From audiveris with GNU Affero General Public License v3.0 6 votes vote down vote up
/**
 * Report the initial source.
 *
 * @param img the initial image
 * @return the initial source
 */
public ByteProcessor getInitialSource (BufferedImage img)
{
    if (img != null) {
        if (img.getType() != BufferedImage.TYPE_BYTE_GRAY) {
            StopWatch watch = new StopWatch("ToGray");
            watch.start("convertToByteProcessor");

            ColorProcessor cp = new ColorProcessor(img);
            ByteProcessor bp = cp.convertToByteProcessor();

            if (constants.printWatch.isSet()) {
                watch.print();
            }

            return bp;
        } else {
            return new ByteProcessor(img);
        }
    } else {
        return null;
    }
}
 
Example #12
Source File: NonLinearTransform.java    From TrakEM2 with GNU General Public License v3.0 6 votes vote down vote up
public ImageProcessor[] transform(final ImageProcessor ip){
	if (!precalculated)
		this.precalculateTransfom();

	final ImageProcessor newIp = ip.createProcessor(ip.getWidth(), ip.getHeight());
	if (ip instanceof ColorProcessor) ip.max(0);
	final ImageProcessor maskIp = new ByteProcessor(ip.getWidth(),ip.getHeight());

	for (int x=0; x < width; x++){
		for (int y=0; y < height; y++){
			if (transField[x][y][0] == -1){
				continue;
			}
			newIp.set(x, y, (int) ip.getInterpolatedPixel((int)transField[x][y][0],(int)transField[x][y][1]));
			maskIp.set(x,y,255);
		}
	}
	return new ImageProcessor[]{newIp, maskIp};
}
 
Example #13
Source File: FSLoader.java    From TrakEM2 with GNU General Public License v3.0 6 votes vote down vote up
boolean save(ImageProcessor ip, final String path, final float quality, final boolean as_grey) {
	if (as_grey) ip = ip.convertToByte(false);
	if (ip instanceof ByteProcessor) {
		return save(path, new byte[][]{(byte[])ip.getPixels()}, ip.getWidth(), ip.getHeight(), quality);
	} else if (ip instanceof ColorProcessor) {
		final int[] p = (int[]) ip.getPixels();
		final byte[] r = new byte[p.length],
		             g = new byte[p.length],
		             b = new byte[p.length],
		             a = new byte[p.length];
		for (int i=0; i<p.length; ++i) {
			final int x = p[i];
			r[i] = (byte)((x >> 16)&0xff);
			g[i] = (byte)((x >>  8)&0xff);
			b[i] = (byte) (x       &0xff);
			a[i] = (byte)((x >> 24)&0xff);
		}
		return save(path, new byte[][]{r, g, b, a}, ip.getWidth(), ip.getHeight(), quality);
	}
	return false;
}
 
Example #14
Source File: ColorImages.java    From MorphoLibJ with GNU Lesser General Public License v3.0 6 votes vote down vote up
/**
 * Creates a new ColorProcessor from a collection of three channels.
 * 
 * @param channels
 *            a collection containing the red, green and blue channels
 * @return the color image corresponding to the concatenation of the three
 *         channels
 * @throws IllegalArgumentException
 *             if the collection contains less than three channels
 */
public static final ColorProcessor mergeChannels(Collection<ImageProcessor> channels) 
{
	// check validity of input
	if (channels.size() < 3)
		throw new IllegalArgumentException("Requires at least three channels in the collection");
	
	// extract each individual channel
	Iterator<ImageProcessor> iterator = channels.iterator();
	ImageProcessor red = iterator.next();
	ImageProcessor green = iterator.next();
	ImageProcessor blue = iterator.next();

	// call helper function
	return mergeChannels(red, green, blue);
}
 
Example #15
Source File: Normalize.java    From TrakEM2 with GNU General Public License v3.0 6 votes vote down vote up
@Override
public ImageProcessor process(ImageProcessor ip) {
	if (ip instanceof ColorProcessor) {
		FloatProcessor r = normalize(ip.toFloat(0, null)),
		               g = normalize(ip.toFloat(1, null)),
		               b = normalize(ip.toFloat(2, null));
		int[] p = new int[ip.getWidth() * ip.getHeight()];
		ColorProcessor cp = new ColorProcessor(ip.getWidth(), ip.getHeight(), p);
		final float[] rp = (float[]) r.getPixels(),
		              gp = (float[]) g.getPixels(),
		              bp = (float[]) b.getPixels();
		for (int i=0; i<p.length; ++i) {
			p[i] = ((int)rp[i] << 16) | ((int)gp[i] << 8) | (int)bp[i];
		}
		return cp;
	}
	final FloatProcessor fp = normalize((FloatProcessor)ip.convertToFloat());
	if (ip instanceof FloatProcessor) {
		return fp;
	}
	final int len = ip.getWidth() * ip.getHeight();
	for (int i=0; i<len; ++i) {
		ip.setf(i, fp.get(i));
	}
	return ip;
}
 
Example #16
Source File: ValueToNoise.java    From TrakEM2 with GNU General Public License v3.0 6 votes vote down vote up
@Override
public ImageProcessor process(final ImageProcessor ip) {
	try {
		if (FloatProcessor.class.isInstance(ip)) {
			if (Double.isNaN(value))
				processFloatNaN((FloatProcessor)ip, min, max);
			else
				processFloat((FloatProcessor)ip, (float)value, min, max);
		} else {
			if (ColorProcessor.class.isInstance(ip))
				processColor((ColorProcessor)ip, (int)Math.round(value), (int)Math.round(min), (int)Math.round(max));
			else
				processGray(ip, (int)Math.round(value), (int)Math.round(min), (int)Math.round(max));
		}
	} catch (final Exception e) {
		e.printStackTrace();
	}
	return ip;
}
 
Example #17
Source File: ValueToNoise.java    From TrakEM2 with GNU General Public License v3.0 6 votes vote down vote up
final static private void processColor(final ColorProcessor ip, final int value, final int min, final int max) {
	final int scale = max - min + 1;
	final Random rnd = new Random();
	final int n = ip.getWidth() * ip.getHeight();
	for (int i =0; i < n; ++i) {
		final int v = ip.get(i);
		if (v == value)
		{
			final int r = rnd.nextInt(scale) + min;
			final int g = rnd.nextInt(scale) + min;
			final int b = rnd.nextInt(scale) + min;

			ip.set(i, (((((0xff << 8) | r) << 8) | g) << 8) | b);
		}
	}
}
 
Example #18
Source File: LUTCustom.java    From TrakEM2 with GNU General Public License v3.0 6 votes vote down vote up
@Override
public ImageProcessor process(ImageProcessor ip) {
	if (ip instanceof ColorProcessor) {
		Utils.log("Ignoring " + getClass().getSimpleName() + " filter for RGB image");
		return ip;
	}
	byte[] s1 = new byte[256];
	byte[] s2 = new byte[256];
	byte[] s3 = new byte[256];
	for (int i=0; i<256; ++i) {
		s1[i] = (byte)(int)(i * r);
		s2[i] = (byte)(int)(i * g);
		s3[i] = (byte)(int)(i * b);
		Utils.log2(i + ": r, g, b " + s1[i] + ", " + s2[i] + ", " + s3[i]);
	}
	ip.setColorModel(new IndexColorModel(8, 256, s1, s2, s3));
	return ip;
}
 
Example #19
Source File: PatchStack.java    From TrakEM2 with GNU General Public License v3.0 6 votes vote down vote up
/** 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;
}
 
Example #20
Source File: Threshold.java    From MorphoLibJ with GNU Lesser General Public License v3.0 6 votes vote down vote up
/**
 * Creates a new binary image with value 255 when input image has value
 * between <code>lower</code> and <code>upper</code> values (inclusive).
 * 
 * @param image
 *            the input grayscale image
 * @param lower
 *            the lower threshold bound (inclusive)
 * @param upper
 *            the upper threshold bound (inclusive)
 * @return a binary image
 */
public static final ImageProcessor threshold(ImageProcessor image, double lower, double upper)
{
	if (image instanceof ColorProcessor) 
	{
		throw new IllegalArgumentException("Requires a gray scale image");
	}
	
	int width = image.getWidth();
	int height = image.getHeight();

	ImageProcessor result = new ByteProcessor(width, height);
	for (int y = 0; y < height; y++)
	{
		for (int x = 0; x < width; x++)
		{
			double value = image.getf(x, y);
			if (value >= lower && value <= upper)
				result.set(x, y, 255);
		}

	}

	return result;
}
 
Example #21
Source File: MorphologyTest.java    From MorphoLibJ with GNU Lesser General Public License v3.0 6 votes vote down vote up
/**
 * Tests that most morphological operations can be run on an RGB image.
 */
@Test
public void testVariousOperations_Square_RGB() {
	String fileName = getClass().getResource("/files/peppers-crop.png").getFile();
	ImagePlus imagePlus = IJ.openImage(fileName);
	assertNotNull(imagePlus);
	ColorProcessor image = (ColorProcessor) imagePlus.getProcessor();
	
	Strel strel = SquareStrel.fromDiameter(5);

	assertNotNull(Morphology.erosion(image, strel));
	assertNotNull(Morphology.dilation(image, strel));

	assertNotNull(Morphology.closing(image, strel));
	assertNotNull(Morphology.opening(image, strel));

	assertNotNull(Morphology.gradient(image, strel));
	assertNotNull(Morphology.internalGradient(image, strel));
	assertNotNull(Morphology.externalGradient(image, strel));
	assertNotNull(Morphology.laplacian(image, strel));
	
	assertNotNull(Morphology.blackTopHat(image, strel));
	assertNotNull(Morphology.whiteTopHat(image, strel));

}
 
Example #22
Source File: MorphologyTest.java    From MorphoLibJ with GNU Lesser General Public License v3.0 6 votes vote down vote up
/**
 * Tests closing can be run on an RGB image.
 */
@Test
public void testOpening_Square_RGB() {
	String fileName = getClass().getResource("/files/peppers-crop.png").getFile();
	ImagePlus imagePlus = IJ.openImage(fileName);
	assertNotNull(imagePlus);
	ColorProcessor image = (ColorProcessor) imagePlus.getProcessor();
	
	Strel strel = SquareStrel.fromDiameter(5);
	ColorProcessor result = (ColorProcessor) Morphology.opening(image, strel);
	assertNotNull(result);
	
	// Check that result is lower than or equal to the original image
	int width = image.getWidth();
	int height = image.getHeight();
	int[] rgb0 = new int[3];
	int[] rgb = new int[3];
	for (int y = 0; y < height; y++) {
		for (int x = 0; x < width; x++) {
			image.getPixel(x, y, rgb0);
			result.getPixel(x, y, rgb);
			for (int c = 0; c < 3; c++)
				assertTrue(rgb[c] <= rgb0[c]);
		}
	}
}
 
Example #23
Source File: ColorImages.java    From MorphoLibJ with GNU Lesser General Public License v3.0 6 votes vote down vote up
/**
 * Applies an overlay of a binary image mask onto a grayscale or color
 * image, using the specified color. Both images must have the same size.
 * 
 * @param refImage
 *            the original image used as background
 * @param mask
 *            the binary mask image
 * @param color
 *            the color used to display overlay
 * @return a new ImagePlus instance containing a 2D color image
 */
public final static ImageProcessor binaryOverlay(ImageProcessor refImage, 
		ImageProcessor mask, Color color)
{
	if (refImage instanceof ColorProcessor) 
	{
		return binaryOverlayRGB(refImage, mask, color);
	}
	else
	{
		if (!(refImage instanceof ByteProcessor)) 
		{
			refImage = refImage.convertToByteProcessor();
		}
		return binaryOverlayGray8(refImage, mask, color);
	}
}
 
Example #24
Source File: FloatProcessorT2.java    From TrakEM2 with GNU General Public License v3.0 5 votes vote down vote up
public FloatProcessorT2(final ColorProcessor cp, final int channel) {
	this(cp.getWidth(), cp.getHeight(), 0, 255);
	final int[] c = (int[])cp.getPixels();
	int bitmask = 0;
	int shift = 0;
	switch (channel) {
		case 0: bitmask = 0x00ff0000; shift = 16; break; // red
		case 1: bitmask = 0x0000ff00; shift =  8; break; // green
		case 2: bitmask = 0x000000ff; break; // blue
	}
	final float[] pixels = (float[])this.getPixels(); // I luv pointlessly private fields
	for (int i=0; i<pixels.length; i++) pixels[i] = ((c[i] & bitmask)>>shift);
	super.setMinAndMax(0, 255); // we know them
}
 
Example #25
Source File: LUTRed.java    From TrakEM2 with GNU General Public License v3.0 5 votes vote down vote up
@Override
public ImageProcessor process(ImageProcessor ip) {
	if (ip instanceof ColorProcessor) {
		Utils.log("Ignoring " + getClass().getSimpleName() + " filter for RGB image");
		return ip;
	}
	byte[] s = new byte[256];
	for (int i=0; i<256; ++i) s[i] = (byte)i;
	ip.setColorModel(new IndexColorModel(8, 256, s, new byte[256], new byte[256]));
	return ip;
}
 
Example #26
Source File: FSLoader.java    From TrakEM2 with GNU General Public License v3.0 5 votes vote down vote up
boolean save(final BufferedImage bi, final String path, final float quality, final boolean as_grey) {
	switch (bi.getType()) {
		case BufferedImage.TYPE_BYTE_GRAY:
			return save(new ByteProcessor(bi), path, quality, false);
		default:
			if (as_grey) return save(new ByteProcessor(bi), path, quality, false);
			return save(new ColorProcessor(bi), path, quality, false);
	}
}
 
Example #27
Source File: LUTMagenta.java    From TrakEM2 with GNU General Public License v3.0 5 votes vote down vote up
@Override
public ImageProcessor process(ImageProcessor ip) {
	if (ip instanceof ColorProcessor) {
		Utils.log("Ignoring " + getClass().getSimpleName() + " filter for RGB image");
		return ip;
	}
	byte[] s = new byte[256];
	for (int i=0; i<256; ++i) s[i] = (byte)i;
	ip.setColorModel(new IndexColorModel(8, 256, s, new byte[256], s));
	return ip;
}
 
Example #28
Source File: SubtractBackground.java    From TrakEM2 with GNU General Public License v3.0 5 votes vote down vote up
@Override
public ImageProcessor process(ImageProcessor ip) {
	BackgroundSubtracter bs = new BackgroundSubtracter();
	if (ip instanceof ColorProcessor) {
		bs.subtractRGBBackround((ColorProcessor)ip, radius);
	} else {
		bs.subtractBackround(ip, radius);
	}
	return ip;
}
 
Example #29
Source File: DownsamplerTest.java    From TrakEM2 with GNU General Public License v3.0 5 votes vote down vote up
/**
	 * Test downsampling the pyramid of a short image + byte alpha channel
	 * including the byte mapping of the short doing the alpha channel in a
	 * separate loop.
	 * 
	 * @param ba
	 */
	final private static void testColorAlphaIndependently( Pair< ColorProcessor, byte[][] > ba, ByteProcessor alpha )
	{
		while( ba.a.getWidth() > 32 )
		{
			ba = Downsampler.downsampleColor( ba.a );
			alpha = Downsampler.downsampleByteProcessor( alpha );
//			new ImagePlus( "pixels " + ba.a.getWidth(), ba.a ).show();
//			new ImagePlus( "pixels to byte " + ba.a.getWidth(), new ByteProcessor( ba.a.getWidth(), ba.a.getHeight(), ba.b[ 0 ], null ) ).show();
//			new ImagePlus( "alpha " + ba.a.getWidth(), new ByteProcessor( ba.a.getWidth(), ba.a.getHeight(), ba.b[ 1 ], null ) ).show();
		}
	}
 
Example #30
Source File: LabelImagesTest.java    From MorphoLibJ with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Test
public final void testLabelToRGB_ImageProcessorByteArrayColor()
{
	// create a byte processor containing four labels
	ImageProcessor image = new ByteProcessor(10, 10);
	for (int y = 0; y < 3; y++)
	{
		for (int x = 0; x < 3; x++)
		{
			image.set(x + 1, y + 1, 1);
			image.set(x + 5, y + 1, 2);
			image.set(x + 1, y + 5, 3);
			image.set(x + 5, y + 5, 4);
		}
	}
	
	// create LUT and background color
	byte[][] lut = CommonLabelMaps.GOLDEN_ANGLE.computeLut(4, false);
	Color bgColor = Color.WHITE;
	
	// compute color image from labels
	ColorProcessor colorImage = LabelImages.labelToRgb(image, lut, bgColor);

	Assert.assertNotEquals(0, colorImage.get(2, 2));
	Assert.assertNotEquals(0, colorImage.get(6, 2));
	Assert.assertNotEquals(0, colorImage.get(2, 6));
	Assert.assertNotEquals(0, colorImage.get(6, 6));
}