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

The following examples show how to use ij.process.ImageProcessor#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: LabelImages.java    From MorphoLibJ with GNU Lesser General Public License v3.0 6 votes vote down vote up
/**
 * Replace all values specified in label array by the value 0.
 *  
 * @param image a label planar image
 * @param labels the list of labels to replace 
 * @param newLabel the new value for labels 
 */
public static final void replaceLabels(ImageProcessor image, float[] labels, float newLabel)
{
	int sizeX = image.getWidth();
	int sizeY = image.getHeight();
	
	TreeSet<Float> labelSet = new TreeSet<Float>();
	for (int i = 0; i < labels.length; i++)
	{
		labelSet.add(labels[i]);
	}
	
	for (int y = 0; y < sizeY; y++)
	{
		for (int x = 0; x < sizeX; x++)
		{
			float value = image.getf(x, y); 
			if (value == newLabel)
				continue;
			if (labelSet.contains(value)) 
				image.setf(x, y, newLabel);
		}
	}
}
 
Example 2
Source File: Loader.java    From TrakEM2 with GNU General Public License v3.0 6 votes vote down vote up
static public ImageProcessor scaleImage(final ImagePlus imp, final int level, final boolean quality) {
	if (level <= 0) return imp.getProcessor();
	// else, make a properly scaled image:
	//  - gaussian blurred for best quality when resizing with nearest neighbor
	//  - direct nearest neighbor otherwise
	ImageProcessor ip = imp.getProcessor();
	final int w = ip.getWidth();
	final int h = ip.getHeight();
	final double mag = 1 / Math.pow(2, level);
	// TODO releseToFit !
	if (quality) {
		// apply proper gaussian filter
		final double sigma = Math.sqrt(Math.pow(2, level) - 0.25); // sigma = sqrt(level^2 - 0.5^2)
		ip = new FloatProcessorT2(w, h, ImageFilter.computeGaussianFastMirror(new FloatArray2D((float[])ip.convertToFloat().getPixels(), w, h), (float)sigma).data, ip.getDefaultColorModel(), ip.getMin(), ip.getMax());
		ip = ip.resize((int)(w * mag), (int)(h * mag)); // better while float
		return Utils.convertTo(ip, imp.getType(), false);
	} else {
		return ip.resize((int)(w * mag), (int)(h * mag));
	}
}
 
Example 3
Source File: GeodesicDistanceTransformFloat5x5.java    From MorphoLibJ with GNU Lesser General Public License v3.0 6 votes vote down vote up
private FloatProcessor initialize(ImageProcessor marker)
{
	// size of image
	sizeX = marker.getWidth();
	sizeY = marker.getHeight();
	
	FloatProcessor distMap = new FloatProcessor(sizeX, sizeY);
	distMap.setValue(0);
	distMap.fill();

	// initialize empty image with either 0 (foreground) or NaN (background)
	for (int y = 0; y < sizeY; y++) 
	{
		for (int x = 0; x < sizeX; x++) 
		{
			int val = marker.get(x, y) & 0x00ff;
			distMap.setf(x, y, val == 0 ? Float.POSITIVE_INFINITY : 0);
		}
	}

	return distMap;
}
 
Example 4
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 5
Source File: ConnectedComponents.java    From MorphoLibJ with GNU Lesser General Public License v3.0 6 votes vote down vote up
/**
 * Computes maximum value in the input 2D image.
 * This method is used to compute display range of result ImagePlus.
 */
private final static int findMax(ImageProcessor image) 
{
	// get image size
	int sizeX = image.getWidth();
	int sizeY = image.getHeight();
	
	// find maximum value over voxels
	int maxVal = 0;
	for (int y = 0; y < sizeY; y++) 
	{
		IJ.showProgress(y, sizeY);
		for (int x = 0; x < sizeX; x++) 
		{
			maxVal = Math.max(maxVal, image.get(x, y));
		}
	}
	IJ.showProgress(1);
	
	return maxVal;
}
 
Example 6
Source File: LabelImages.java    From MorphoLibJ with GNU Lesser General Public License v3.0 6 votes vote down vote up
public static final ImageProcessor binarize(ImageProcessor image, int label)
{
	int sizeX = image.getWidth();
	int sizeY = image.getHeight();

	ByteProcessor result = new ByteProcessor( sizeX, sizeY );
	for (int y = 0; y < sizeY; y++)
	{
		for (int x = 0; x < sizeX; x++)
		{
			// process only specified label
			int val = (int) image.getf(x, y);
			if (val == label)
			{
				result.set(x, y, 255);
			}
		}
	}
	return result;
}
 
Example 7
Source File: Distortion_Correction.java    From TrakEM2 with GNU General Public License v3.0 6 votes vote down vote up
ImageProcessor applyTransformToImageInverse(
		final AbstractAffineModel2D< ? > a, final ImageProcessor ip){
	final ImageProcessor newIp = ip.duplicate();
	newIp.max(0.0);

	for (int x=0; x<ip.getWidth(); x++){
		for (int y=0; y<ip.getHeight(); y++){
			final double[] position = new double[]{x,y};
			//				float[] newPosition = a.apply(position);
			double[] newPosition = new double[]{0,0};
			try
			{
				newPosition = a.applyInverse(position);
			}
			catch ( final NoninvertibleModelException e ) {}

			final int xn = (int) newPosition[0];
			final int yn = (int) newPosition[1];

			if ( (xn >= 0) && (yn >= 0) && (xn < ip.getWidth()) && (yn < ip.getHeight()))
				newIp.set(xn,yn,ip.get(x,y));

		}
	}
	return newIp;
}
 
Example 8
Source File: DistanceTransform5x5Short.java    From MorphoLibJ with GNU Lesser General Public License v3.0 6 votes vote down vote up
private ShortProcessor initializeResult(ImageProcessor labelImage)
{
	this.fireStatusChanged(new AlgoEvent(this, "Initialization"));

	// size of image
	int sizeX = labelImage.getWidth();
	int sizeY = labelImage.getHeight();

	// create new empty image, and fill it with black
	ShortProcessor distMap = new ShortProcessor(sizeX, sizeY);
	distMap.setValue(0);
	distMap.fill();

	// initialize empty image with either 0 (background) or Inf (foreground)
	for (int y = 0; y < sizeY; y++) 
	{
		for (int x = 0; x < sizeX; x++)
		{
			int label = (int) labelImage.getf(x, y);
			distMap.set(x, y, label == 0 ? 0 : Short.MAX_VALUE);
		}
	}
	
	return distMap;
}
 
Example 9
Source File: BinaryOrbit.java    From orbit-image-analysis with GNU General Public License v3.0 6 votes vote down vote up
public void fill(ImageProcessor ip, int foreground, int background) {
	int width = ip.getWidth();
	int height = ip.getHeight();
	FloodFiller ff = new FloodFiller(ip);
	ip.setColor(127);
	for (int y=0; y<height; y++) {
		if (ip.getPixel(0,y)==background) ff.fill(0, y);
		if (ip.getPixel(width-1,y)==background) ff.fill(width-1, y);
	}
	for (int x=0; x<width; x++){
		if (ip.getPixel(x,0)==background) ff.fill(x, 0);
		if (ip.getPixel(x,height-1)==background) ff.fill(x, height-1);
	}
	byte[] pixels = (byte[])ip.getPixels();
	int n = width*height;
	for (int i=0; i<n; i++) {
		if (pixels[i]==127)
			pixels[i] = (byte)background;
		else
			pixels[i] = (byte)foreground;
	}
}
 
Example 10
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 11
Source File: MorphologicalSegmentationTest.java    From MorphoLibJ with GNU Lesser General Public License v3.0 5 votes vote down vote up
private int diff(final ImageProcessor a, final ImageProcessor b) {
	int count = 0;
	final int width = a.getWidth(), height = a.getHeight();
	for (int y = 0; y < height; y++) {
		for (int x = 0; x < width; x++) {
			if (a.getf(x, y) != b.getf(x, y)) count++;
		}
	}
	return count;
}
 
Example 12
Source File: ShiftedCross3x3Strel_RightTest.java    From MorphoLibJ with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Test
public void testDilation_Square4x4() {
	ImageProcessor image = createImage_Square4x4();
	Strel strel = ShiftedCross3x3Strel.RIGHT;
	
	ImageProcessor expected = image.createProcessor(10, 10);
	for (int x = 2; x < 6; x++) {
		expected.set(x, 2, 255);
		expected.set(x, 7, 255);
	}
	for (int y = 3; y < 7; y++) {
		for (int x = 1; x < 7; x++) {
			expected.set(x, y, 255);
		}
	}

	ImageProcessor result = strel.dilation(image);
	
	for (int y = 0; y < image.getHeight(); y++) {
		for (int x = 0; x < image.getWidth(); x++) {
			int exp = expected.get(x, y);
			int res = result.get(x, y);
			if(expected.get(x, y) != result.get(x, y)) {
				System.out.println("At x=" + x + " and y=" + y
						+ ", exp=" + exp + " and res = " + res);
			}
			assertEquals(exp, res);
		}			
	}
}
 
Example 13
Source File: ColorImages.java    From MorphoLibJ with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * Creates a new ColorProcessor from the red, green and blue channels. Each
 * channel must be an instance of ByteProcessor.
 * 
 * @param red
 *            the image for the red channel (must be a ByteProcessor)
 * @param green
 *            the image for the green channel (must be a ByteProcessor)
 * @param blue
 *            the image for the blue channel (must be a ByteProcessor)
 * @return the color image corresponding to the concatenation of the three
 *         channels
 * @throws IllegalArgumentException
 *             if one of the channel is not an instance of ByteProcessor
 */
public static final ColorProcessor mergeChannels(ImageProcessor red, 
		ImageProcessor green, ImageProcessor blue)
{
	// check validity of input
	if (!(red instanceof ByteProcessor))
		throw new IllegalArgumentException("Input channels must be instances of ByteProcessor");
	if (!(green instanceof ByteProcessor))
		throw new IllegalArgumentException("Input channels must be instances of ByteProcessor");
	if (!(blue instanceof ByteProcessor))
		throw new IllegalArgumentException("Input channels must be instances of ByteProcessor");
	
	// Extract byte array of each channel
	byte[] redArray 	= (byte[]) red.getPixels();
	byte[] greenArray 	= (byte[]) green.getPixels();
	byte[] blueArray 	= (byte[]) blue.getPixels();
	
	// get image size
	int width = red.getWidth();
	int height = red.getHeight();

	// create result color image
	ColorProcessor result = new ColorProcessor(width, height);
	result.setRGB(redArray, greenArray, blueArray);
	
	return result;	
}
 
Example 14
Source File: LabelImages.java    From MorphoLibJ with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * Creates a new Color image from a label planar image, a LUT, and a 
 * color for background.
 * 
 * @param image an ImageProcessor with label values and 0 for background
 * @param lut the array of color components for each label 
 * @param bgColor the background color
 * @return a new instance of ColorProcessor
 */
public static final ColorProcessor labelToRgb(ImageProcessor image, byte[][] lut, Color bgColor) 
{
	int width = image.getWidth();
	int height = image.getHeight();
	
	int bgColorCode = bgColor.getRGB();
	
	ColorProcessor result = new ColorProcessor(width, height);
	for (int y = 0; y < height; y++) 
	{
		for (int x = 0; x < width; x++) 
		{
			int index = (int) image.getf(x, y);
			if (index == 0) 
			{
				result.set(x, y, bgColorCode);
			} 
			else 
			{
				byte[] rgb = lut[index - 1];
				int color = (int) ((rgb[0] & 0xFF) << 16
						| (rgb[1] & 0xFF) << 8 | (rgb[2] & 0xFF));
				result.set(x, y, color);
			}
		}
	}
	
	return result;
}
 
Example 15
Source File: MinimaAndMaxima.java    From MorphoLibJ with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * Imposes the maxima given by marker image into the input image, using
 * the specified connectivity.
 * 
 * @param image
 *            the image to process
 * @param maxima
 *            a binary image of maxima 
 * @param conn
 *            the connectivity for maxima, that should be either 4 or 8
 * @return the result of maxima imposition
 */
public final static ImageProcessor imposeMaxima(ImageProcessor image,
		ImageProcessor maxima, int conn)
{
	ImageProcessor marker = image.duplicate();
	ImageProcessor mask = image.duplicate();
	
	int width = image.getWidth();
	int height = image.getHeight();
	for (int y = 0; y < height; y++)
	{
		for (int x = 0; x < width; x++)
		{
			if (maxima.get(x, y) > 0)
			{
				marker.set(x, y, 255);
				mask.set(x, y, 255);
			} 
			else
			{
				marker.set(x, y, 0);
				mask.set(x, y, image.get(x, y)-1);
			}
		}
	}
	
	return Reconstruction.reconstructByDilation(marker, mask, conn);
}
 
Example 16
Source File: LabelImages.java    From MorphoLibJ with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * Returns a binary image that contains only the largest label.
 * 
 * @param image
 *            a label image
 * @return a new image containing only the largest label
 * @throws RuntimeException if the image is empty
 */
public static final ImageProcessor keepLargestLabel(ImageProcessor image)
{
	int sizeX = image.getWidth();
	int sizeY = image.getHeight();

	ImageProcessor result = new ByteProcessor(sizeX, sizeY);
	
	// identify labels of input image
	int[] labels = findAllLabels(image);
	if (labels.length == 0)
	{
		throw new RuntimeException("Can not select a label in an empty image");
	}

	// find the label of the largest particle
	int[] areas = pixelCount(image, labels);
	int largestLabel = labels[indexOfMax(areas)];

	// convert label image to binary image
	for (int y = 0; y < sizeY; y++) 
	{
		for (int x = 0; x < sizeX; x++) 
		{
			int label = (int) image.getf(x, y); 
			if (label == largestLabel)
				result.set(x, y, 255);
			else
				result.set(x, y, 0);
		}
	}
	
	return result;
}
 
Example 17
Source File: MinimaAndMaximaTest.java    From MorphoLibJ with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Test
public final void testImposeMinimaMaximaConsistency_C8 () {
	String fileName = getClass().getResource("/files/grains-crop.png").getFile();
	ImagePlus imagePlus = IJ.openImage(fileName);
	ImageProcessor image = imagePlus.getProcessor();
	
	Strel strel = SquareStrel.fromDiameter(3);
	ImageProcessor grad = Morphology.gradient(image, strel);
	ImageProcessor markers = MinimaAndMaxima.extendedMinima(grad, 20, 8);
	
	ImageProcessor imp = MinimaAndMaxima.imposeMinima(grad, markers, 8);
	ImageProcessor rmin = MinimaAndMaxima.regionalMinima(imp, 8);

	grad.invert();
	ImageProcessor imp2 = MinimaAndMaxima.imposeMaxima(grad, markers, 8);
	ImageProcessor rmax = MinimaAndMaxima.regionalMaxima(imp2, 8);
	
	
	int width = image.getWidth();
	int height = image.getHeight();
	for (int y = 0; y < height; y++) {
		for (int x = 0; x < width; x++) {
			assertEquals("Results differ at position " + x + "," + y, 
					rmin.get(x, y), rmax.get(x, y));
		}
	}
}
 
Example 18
Source File: DirectionalFilter.java    From MorphoLibJ with GNU Lesser General Public License v3.0 4 votes vote down vote up
/**
 * Apply directional filter with current settings to the specified image.
 * 
 * @param image
 *            a grayscale image
 * @return the result of directional filter
 */
public ImageProcessor process(ImageProcessor image)
{
	// determine the sign of min/max computation
	int sign = this.type == Type.MAX ? 1 : -1;
	
	// initialize result
	ImageProcessor result = image.duplicate();
	if (this.type == Type.MAX)
	{
		result.setValue(0);
	}
	else
	{
		result.setValue(Integer.MAX_VALUE);
	}
	result.fill();
	
	int sizeX = image.getWidth();
	int sizeY = image.getHeight();
	
	fireStatusChanged(this, "Directional Filter...");

	// Iterate over the set of directions
	for (int i = 0; i < nDirections; i++)
	{
		fireProgressChanged(this, i, nDirections);
		
		// Create the structuring element for current orientation
		double theta = ((double) i) * 180.0 / nDirections;
		Strel strel = this.strelFactory.createStrel(theta);

		// Apply oriented filter
		ImageProcessor oriented = this.operation.apply(image, strel);

		// combine current result with global result
		for (int y = 0; y < sizeY; y++)
		{
			for (int x = 0; x < sizeX; x++)
			{
				float value = oriented.getf(x, y);
				if (value * sign > result.getf(x, y) * sign)
				{
					result.setf(x, y, value);
				}
			}
		}
	}
	
	fireProgressChanged(this, 1, 1);
	
	// return the min or max value computed over all orientations
	return result;
}
 
Example 19
Source File: ShiftedCross3x3Strel.java    From MorphoLibJ with GNU Lesser General Public License v3.0 4 votes vote down vote up
private void inPlaceErosionGray8(ImageProcessor image) {
	// size of image
	int width = image.getWidth();
	int height = image.getHeight();

	int[][] buffer = new int[3][width];

	// init buffer with background and first two lines
	for (int x = 0; x < width; x++) {
		buffer[0][x] = Strel.FOREGROUND;
		buffer[1][x] = Strel.FOREGROUND;
		buffer[2][x] = image.get(x, 0);
	}

	// Iterate over image lines
	int valMin;
	for (int y = 0; y < height; y++) {
		fireProgressChanged(this, y, height);
		
		// permute lines in buffer
		int[] tmp = buffer[0];
		buffer[0] = buffer[1];
		buffer[1] = buffer[2];

		// initialize values of the last line in buffer 
		if (y < height - 1) {
			for (int x = 0; x < width; x++) 
				tmp[x] = image.get(x, y+1);
		} else {
			for (int x = 0; x < width; x++) 
				tmp[x] = Strel.FOREGROUND;
		}
		buffer[2] = tmp;

		// Iterate over pixels of the line
		for (int x = 0; x < width - 2; x++) {
			valMin = min5(buffer[0][x+1], buffer[1][x], buffer[1][x+1], 
					buffer[1][x+2], buffer[2][x+1]);
			image.set(x, y, valMin);
		}

		// process last two pixels independently
		valMin = min5(buffer[0][width-1], buffer[1][width-2], 
				buffer[1][width-1], buffer[2][width-1], Strel.FOREGROUND);
		image.set(width-2, y, valMin);
		valMin = Math.min(buffer[1][width-1], Strel.FOREGROUND);
		image.set(width-1, y, valMin);
	}
	
	// clear the progress bar
	fireProgressChanged(this, height, height);
}
 
Example 20
Source File: LabelImages.java    From MorphoLibJ with GNU Lesser General Public License v3.0 4 votes vote down vote up
/**
 * Returns a binary image that contains only the selected particle or
 * region, by automatically cropping the image and eventually adding some
 * borders.
 * 
 * @param image a, image containing label of particles
 * @param label the label of the particle to select
 * @param border the number of pixels to add to each side of the particle
 * @return a smaller binary image containing only the selected particle
 */
public static final ImageProcessor cropLabel(ImageProcessor image, int label, int border) 
{
	// image size
	int sizeX = image.getWidth();
	int sizeY = image.getHeight();
	
	// Initialize label bounds
	int xmin = Integer.MAX_VALUE;
	int xmax = Integer.MIN_VALUE;
	int ymin = Integer.MAX_VALUE;
	int ymax = Integer.MIN_VALUE;
	
	// update bounds by iterating on voxels 
	for (int y = 0; y < sizeY; y++)
	{
		for (int x = 0; x < sizeX; x++)
		{
			// process only specified label
			int val = image.get(x, y);
			if (val != label)
			{
				continue;
			}

			// update bounds of current label
			xmin = min(xmin, x);
			xmax = max(xmax, x);
			ymin = min(ymin, y);
			ymax = max(ymax, y);
		}
	}

	// Compute size of result, taking into account border
	int sizeX2 = (xmax - xmin + 1 + 2 * border);
	int sizeY2 = (ymax - ymin + 1 + 2 * border);

	// allocate memory for result image
	ImageProcessor result = new ByteProcessor(sizeX2, sizeY2);
	
	// fill result with binary label
	for (int y = ymin, y2 = border; y <= ymax; y++, y2++) 
	{
		for (int x = xmin, x2 = border; x <= xmax; x++, x2++) 
		{
			if ((image.get(x, y)) == label)
			{
				result.set(x2, y2, 255);
			}
		}
	}

	return result;
}