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

The following examples show how to use ij.process.ImageProcessor#setColor() . 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: 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 2
Source File: TestImageAccessor.java    From Colocalisation_Analysis with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Creates a mask image with a black background and a white
 * rectangular foreground.
 *
 * @param width The width of the result image.
 * @param height The height of the result image.
 * @param offset The offset of the rectangular mask.
 * @param size The size of the rectangular mask.
 * @return A black image with a white rectangle on it.
 */
public static <T extends RealType<T> & NativeType<T>> RandomAccessibleInterval<T> createRectengularMaskImage(
		long width, long height, long[] offset, long[] size) {
	/* For now (probably until ImageJ2 is out) we use an
	 * ImageJ image to draw lines.
	 */
	int options = NewImage.FILL_BLACK + NewImage.CHECK_AVAILABLE_MEMORY;
        ImagePlus img = NewImage.createByteImage("Noise", (int)width, (int)height, 1, options);
	ImageProcessor imp = img.getProcessor();
	imp.setColor(Color.WHITE);
	Roi rect = new Roi(offset[0], offset[1], size[0], size[1]);

	imp.fill(rect);
	// we changed the data, so update it
	img.updateImage();

	return ImagePlusAdapter.wrap(img);
}
 
Example 3
Source File: TestImageAccessor.java    From Colocalisation_Analysis with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Creates a noisy image that is created by repeatedly adding points
 * with random intensity to the canvas. That way it tries to mimic the
 * way a microscope produces images.
 *
 * @param <T> The wanted output type.
 * @param width The image width.
 * @param height The image height.
 * @param dotSize The size of the dots.
 * @param numDots The number of dots.
 * @param smoothingSigma The two dimensional sigma for smoothing.
 * @return The noise image.
 */
public static <T extends RealType<T> & NativeType<T>> RandomAccessibleInterval<T> produceNoiseImage(int width,
		int height, float dotSize, int numDots) {
	/* For now (probably until ImageJ2 is out) we use an
	 * ImageJ image to draw circles.
	 */
	int options = NewImage.FILL_BLACK + NewImage.CHECK_AVAILABLE_MEMORY;
        ImagePlus img = NewImage.createByteImage("Noise", width, height, 1, options);
	ImageProcessor imp = img.getProcessor();

	float dotRadius = dotSize * 0.5f;
	int dotIntSize = (int) dotSize;

	for (int i=0; i < numDots; i++) {
		int x = (int) (Math.random() * width - dotRadius);
		int y = (int) (Math.random() * height - dotRadius);
		imp.setColor(Color.WHITE);
		imp.fillOval(x, y, dotIntSize, dotIntSize);
	}
	// we changed the data, so update it
	img.updateImage();
	// create the new image
	RandomAccessibleInterval<T> noiseImage = ImagePlusAdapter.wrap(img);

	return noiseImage;
}
 
Example 4
Source File: BinaryOrbit.java    From orbit-image-analysis with GNU General Public License v3.0 5 votes vote down vote up
ImageProcessor expand(ImageProcessor ip, boolean hasEdgePixels) {
	if (hasEdgePixels) {
		ImageProcessor ip2 = ip.createProcessor(ip.getWidth()+2, ip.getHeight()+2);
		if (foreground==0) {
			ip2.setColor(255);
			ip2.fill();
		}
		ip2.insert(ip, 1, 1);
		//new ImagePlus("ip2", ip2).show();
		return ip2;
	} else
		return ip;
}
 
Example 5
Source File: FloodFillTest.java    From MorphoLibJ with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * Test method for {@link ijt.binary.FloodFiller#fill(int, int, int)}.
 */
@Test
public final void testFloodFill_FullImage() {
	ImageProcessor image = new ByteProcessor(10, 10);
	image.setColor(8);
	image.fill();
	
	FloodFill.floodFill(image, 3, 3, 12, 4);
	
	for (int y = 0; y < 10; y++) {
		for (int x = 0; x < 10; x++) {
			assertEquals(12, image.get(x, y));
		}			
	}
}
 
Example 6
Source File: TestImageAccessor.java    From Colocalisation_Analysis with GNU General Public License v3.0 5 votes vote down vote up
/**
 * This method creates a noise image that is made of many little
 * sticks oriented in a random direction. How many of them and
 * what the length of them are can be specified.
 *
 * @return a new noise image that is not smoothed
 */
public static <T extends RealType<T> & NativeType<T>> RandomAccessibleInterval<T> produceSticksNoiseImage(int width,
		int height, int numSticks, int lineWidth, double maxLength) {
	/* For now (probably until ImageJ2 is out) we use an
	 * ImageJ image to draw lines.
	 */
	int options = NewImage.FILL_BLACK + NewImage.CHECK_AVAILABLE_MEMORY;
        ImagePlus img = NewImage.createByteImage("Noise", width, height, 1, options);
	ImageProcessor imp = img.getProcessor();
	imp.setColor(Color.WHITE);
	imp.setLineWidth(lineWidth);

	for (int i=0; i < numSticks; i++) {
		// find random starting point
		int x = (int) (Math.random() * width);
		int y = (int) (Math.random() * height);
		// create random stick length and direction
		double length = Math.random() * maxLength;
		double angle = Math.random() * 2 * Math.PI;
		// calculate random point on circle, for the direction
		int destX = x + (int) (length * Math.cos(angle));
		int destY = y + (int) (length * Math.sin(angle));
		// now draw the line
		imp.drawLine(x, y, destX, destY);
	}
	// we changed the data, so update it
	img.updateImage();

	return ImagePlusAdapter.wrap(img);
}
 
Example 7
Source File: ImageJRectExample.java    From tutorials with MIT License 4 votes vote down vote up
private static void drawRect(ImagePlus imp) {
    ImageProcessor ip = imp.getProcessor();
    ip.setColor(Color.BLUE);
    ip.setLineWidth(4);
    ip.drawRect(10, 10, imp.getWidth() - 20, imp.getHeight() - 20);
}
 
Example 8
Source File: GeodesicReconstructionHybridTest.java    From MorphoLibJ with GNU Lesser General Public License v3.0 4 votes vote down vote up
/**
 * Test method for {@link ijt.filter.morphology.GeodesicReconstruction#reconstructByErosion()}.
 */
@Test
public void testReconstructByErosion_FloatC8() {
	float BG = -42;
	float FG = 2500;
	float[][] data = new float[][]{
			{BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG},   
			{BG, FG, FG, BG, FG, FG, BG, FG, FG, FG, FG, FG, FG, FG, FG, BG},
			{BG, FG, FG, BG, FG, FG, BG, FG, FG, FG, FG, FG, FG, FG, FG, BG},
			{BG, FG, FG, BG, FG, FG, BG, FG, FG, BG, BG, BG, BG, FG, FG, BG},
			{BG, FG, FG, BG, FG, FG, BG, FG, FG, BG, FG, FG, BG, FG, FG, BG},
			{BG, FG, FG, BG, FG, FG, BG, FG, FG, BG, FG, FG, BG, FG, FG, BG},
			{BG, FG, FG, BG, BG, BG, BG, FG, FG, BG, FG, FG, BG, FG, FG, BG},
			{BG, FG, FG, FG, FG, FG, FG, FG, FG, BG, FG, FG, BG, FG, FG, BG},
			{BG, FG, FG, FG, FG, FG, FG, FG, FG, BG, FG, FG, BG, FG, FG, BG},
			{BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG},
	};
	int height = data.length;
	int width = data[0].length;
	ImageProcessor mask = new FloatProcessor(width, height);
	for (int y = 0; y < height; y++) {
		for (int x = 0; x < width; x++) {
			if (data[y][x] == FG)
				mask.setf(x, y, BG);
			else
				mask.setf(x, y, FG);
		}
	}
	
	ImageProcessor marker = new FloatProcessor(width, height);
	marker.setColor(FG);
	marker.fill();
	marker.setf(2, 3, BG);
	
	GeodesicReconstructionHybrid algo = new GeodesicReconstructionHybrid(
			GeodesicReconstructionType.BY_EROSION, 8);
	ImageProcessor result = algo.applyTo(marker, mask);
	
	assertEquals(16, result.getWidth());
	assertEquals(10, result.getHeight());
	assertEquals(BG, result.getf(2, 8), .01);
	assertEquals(BG, result.getf(8, 8), .01);
	assertEquals(BG, result.getf(8, 5), .01);
	assertEquals(BG, result.getf(14, 8), .01);
	assertEquals(FG, result.getf(15, 9), .01);
	assertEquals(FG, result.getf(0, 0), .01);
	assertEquals(FG, result.getf(5, 3), .01);
	assertEquals(FG, result.getf(11, 5), .01);
}
 
Example 9
Source File: GeodesicReconstructionHybridTest.java    From MorphoLibJ with GNU Lesser General Public License v3.0 4 votes vote down vote up
/**
 * Test method for {@link ijt.filter.morphology.GeodesicReconstruction#reconstructByErosion()}.
 */
@Test
public void testReconstructByErosion_FloatC4() {
	float BG = -42;
	float FG = 2500;
	float[][] data = new float[][]{
			{BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG},   
			{BG, FG, FG, BG, FG, FG, BG, FG, FG, FG, FG, FG, FG, FG, FG, BG},
			{BG, FG, FG, BG, FG, FG, BG, FG, FG, FG, FG, FG, FG, FG, FG, BG},
			{BG, FG, FG, BG, FG, FG, BG, FG, FG, BG, BG, BG, BG, FG, FG, BG},
			{BG, FG, FG, BG, FG, FG, BG, FG, FG, BG, FG, FG, BG, FG, FG, BG},
			{BG, FG, FG, BG, FG, FG, BG, FG, FG, BG, FG, FG, BG, FG, FG, BG},
			{BG, FG, FG, BG, BG, BG, BG, FG, FG, BG, FG, FG, BG, FG, FG, BG},
			{BG, FG, FG, FG, FG, FG, FG, FG, FG, BG, FG, FG, BG, FG, FG, BG},
			{BG, FG, FG, FG, FG, FG, FG, FG, FG, BG, FG, FG, BG, FG, FG, BG},
			{BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG},
	};
	int height = data.length;
	int width = data[0].length;
	ImageProcessor mask = new FloatProcessor(width, height);
	for (int y = 0; y < height; y++) {
		for (int x = 0; x < width; x++) {
			if (data[y][x] == FG)
				mask.setf(x, y, BG);
			else
				mask.setf(x, y, FG);
		}
	}
	
	ImageProcessor marker = new FloatProcessor(width, height);
	marker.setColor(FG);
	marker.fill();
	marker.setf(2, 3, BG);
	
	GeodesicReconstructionHybrid algo = new GeodesicReconstructionHybrid(
			GeodesicReconstructionType.BY_EROSION, 4);
	ImageProcessor result = algo.applyTo(marker, mask);
	
	assertEquals(16, result.getWidth());
	assertEquals(10, result.getHeight());
	assertEquals(BG, result.getf(2, 8), .01);
	assertEquals(BG, result.getf(8, 8), .01);
	assertEquals(BG, result.getf(8, 5), .01);
	assertEquals(BG, result.getf(14, 8), .01);
	assertEquals(FG, result.getf(15, 9), .01);
	assertEquals(FG, result.getf(0, 0), .01);
	assertEquals(FG, result.getf(5, 3), .01);
	assertEquals(FG, result.getf(11, 5), .01);
}
 
Example 10
Source File: GeodesicReconstructionHybridTest.java    From MorphoLibJ with GNU Lesser General Public License v3.0 4 votes vote down vote up
/**
 * Test method for {@link ijt.filter.morphology.GeodesicReconstruction#reconstructByErosion()}.
 */
@Test
public void testReconstructByErosion_C8() {
	int BG = 0;
	int FG = 255;
	int[][] data = new int[][]{
			{BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG},   
			{BG, FG, FG, BG, FG, FG, BG, BG, BG, FG, FG, FG, FG, BG, BG, BG},
			{BG, FG, FG, BG, FG, FG, BG, BG, BG, FG, FG, FG, FG, BG, BG, BG},
			{BG, FG, FG, BG, FG, FG, BG, FG, FG, BG, BG, BG, BG, FG, FG, BG},
			{BG, FG, FG, BG, FG, FG, BG, FG, FG, BG, FG, FG, BG, FG, FG, BG},
			{BG, FG, FG, BG, FG, FG, BG, FG, FG, BG, FG, FG, BG, FG, FG, BG},
			{BG, FG, FG, BG, BG, BG, BG, FG, FG, BG, FG, FG, BG, FG, FG, BG},
			{BG, BG, BG, FG, FG, FG, FG, BG, BG, BG, FG, FG, BG, FG, FG, BG},
			{BG, BG, BG, FG, FG, FG, FG, BG, BG, BG, FG, FG, BG, FG, FG, BG},
			{BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG},
	};
	int height = data.length;
	int width = data[0].length;
	ImageProcessor mask = new ByteProcessor(width, height);
	for (int y = 0; y < height; y++) {
		for (int x = 0; x < width; x++) {
			mask.set(x, y, data[y][x]);
		}
	}
	mask.invert();
	
	ImageProcessor marker = new ByteProcessor(width, height);
	marker.setColor(255);
	marker.fill();
	marker.set(2, 3, 0);
	
	GeodesicReconstructionHybrid algo = new GeodesicReconstructionHybrid(
			GeodesicReconstructionType.BY_EROSION, 8);
	ImageProcessor result = algo.applyTo(marker, mask);
	
	assertEquals(16, result.getWidth());
	assertEquals(10, result.getHeight());
	assertEquals(0, result.get(2, 6));
	assertEquals(0, result.get(4, 8));
	assertEquals(0, result.get(8, 5));
	assertEquals(0, result.get(14, 8));
	assertEquals(255, result.get(15, 9));
	assertEquals(255, result.get(0, 0));
	assertEquals(255, result.get(5, 3));
	assertEquals(255, result.get(11, 5));
}
 
Example 11
Source File: GeodesicReconstructionHybridTest.java    From MorphoLibJ with GNU Lesser General Public License v3.0 4 votes vote down vote up
/**
 * Test method for {@link ijt.filter.morphology.GeodesicReconstruction#reconstructByErosion()}.
 */
@Test
public void testReconstructByErosion_C4() {
	int BG = 0;
	int FG = 255;
	int[][] data = new int[][]{
			{BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG},   
			{BG, FG, FG, BG, FG, FG, BG, FG, FG, FG, FG, FG, FG, FG, FG, BG},
			{BG, FG, FG, BG, FG, FG, BG, FG, FG, FG, FG, FG, FG, FG, FG, BG},
			{BG, FG, FG, BG, FG, FG, BG, FG, FG, BG, BG, BG, BG, FG, FG, BG},
			{BG, FG, FG, BG, FG, FG, BG, FG, FG, BG, FG, FG, BG, FG, FG, BG},
			{BG, FG, FG, BG, FG, FG, BG, FG, FG, BG, FG, FG, BG, FG, FG, BG},
			{BG, FG, FG, BG, BG, BG, BG, FG, FG, BG, FG, FG, BG, FG, FG, BG},
			{BG, FG, FG, FG, FG, FG, FG, FG, FG, BG, FG, FG, BG, FG, FG, BG},
			{BG, FG, FG, FG, FG, FG, FG, FG, FG, BG, FG, FG, BG, FG, FG, BG},
			{BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG},
	};
	int height = data.length;
	int width = data[0].length;
	ImageProcessor mask = new ByteProcessor(width, height);
	for (int y = 0; y < height; y++) {
		for (int x = 0; x < width; x++) {
			mask.set(x, y, data[y][x]);
		}
	}
	mask.invert();
	
	ImageProcessor marker = new ByteProcessor(width, height);
	marker.setColor(255);
	marker.fill();
	marker.set(2, 3, 0);
	
	GeodesicReconstructionHybrid algo = new GeodesicReconstructionHybrid(
			GeodesicReconstructionType.BY_EROSION, 4);
	ImageProcessor result = algo.applyTo(marker, mask);
	
	assertEquals(16, result.getWidth());
	assertEquals(10, result.getHeight());
	assertEquals(0, result.get(2, 8));
	assertEquals(0, result.get(8, 8));
	assertEquals(0, result.get(8, 5));
	assertEquals(0, result.get(14, 8));
	assertEquals(FG, result.get(15, 9));
	assertEquals(FG, result.get(0, 0));
	assertEquals(FG, result.get(5, 3));
	assertEquals(FG, result.get(11, 5));
}
 
Example 12
Source File: GeodesicReconstructionScanningTest.java    From MorphoLibJ with GNU Lesser General Public License v3.0 4 votes vote down vote up
/**
 * Test method for {@link ijt.filter.morphology.GeodesicReconstruction#reconstructByErosion()}.
 */
@Test
public void testReconstructByErosion_FloatC8() {
	float BG = -42;
	float FG = 2500;
	float[][] data = new float[][]{
			{BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG},   
			{BG, FG, FG, BG, FG, FG, BG, FG, FG, FG, FG, FG, FG, FG, FG, BG},
			{BG, FG, FG, BG, FG, FG, BG, FG, FG, FG, FG, FG, FG, FG, FG, BG},
			{BG, FG, FG, BG, FG, FG, BG, FG, FG, BG, BG, BG, BG, FG, FG, BG},
			{BG, FG, FG, BG, FG, FG, BG, FG, FG, BG, FG, FG, BG, FG, FG, BG},
			{BG, FG, FG, BG, FG, FG, BG, FG, FG, BG, FG, FG, BG, FG, FG, BG},
			{BG, FG, FG, BG, BG, BG, BG, FG, FG, BG, FG, FG, BG, FG, FG, BG},
			{BG, FG, FG, FG, FG, FG, FG, FG, FG, BG, FG, FG, BG, FG, FG, BG},
			{BG, FG, FG, FG, FG, FG, FG, FG, FG, BG, FG, FG, BG, FG, FG, BG},
			{BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG},
	};
	int height = data.length;
	int width = data[0].length;
	ImageProcessor mask = new FloatProcessor(width, height);
	for (int y = 0; y < height; y++) {
		for (int x = 0; x < width; x++) {
			if (data[y][x] == FG)
				mask.setf(x, y, BG);
			else
				mask.setf(x, y, FG);
		}
	}
	
	ImageProcessor marker = new FloatProcessor(width, height);
	marker.setColor(FG);
	marker.fill();
	marker.setf(2, 3, BG);
	
	GeodesicReconstructionScanning algo = new GeodesicReconstructionScanning(
			GeodesicReconstructionType.BY_EROSION, 8);
	ImageProcessor result = algo.applyTo(marker, mask);
	
	assertEquals(16, result.getWidth());
	assertEquals(10, result.getHeight());
	assertEquals(BG, result.getf(2, 8), .01);
	assertEquals(BG, result.getf(8, 8), .01);
	assertEquals(BG, result.getf(8, 5), .01);
	assertEquals(BG, result.getf(14, 8), .01);
	assertEquals(FG, result.getf(15, 9), .01);
	assertEquals(FG, result.getf(0, 0), .01);
	assertEquals(FG, result.getf(5, 3), .01);
	assertEquals(FG, result.getf(11, 5), .01);
}
 
Example 13
Source File: GeodesicReconstructionScanningTest.java    From MorphoLibJ with GNU Lesser General Public License v3.0 4 votes vote down vote up
/**
 * Test method for {@link ijt.filter.morphology.GeodesicReconstruction#reconstructByErosion()}.
 */
@Test
public void testReconstructByErosion_FloatC4() {
	float BG = -42;
	float FG = 2500;
	float[][] data = new float[][]{
			{BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG},   
			{BG, FG, FG, BG, FG, FG, BG, FG, FG, FG, FG, FG, FG, FG, FG, BG},
			{BG, FG, FG, BG, FG, FG, BG, FG, FG, FG, FG, FG, FG, FG, FG, BG},
			{BG, FG, FG, BG, FG, FG, BG, FG, FG, BG, BG, BG, BG, FG, FG, BG},
			{BG, FG, FG, BG, FG, FG, BG, FG, FG, BG, FG, FG, BG, FG, FG, BG},
			{BG, FG, FG, BG, FG, FG, BG, FG, FG, BG, FG, FG, BG, FG, FG, BG},
			{BG, FG, FG, BG, BG, BG, BG, FG, FG, BG, FG, FG, BG, FG, FG, BG},
			{BG, FG, FG, FG, FG, FG, FG, FG, FG, BG, FG, FG, BG, FG, FG, BG},
			{BG, FG, FG, FG, FG, FG, FG, FG, FG, BG, FG, FG, BG, FG, FG, BG},
			{BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG},
	};
	int height = data.length;
	int width = data[0].length;
	ImageProcessor mask = new FloatProcessor(width, height);
	for (int y = 0; y < height; y++) {
		for (int x = 0; x < width; x++) {
			if (data[y][x] == FG)
				mask.setf(x, y, BG);
			else
				mask.setf(x, y, FG);
		}
	}
	
	ImageProcessor marker = new FloatProcessor(width, height);
	marker.setColor(FG);
	marker.fill();
	marker.setf(2, 3, BG);
	
	GeodesicReconstructionScanning algo = new GeodesicReconstructionScanning(
			GeodesicReconstructionType.BY_EROSION, 4);
	ImageProcessor result = algo.applyTo(marker, mask);
	
	assertEquals(16, result.getWidth());
	assertEquals(10, result.getHeight());
	assertEquals(BG, result.getf(2, 8), .01);
	assertEquals(BG, result.getf(8, 8), .01);
	assertEquals(BG, result.getf(8, 5), .01);
	assertEquals(BG, result.getf(14, 8), .01);
	assertEquals(FG, result.getf(15, 9), .01);
	assertEquals(FG, result.getf(0, 0), .01);
	assertEquals(FG, result.getf(5, 3), .01);
	assertEquals(FG, result.getf(11, 5), .01);
}
 
Example 14
Source File: GeodesicReconstructionScanningTest.java    From MorphoLibJ with GNU Lesser General Public License v3.0 4 votes vote down vote up
/**
 * Test method for {@link ijt.filter.morphology.GeodesicReconstruction#reconstructByErosion()}.
 */
@Test
public void testReconstructByErosion_C8() {
	int BG = 0;
	int FG = 255;
	int[][] data = new int[][]{
			{BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG},   
			{BG, FG, FG, BG, FG, FG, BG, BG, BG, FG, FG, FG, FG, BG, BG, BG},
			{BG, FG, FG, BG, FG, FG, BG, BG, BG, FG, FG, FG, FG, BG, BG, BG},
			{BG, FG, FG, BG, FG, FG, BG, FG, FG, BG, BG, BG, BG, FG, FG, BG},
			{BG, FG, FG, BG, FG, FG, BG, FG, FG, BG, FG, FG, BG, FG, FG, BG},
			{BG, FG, FG, BG, FG, FG, BG, FG, FG, BG, FG, FG, BG, FG, FG, BG},
			{BG, FG, FG, BG, BG, BG, BG, FG, FG, BG, FG, FG, BG, FG, FG, BG},
			{BG, BG, BG, FG, FG, FG, FG, BG, BG, BG, FG, FG, BG, FG, FG, BG},
			{BG, BG, BG, FG, FG, FG, FG, BG, BG, BG, FG, FG, BG, FG, FG, BG},
			{BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG},
	};
	int height = data.length;
	int width = data[0].length;
	ImageProcessor mask = new ByteProcessor(width, height);
	for (int y = 0; y < height; y++) {
		for (int x = 0; x < width; x++) {
			mask.set(x, y, data[y][x]);
		}
	}
	mask.invert();
	
	ImageProcessor marker = new ByteProcessor(width, height);
	marker.setColor(255);
	marker.fill();
	marker.set(2, 3, 0);
	
	GeodesicReconstructionScanning algo = new GeodesicReconstructionScanning(
			GeodesicReconstructionType.BY_EROSION, 8);
	ImageProcessor result = algo.applyTo(marker, mask);
	
	assertEquals(16, result.getWidth());
	assertEquals(10, result.getHeight());
	assertEquals(0, result.get(2, 6));
	assertEquals(0, result.get(4, 8));
	assertEquals(0, result.get(8, 5));
	assertEquals(0, result.get(14, 8));
	assertEquals(255, result.get(15, 9));
	assertEquals(255, result.get(0, 0));
	assertEquals(255, result.get(5, 3));
	assertEquals(255, result.get(11, 5));
}
 
Example 15
Source File: GeodesicReconstructionScanningTest.java    From MorphoLibJ with GNU Lesser General Public License v3.0 4 votes vote down vote up
/**
	 * Test method for {@link ijt.filter.morphology.GeodesicReconstruction#reconstructByErosion()}.
	 */
	@Test
	public void testReconstructByErosion_C4() {
		int BG = 0;
		int FG = 255;
		int[][] data = new int[][]{
				{BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG},   
				{BG, FG, FG, BG, FG, FG, BG, FG, FG, FG, FG, FG, FG, FG, FG, BG},
				{BG, FG, FG, BG, FG, FG, BG, FG, FG, FG, FG, FG, FG, FG, FG, BG},
				{BG, FG, FG, BG, FG, FG, BG, FG, FG, BG, BG, BG, BG, FG, FG, BG},
				{BG, FG, FG, BG, FG, FG, BG, FG, FG, BG, FG, FG, BG, FG, FG, BG},
				{BG, FG, FG, BG, FG, FG, BG, FG, FG, BG, FG, FG, BG, FG, FG, BG},
				{BG, FG, FG, BG, BG, BG, BG, FG, FG, BG, FG, FG, BG, FG, FG, BG},
				{BG, FG, FG, FG, FG, FG, FG, FG, FG, BG, FG, FG, BG, FG, FG, BG},
				{BG, FG, FG, FG, FG, FG, FG, FG, FG, BG, FG, FG, BG, FG, FG, BG},
				{BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG},
		};
		int height = data.length;
		int width = data[0].length;
		ImageProcessor mask = new ByteProcessor(width, height);
		for (int y = 0; y < height; y++) {
			for (int x = 0; x < width; x++) {
				mask.set(x, y, data[y][x]);
			}
		}
		mask.invert();
		
		ImageProcessor marker = new ByteProcessor(width, height);
		marker.setColor(255);
		marker.fill();
		marker.set(2, 3, 0);
		
//		System.out.println("Marker Image:");
//		printImage(marker);
//		System.out.println("Mask Image:");
//		printImage(mask);

		GeodesicReconstructionScanning algo = new GeodesicReconstructionScanning(
				GeodesicReconstructionType.BY_EROSION, 4);
		ImageProcessor result = algo.applyTo(marker, mask);
//		System.out.println("Result Image:");
//		printImage(result);
		
		assertEquals(16, result.getWidth());
		assertEquals(10, result.getHeight());
		assertEquals(0, result.get(2, 8));
		assertEquals(0, result.get(8, 8));
		assertEquals(0, result.get(8, 5));
		assertEquals(0, result.get(14, 8));
		assertEquals(255, result.get(15, 9));
		assertEquals(255, result.get(0, 0));
		assertEquals(255, result.get(5, 3));
		assertEquals(255, result.get(11, 5));
	}
 
Example 16
Source File: ReconstructionTest.java    From MorphoLibJ with GNU Lesser General Public License v3.0 4 votes vote down vote up
/**
 * Test method for {@link ijt.filter.morphology.Reconstruction#reconstructByErosion()}.
 */
@Test
public void testReconstructByErosion_C8() {
	int BG = 0;
	int FG = 255;
	int[][] data = new int[][]{
			{BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG},   
			{BG, FG, FG, BG, FG, FG, BG, BG, BG, FG, FG, FG, FG, BG, BG, BG},
			{BG, FG, FG, BG, FG, FG, BG, BG, BG, FG, FG, FG, FG, BG, BG, BG},
			{BG, FG, FG, BG, FG, FG, BG, FG, FG, BG, BG, BG, BG, FG, FG, BG},
			{BG, FG, FG, BG, FG, FG, BG, FG, FG, BG, FG, FG, BG, FG, FG, BG},
			{BG, FG, FG, BG, FG, FG, BG, FG, FG, BG, FG, FG, BG, FG, FG, BG},
			{BG, FG, FG, BG, BG, BG, BG, FG, FG, BG, FG, FG, BG, FG, FG, BG},
			{BG, BG, BG, FG, FG, FG, FG, BG, BG, BG, FG, FG, BG, FG, FG, BG},
			{BG, BG, BG, FG, FG, FG, FG, BG, BG, BG, FG, FG, BG, FG, FG, BG},
			{BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG},
	};
	int height = data.length;
	int width = data[0].length;
	ImageProcessor mask = new ByteProcessor(width, height);
	for (int y = 0; y < height; y++) {
		for (int x = 0; x < width; x++) {
			mask.set(x, y, data[y][x]);
		}
	}
	mask.invert();
	
	ImageProcessor marker = new ByteProcessor(width, height);
	marker.setColor(255);
	marker.fill();
	marker.set(2, 3, 0);
	
	ImageProcessor result = Reconstruction.reconstructByErosion(marker, mask, 8);
	
	assertEquals(16, result.getWidth());
	assertEquals(10, result.getHeight());
	assertEquals(0, result.get(2, 6));
	assertEquals(0, result.get(4, 8));
	assertEquals(0, result.get(8, 5));
	assertEquals(0, result.get(14, 8));
}
 
Example 17
Source File: ReconstructionTest.java    From MorphoLibJ with GNU Lesser General Public License v3.0 4 votes vote down vote up
/**
 * Test method for {@link ijt.filter.morphology.Reconstruction#reconstructByErosion()}.
 */
@Test
public void testReconstructByErosion() {
	int BG = 0;
	int FG = 255;
	int[][] data = new int[][]{
			{BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG},   
			{BG, FG, FG, BG, FG, FG, BG, FG, FG, FG, FG, FG, FG, FG, FG, BG},
			{BG, FG, FG, BG, FG, FG, BG, FG, FG, FG, FG, FG, FG, FG, FG, BG},
			{BG, FG, FG, BG, FG, FG, BG, FG, FG, BG, BG, BG, BG, FG, FG, BG},
			{BG, FG, FG, BG, FG, FG, BG, FG, FG, BG, FG, FG, BG, FG, FG, BG},
			{BG, FG, FG, BG, FG, FG, BG, FG, FG, BG, FG, FG, BG, FG, FG, BG},
			{BG, FG, FG, BG, BG, BG, BG, FG, FG, BG, FG, FG, BG, FG, FG, BG},
			{BG, FG, FG, FG, FG, FG, FG, FG, FG, BG, FG, FG, BG, FG, FG, BG},
			{BG, FG, FG, FG, FG, FG, FG, FG, FG, BG, FG, FG, BG, FG, FG, BG},
			{BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG},
	};
	int height = data.length;
	int width = data[0].length;
	ImageProcessor mask = new ByteProcessor(width, height);
	for (int y = 0; y < height; y++) {
		for (int x = 0; x < width; x++) {
			mask.set(x, y, data[y][x]);
		}
	}
	mask.invert();
	
	ImageProcessor marker = new ByteProcessor(width, height);
	marker.setColor(255);
	marker.fill();
	marker.set(2, 3, 0);
	
	ImageProcessor result = Reconstruction.reconstructByErosion(marker, mask);
	
	assertEquals(16, result.getWidth());
	assertEquals(10, result.getHeight());
	assertEquals(0, result.get(2, 8));
	assertEquals(0, result.get(8, 8));
	assertEquals(0, result.get(8, 5));
	assertEquals(0, result.get(14, 8));
}
 
Example 18
Source File: Lines_.java    From ij-ridgedetection with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Make binary.
 */
public void makeBinary() {
	ImagePlus binary = IJ.createHyperStack(imp.getTitle() + " Detected segments", imp.getWidth(), imp.getHeight(),
			imp.getNChannels(), imp.getStackSize() / imp.getNChannels(), 1, 8);
	binary.copyScale(imp);

	ImageProcessor binaryProcessor = binary.getProcessor();
	binaryProcessor.invertLut();
	if (imp.getCompositeMode() > 0) {
		((CompositeImage) binary).setLuts(imp.getLuts());
	}

	ImageStack is = binary.getImageStack();
	ImageProcessor ip = binary.getProcessor();

	for (Lines contours : result) {
		for (Line c : contours) {

			float[] x = c.getXCoordinates();
			float[] y = c.getYCoordinates();

			int[] x_poly_r = new int[x.length];
			int[] y_poly_r = new int[x.length];

			Polygon LineSurface = new Polygon();

			ip = is.getProcessor(c.getFrame());

			ip.setLineWidth(1);
			ip.setColor(255);

			for (int j = 0; j < x.length; j++) {
				// this draws the identified line
				if (j > 0) {
					ip.drawLine((int) Math.round(x[j - 1]), (int) Math.round(y[j - 1]), (int) Math.round(x[j]),
							(int) Math.round(y[j]));
				}

				// If Estimate Width is ticked, we also draw the line surface in the binary
				if (doEstimateWidth) {

					double nx = Math.sin(c.angle[j]);
					double ny = Math.cos(c.angle[j]);

					// left point coordinates are directly added to the polygon. right coordinates
					// are saved to be added at the end of the coordinates list
					LineSurface.addPoint((int) Math.round(x[j] - c.width_l[j] * nx),
							(int) Math.round(y[j] - c.width_l[j] * ny));

					x_poly_r[j] = (int) Math.round(x[j] + c.width_r[j] * nx);
					y_poly_r[j] = (int) Math.round(y[j] + c.width_r[j] * ny);
				}
			}

			if (doEstimateWidth) {
				// loop to add the right coordinates to the end of the polygon, reversed
				for (int j = 0; j < x.length; j++) {
					if (j < x.length) {
						LineSurface.addPoint(x_poly_r[x.length - 1 - j], y_poly_r[x.length - 1 - j]);
					}
				}
				// draw surfaces.
				ip.fillPolygon(LineSurface);
			}
		}
	}
	binary.show();
	binary.updateAndDraw();
}
 
Example 19
Source File: ThresholderOrbit.java    From orbit-image-analysis with GNU General Public License v3.0 4 votes vote down vote up
public void applyThreshold(ImagePlus imp) {
	imp.deleteRoi();
	ImageProcessor ip = imp.getProcessor();
	ip.resetBinaryThreshold();
	int type = imp.getType();
	if (type==ImagePlus.GRAY16 || type==ImagePlus.GRAY32) {
		applyShortOrFloatThreshold(imp);
		return;
	}
	if (!imp.lock()) return;
	double saveMinThreshold = ip.getMinThreshold();
	double saveMaxThreshold = ip.getMaxThreshold();
	autoThreshold = saveMinThreshold==ImageProcessor.NO_THRESHOLD;

	boolean useBlackAndWhite = true;
	boolean noArgMacro =IJ.macroRunning() && Macro.getOptions()==null;
	if (skipDialog)
		fill1 = fill2 = useBlackAndWhite = true;
	else if (!(autoThreshold||noArgMacro)) {
		GenericDialog gd = new GenericDialog("Make Binary");
		gd.addCheckbox("Thresholded pixels to foreground color", fill1);
		gd.addCheckbox("Remaining pixels to background color", fill2);
		gd.addMessage("");
		gd.addCheckbox("Black foreground, white background", useBW);
		gd.showDialog();
		if (gd.wasCanceled())
		{imp.unlock(); return;}
		fill1 = gd.getNextBoolean();
		fill2 = gd.getNextBoolean();
		useBW = useBlackAndWhite = gd.getNextBoolean();
	} else {
		fill1 = fill2 = true;
		convertToMask = true;
	}

	if (type!=ImagePlus.GRAY8)
		convertToByte(imp);
	ip = imp.getProcessor();

	if (autoThreshold)
		autoThreshold(ip);
	else {
		if (Recorder.record && !Recorder.scriptMode() && (!IJ.isMacro()||Recorder.recordInMacros))
			Recorder.record("setThreshold", (int)saveMinThreshold, (int)saveMaxThreshold);
		minThreshold = saveMinThreshold;
		maxThreshold = saveMaxThreshold;
	}

	if (convertToMask && ip.isColorLut())
		ip.setColorModel(ip.getDefaultColorModel());
	int fcolor, bcolor;
	ip.resetThreshold();
	int savePixel = ip.getPixel(0,0);
	if (useBlackAndWhite)
		ip.setColor(Color.black);
	else
		ip.setColor(Toolbar.getForegroundColor());
	ip.drawPixel(0,0);
	fcolor = ip.getPixel(0,0);
	if (useBlackAndWhite)
		ip.setColor(Color.white);
	else
		ip.setColor(Toolbar.getBackgroundColor());
	ip.drawPixel(0,0);
	bcolor = ip.getPixel(0,0);
	ip.setColor(Toolbar.getForegroundColor());
	ip.putPixel(0,0,savePixel);

	int[] lut = new int[256];
	for (int i=0; i<256; i++) {
		if (i>=minThreshold && i<=maxThreshold)
			lut[i] = fill1?fcolor:(byte)i;
		else {
			lut[i] = fill2?bcolor:(byte)i;
		}
	}
	if (imp.getStackSize()>1)
		new StackProcessor(imp.getStack(), ip).applyTable(lut);
	else
		ip.applyTable(lut);
	if (convertToMask) {
		if (!imp.isInvertedLut()) {
			setInvertedLut(imp);
			fcolor = 255 - fcolor;
			bcolor = 255 - bcolor;
		}
		if (Prefs.blackBackground)
			ip.invertLut();
	}
	if (fill1 && fill2 && ((fcolor==0&&bcolor==255)||(fcolor==255&&bcolor==0)))
		imp.getProcessor().setThreshold(fcolor, fcolor, ImageProcessor.NO_LUT_UPDATE);
	imp.updateAndRepaintWindow();
	imp.unlock();
}