Java Code Examples for ij.process.ColorProcessor#getHeight()

The following examples show how to use ij.process.ColorProcessor#getHeight() . 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: 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 2
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 3
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 4
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 5
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 6
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 7
Source File: ImagePlusMatConverter.java    From IJ-OpenCV with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Duplicates {@link ColorProcessor} to the corresponding OpenCV image of
 * type {@link Mat}.
 *
 * @param cp The {@link ColorProcessor} to be converted
 * @return The OpenCV image (of type {@link Mat})
 */
public static Mat toMat(ColorProcessor cp) {
    final int w = cp.getWidth();
    final int h = cp.getHeight();
    final int[] pixels = (int[]) cp.getPixels();
    byte[] bData = new byte[w * h * 3];

    // convert int-encoded RGB values to byte array
    for (int i = 0; i < pixels.length; i++) {
        bData[i * 3 + 0] = (byte) ((pixels[i] >> 16) & 0xFF);	// red
        bData[i * 3 + 1] = (byte) ((pixels[i] >> 8) & 0xFF);	// grn
        bData[i * 3 + 2] = (byte) ((pixels[i]) & 0xFF);	// blu
    }
    return new Mat(h, w, opencv_core.CV_8UC3, new BytePointer(bData));
}
 
Example 8
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 9
Source File: ArgbRenderer.java    From render with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Converts the processor to an ARGB image.
 *
 * @param  renderedImageProcessorWithMasks  processor to convert.
 * @param  binaryMask                       indicates whether a binary mask should be applied.
 *
 * @return the converted image.
 */
public static BufferedImage targetToARGBImage(final ImageProcessorWithMasks renderedImageProcessorWithMasks,
                                              final boolean binaryMask) {

    // convert to 24bit RGB
    final ColorProcessor cp = renderedImageProcessorWithMasks.ip.convertToColorProcessor();

    // set alpha channel
    final int[] cpPixels = (int[]) cp.getPixels();
    final byte[] alphaPixels;

    if (renderedImageProcessorWithMasks.mask != null) {
        alphaPixels = (byte[]) renderedImageProcessorWithMasks.mask.getPixels();
    } else if (renderedImageProcessorWithMasks.outside != null) {
        alphaPixels = (byte[]) renderedImageProcessorWithMasks.outside.getPixels();
    } else {
        alphaPixels = null;
    }

    if (alphaPixels == null) {
        for (int i = 0; i < cpPixels.length; ++i) {
            cpPixels[i] &= 0xffffffff;
        }
    } else if (binaryMask) {
        for (int i = 0; i < cpPixels.length; ++i) {
            if (alphaPixels[i] == -1)
                cpPixels[i] &= 0xffffffff;
            else
                cpPixels[i] &= 0x00ffffff;
        }
    } else {
        for (int i = 0; i < cpPixels.length; ++i) {
            cpPixels[i] &= 0x00ffffff | (alphaPixels[i] << 24);
        }
    }

    final BufferedImage image = new BufferedImage(cp.getWidth(), cp.getHeight(), BufferedImage.TYPE_INT_ARGB);
    final WritableRaster raster = image.getRaster();
    raster.setDataElements(0, 0, cp.getWidth(), cp.getHeight(), cpPixels);

    return image;
}
 
Example 10
Source File: IntegralImageMipMaps.java    From TrakEM2 with GNU General Public License v3.0 4 votes vote down vote up
@SuppressWarnings({ "unused", "unchecked", "null" })
private static final BufferedImage[] createRGB(
		final Patch patch,
		final ColorProcessor ip,
		final ByteProcessor mask) {
	final int w = ip.getWidth();
	final int h = ip.getHeight();
	final int[] dims = new int[]{w, h};
	final ScaleAreaAveraging2d<LongType, UnsignedByteType> saar, saag, saab, saam;
	{
		// Split color channels
		final int[] p = (int[]) ip.getPixels();
		final byte[] r = new byte[p.length],
		             g = new byte[p.length],
		             b = new byte[p.length];
		for (int i=0; i<p.length; ++i) {
			final int a = p[i];
			r[i] = (byte)((a >> 16)&0xff);
			g[i] = (byte)((a >> 8)&0xff);
			b[i] = (byte)(a&0xff);
		}
		//
		saar = saa(r, dims);
		saag = saa(g, dims);
		saab = saa(b, dims);
		saam = null == mask? null : saa((byte[])mask.getPixels(), dims);
	}
	
	// Generate images
	final BufferedImage[] bis = new BufferedImage[Loader.getHighestMipMapLevel(patch) + 1];
	//
	if (null == saam) { // mask is null
		bis[0] = ImageSaver.createARGBImage((int[])ip.getPixels(), w, h); // sharing the int[] pixels
		for (int i=1; i<bis.length; i++) {
			final int K = (int) Math.pow(2, i),
		          wk = w / K,
		          hk = h / K;
			// An image of the scaled size
			saar.setOutputDimensions(wk, hk);
			saar.process();
			saag.setOutputDimensions(wk, hk);
			saag.process();
			saab.setOutputDimensions(wk, hk);
			saab.process();
			bis[i] = ImageSaver.createARGBImage(
				blend(((Array<UnsignedByteType,ByteArray>) saar.getResult().getContainer()).update(null).getCurrentStorageArray(),
					  ((Array<UnsignedByteType,ByteArray>) saag.getResult().getContainer()).update(null).getCurrentStorageArray(),
					  ((Array<UnsignedByteType,ByteArray>) saab.getResult().getContainer()).update(null).getCurrentStorageArray()),
				wk, hk);
		}
	} else {
		// With alpha channel
		bis[0] = ImageSaver.createARGBImage(blend((int[])ip.getPixels(), (byte[])mask.getPixels()), w, h); // sharing the int[] pixels
		for (int i=1; i<bis.length; i++) {
			final int K = (int) Math.pow(2, i),
		          wk = w / K,
		          hk = h / K;
			// An image of the scaled size
			saar.setOutputDimensions(wk, hk);
			saar.process();
			saag.setOutputDimensions(wk, hk);
			saag.process();
			saab.setOutputDimensions(wk, hk);
			saab.process();
			saam.setOutputDimensions(wk, hk);
			saam.process();
			bis[i] = ImageSaver.createARGBImage(
				blend(((Array<UnsignedByteType,ByteArray>) saar.getResult().getContainer()).update(null).getCurrentStorageArray(),
					  ((Array<UnsignedByteType,ByteArray>) saag.getResult().getContainer()).update(null).getCurrentStorageArray(),
					  ((Array<UnsignedByteType,ByteArray>) saab.getResult().getContainer()).update(null).getCurrentStorageArray(),
					  ((Array<UnsignedByteType,ByteArray>) saam.getResult().getContainer()).update(null).getCurrentStorageArray()),
				wk, hk);
		}
	}
	
	return bis;
}
 
Example 11
Source File: IntegralImageMipMaps.java    From TrakEM2 with GNU General Public License v3.0 4 votes vote down vote up
private static final ImageBytes[] fastCreateRGB(
		final Patch patch,
		final ColorProcessor ip,
		final ByteProcessor mask) {
	final int w = ip.getWidth();
	final int h = ip.getHeight();
	
	final long[] ir, ig, ib, im;
	{
		// Split color channels
		final int[] p = (int[]) ip.getPixels();
		final byte[] r = new byte[p.length],
		             g = new byte[p.length],
		             b = new byte[p.length];
		for (int i=0; i<p.length; ++i) {
			final int a = p[i];
			r[i] = (byte)((a >> 16)&0xff);
			g[i] = (byte)((a >> 8)&0xff);
			b[i] = (byte)(a&0xff);
		}
		//
		ir = FastIntegralImage.longIntegralImage(r, w, h);
		ig = FastIntegralImage.longIntegralImage(g, w, h);
		ib = FastIntegralImage.longIntegralImage(b, w, h);
		im = null == mask ? null : FastIntegralImage.longIntegralImage((byte[])mask.getPixels(), w, h);
	}
	
	// Generate images
	final ImageBytes[] bis = new ImageBytes[Loader.getHighestMipMapLevel(patch) + 1];
	//
	if (null == mask) {
		bis[0] = new ImageBytes(P.asRGBBytes((int[])ip.getPixels()), ip.getWidth(), ip.getHeight());
		for (int i=1; i<bis.length; i++) {
			final int K = (int) Math.pow(2, i),
		          wk = w / K,
		          hk = h / K;
			// An image of the scaled size
			bis[i] = new ImageBytes(new byte[][]{FastIntegralImage.scaleAreaAverage(ir, w+1, h+1, wk, hk),
			                                     FastIntegralImage.scaleAreaAverage(ig, w+1, h+1, wk, hk),
			                                     FastIntegralImage.scaleAreaAverage(ib, w+1, h+1, wk, hk)},
			                        wk, hk);
		}
	} else {
		// With alpha channel
		bis[0] = new ImageBytes(P.asRGBABytes((int[])ip.getPixels(), (byte[])mask.getPixels(), null), ip.getWidth(), ip.getHeight());
		for (int i=1; i<bis.length; i++) {
			final int K = (int) Math.pow(2, i),
		          wk = w / K,
		          hk = h / K;
			// An image of the scaled size
			bis[i] = new ImageBytes(new byte[][]{FastIntegralImage.scaleAreaAverage(ir, w+1, h+1, wk, hk),
			                                     FastIntegralImage.scaleAreaAverage(ig, w+1, h+1, wk, hk),
			                                     FastIntegralImage.scaleAreaAverage(ib, w+1, h+1, wk, hk),
			                                     FastIntegralImage.scaleAreaAverage(im, w+1, h+1, wk, hk)},
			                        wk, hk);
		}
	}

	return bis;
}
 
Example 12
Source File: DownsamplerMipMaps.java    From TrakEM2 with GNU General Public License v3.0 4 votes vote down vote up
static private final ImageBytes asBytes(final ColorProcessor cp) {
	return new ImageBytes(P.asRGBBytes((int[])cp.getPixels()), cp.getWidth(), cp.getHeight());
}
 
Example 13
Source File: DownsamplerMipMaps.java    From TrakEM2 with GNU General Public License v3.0 4 votes vote down vote up
static private final ImageBytes asBytes(final ColorProcessor cp, final ByteProcessor mask) {
	return new ImageBytes(P.asRGBABytes((int[])cp.getPixels(), (byte[])mask.getPixels(), null), cp.getWidth(), cp.getHeight());
}