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

The following examples show how to use ij.process.ImageProcessor#fill() . 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: GeodesicDistanceMapPluginTest.java    From MorphoLibJ with GNU Lesser General Public License v3.0 6 votes vote down vote up
/**
 * Test method for {@link inra.ijpb.plugins.GeodesicDistanceMapPlugin#process(ij.ImagePlus, ij.ImagePlus, java.lang.String, short[], boolean)}.
 */
@Test
public void testProcess_Short()
{
	ImagePlus maskPlus = IJ.openImage(getClass().getResource("/files/circles.tif").getFile());
	ImageProcessor mask = maskPlus.getProcessor();
	ImageProcessor marker = mask.duplicate();
	marker.fill();
	marker.set(30, 30, 255);
	ImagePlus markerPlus = new ImagePlus("marker", marker);
	
	GeodesicDistanceMapPlugin plugin = new GeodesicDistanceMapPlugin();
	short[] weights = new short[]{3, 4};
	ImagePlus mapPlus = plugin.process(markerPlus, maskPlus, "map", weights, true);
	ImageProcessor map = mapPlus.getProcessor();
			
	assertEquals(259, map.get(190, 213));
}
 
Example 2
Source File: GeodesicDistanceMapPluginTest.java    From MorphoLibJ with GNU Lesser General Public License v3.0 6 votes vote down vote up
/**
 * Test method for {@link inra.ijpb.plugins.GeodesicDistanceMapPlugin#process(ij.ImagePlus, ij.ImagePlus, java.lang.String, float[], boolean)}.
 */
@Test
public void testProcess_Float()
{
	ImagePlus maskPlus = IJ.openImage(getClass().getResource("/files/circles.tif").getFile());
	ImageProcessor mask = maskPlus.getProcessor();
	ImageProcessor marker = mask.duplicate();
	marker.fill();
	marker.set(30, 30, 255);
	ImagePlus markerPlus = new ImagePlus("marker", marker);
	
	GeodesicDistanceMapPlugin plugin = new GeodesicDistanceMapPlugin();
	float[] weights = new float[]{3, 4};
	ImagePlus mapPlus = plugin.process(markerPlus, maskPlus, "map", weights, true);
	ImageProcessor map = mapPlus.getProcessor();

	assertEquals(259, map.getf(190, 213), .01);
}
 
Example 3
Source File: DiskStrelTest.java    From MorphoLibJ with GNU Lesser General Public License v3.0 6 votes vote down vote up
/**
 * Dilates a single pixel by a 3x3 disk, and check the shape of the result.
 * The result should be a 3x3 square (approximation of 3x3 disk)
 */
@Test
public void testDilate_SinglePixel_Radius() {
	Strel strel = DiskStrel.fromRadius(1);
	
	ImageProcessor image = new ByteProcessor(10, 10);
	image.setValue(0);
	image.fill();
	image.set(5, 5, 255);
	ImageProcessor result = strel.dilation(image);

	// Check all values inside square
	for (int y = 4; y < 7; y++)
		for (int x = 4; x < 7; x++)
			assertEquals(255, result.get(x, y));
}
 
Example 4
Source File: Utils.java    From TrakEM2 with GNU General Public License v3.0 6 votes vote down vote up
/** Paints an approximation of the pipe into the set of slices. */
static public void paint(final Pipe pipe, final Map<Layer,ImageProcessor> slices, final int value, final float scale) {
	final VectorString3D vs = pipe.asVectorString3D();
	vs.resample(1); // one pixel
	final double[] px = vs.getPoints(0);
	final double[] py = vs.getPoints(1);
	final double[] pz = vs.getPoints(2);
	final double[] pr = vs.getDependent(0);
	// For each point
	for (int i=0; i<px.length-1; i++) {
		final ImageProcessor ip = slices.get(pipe.getLayerSet().getNearestLayer(pz[i]));
		if (null == ip) continue;
		final OvalRoi ov = new OvalRoi((int)((px[i] - pr[i]) * scale),
				         (int)((py[i] - pr[i]) * scale),
					 (int)(pr[i]*2*scale), (int)(pr[i]*2*scale));
		ip.setRoi(ov);
		ip.setValue(value);
		ip.fill(ip.getMask());
	}
}
 
Example 5
Source File: FloodFillTest.java    From MorphoLibJ with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Test
	public final void testFloodFill_EmptySquaresC4() {
		int[] data = new int[]{
				10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10,
				10, 20, 20, 20, 10, 10, 10, 30, 30, 30, 10,
				10, 20, 20, 20, 10, 10, 10, 30, 30, 30, 10,
				10, 20, 20, 20, 10, 10, 10, 30, 30, 30, 10,
				10, 10, 10, 10, 40, 40, 40, 10, 10, 10, 10,
				10, 10, 10, 10, 40, 40, 40, 10, 10, 10, 10,
				10, 10, 10, 10, 40, 40, 40, 10, 10, 10, 10,
				10, 20, 20, 20, 10, 10, 10, 30, 30, 30, 10,
				10, 20, 20, 20, 10, 10, 10, 30, 30, 30, 10,
				10, 20, 20, 20, 10, 10, 10, 30, 30, 30, 10,
				10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10
		};
		ImageProcessor image = new ByteProcessor(11, 11);
		for (int i = 0; i < 11*11; i++) {
			image.set(i, data[i]);
		}

		// initialize result
		ImageProcessor result = new ByteProcessor(11, 11);
		result.setValue(255);
		result.fill();
		
		// compute flood fill result
		FloodFill.floodFill(image, 1, 0, result, 50, 4);
		
		assertEquals(50, result.get(0, 0));
		assertEquals(50, result.get(10, 0));
		assertEquals(50, result.get(0, 10));
		assertEquals(50, result.get(10, 10));
		
		assertEquals(50, result.get(5, 3));
		assertEquals(50, result.get(5, 7));
		assertEquals(50, result.get(3, 5));
		assertEquals(50, result.get(7, 5));
		
//		printImage(result);
	}
 
Example 6
Source File: OctagonStrelTest.java    From MorphoLibJ with GNU Lesser General Public License v3.0 5 votes vote down vote up
private ImageProcessor createImage_Square10x10 () {
	ImageProcessor image = new ByteProcessor(30, 30);
	image.setValue(0);
	image.fill();
	
	for (int y = 10; y < 20; y++) {
		for (int x = 10; x < 20; x++) {
			image.set(x, y, 255);
		}			
	}
	
	return image;
}
 
Example 7
Source File: ShiftedCross3x3Strel_RightTest.java    From MorphoLibJ with GNU Lesser General Public License v3.0 5 votes vote down vote up
private ImageProcessor createImage_Square4x4 () {
	ImageProcessor image = new ByteProcessor(10, 10);
	image.setValue(0);
	image.fill();
	
	for (int y = 3; y < 7; y++) {
		for (int x = 3; x < 7; x++) {
			image.set(x, y, 255);
		}			
	}
	
	return image;
}
 
Example 8
Source File: SquareStrelTest.java    From MorphoLibJ with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * Creates a 30-by-30 image with a 10-by-10 square in the middle.
 */
private ImageProcessor createImage_Square10x10 () {
	ImageProcessor image = new ByteProcessor(30, 30);
	image.setValue(0);
	image.fill();
	
	for (int y = 10; y < 20; y++) {
		for (int x = 10; x < 20; x++) {
			image.set(x, y, 255);
		}			
	}
	
	return image;
}
 
Example 9
Source File: LinearDiagUpStrelTest.java    From MorphoLibJ with GNU Lesser General Public License v3.0 5 votes vote down vote up
private ImageProcessor createImage_Square4x4 () {
	ImageProcessor image = new ByteProcessor(10, 10);
	image.setValue(0);
	image.fill();
	
	for (int y = 3; y < 7; y++) {
		for (int x = 3; x < 7; x++) {
			image.set(x, y, 255);
		}			
	}
	
	return image;
}
 
Example 10
Source File: DiskStrelTest.java    From MorphoLibJ with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * Dilates a single pixel by a disk with diameter 4. 
 * The result should be larger than dilation with diameter 3.
 */
@Test
public void testDilate_SinglePixel_EvenDiameter() {
	Strel disk3 = DiskStrel.fromDiameter(3);
	Strel disk4 = DiskStrel.fromDiameter(4);
	
	ImageProcessor image = new ByteProcessor(10, 10);
	image.setValue(0);
	image.fill();
	image.set(5, 5, 255);
	
	ImageProcessor result3 = disk3.dilation(image);
	ImageProcessor result4 = disk4.dilation(image);

	// Check result3 <= result4
	boolean different = false;
	for (int y = 0; y < 10; y++)
	{
		for (int x = 0; x < 10; x++)
		{
			int res3 = result3.get(x, y);
			int res4 = result4.get(x, y);
			assertTrue(res3 <= res4);
			
			if (res3 != res4)
			{
				different = true;
			}
		}
	}
	
	assertTrue(different);
}
 
Example 11
Source File: LinearDiagDownStrelTest.java    From MorphoLibJ with GNU Lesser General Public License v3.0 5 votes vote down vote up
private ImageProcessor createImage_Square10x10 () {
	ImageProcessor image = new ByteProcessor(30, 30);
	image.setValue(0);
	image.fill();
	
	for (int y = 10; y < 20; y++) {
		for (int x = 10; x < 20; x++) {
			image.set(x, y, 255);
		}			
	}
	
	return image;
}
 
Example 12
Source File: LinearDiagDownStrelTest.java    From MorphoLibJ with GNU Lesser General Public License v3.0 5 votes vote down vote up
private ImageProcessor createImage_Square4x4 () {
	ImageProcessor image = new ByteProcessor(10, 10);
	image.setValue(0);
	image.fill();
	
	for (int y = 3; y < 7; y++) {
		for (int x = 3; x < 7; x++) {
			image.set(x, y, 255);
		}			
	}
	
	return image;
}
 
Example 13
Source File: LinearDiagUpStrelTest.java    From MorphoLibJ with GNU Lesser General Public License v3.0 5 votes vote down vote up
private ImageProcessor createImage10x15_Square4x4 () {
	ImageProcessor image = new ByteProcessor(10, 15);
	image.setValue(0);
	image.fill();
	
	for (int y = 3; y < 7; y++) {
		for (int x = 3; x < 7; x++) {
			image.set(x, y, 255);
		}			
	}
	
	return image;
}
 
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_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 15
Source File: FloodFillTest.java    From MorphoLibJ with GNU Lesser General Public License v3.0 4 votes vote down vote up
@Test
	public final void testFloodFillC8Marker() {
		int[][] data = new int[][]{
				{10, 10, 10, 20, 20, 20, 10, 10, 10, 10, 20, 20, 10, 10, 10},
				{10, 10, 20, 20, 20, 20, 20, 20, 10, 20, 20, 20, 20, 10, 10},
				{10, 20, 10, 10, 10, 10, 20, 20, 10, 20, 10, 10, 20, 20, 10},
				{20, 20, 10, 20, 10, 10, 10, 20, 10, 20, 20, 10, 10, 20, 20},
				{20, 20, 10, 20, 10, 10, 10, 20, 10, 10, 10, 20, 10, 20, 20},
				{20, 20, 10, 10, 20, 20, 10, 20, 10, 10, 10, 20, 10, 20, 20},
				{10, 20, 10, 10, 10, 20, 10, 20, 20, 10, 10, 10, 10, 20, 10},
				{10, 20, 10, 20, 20, 20, 10, 20, 20, 20, 20, 20, 20, 20, 10},
				{10, 10, 20, 20, 10, 10, 10, 10, 10, 10, 10, 20, 20, 10, 10},
		};
		int height = data.length;
		int width = data[0].length;
		ImageProcessor image = new ByteProcessor(width, height);
		for (int y = 0; y < height; y++) {
			for (int x = 0; x < width; x++) {
				image.set(x, y, data[y][x]);
			}
		}
		
		// initialize empty result image fill with 255
		ImageProcessor result = new ByteProcessor(width, height);
		result.setValue(255);
		result.fill();
		
		// Apply 
		FloodFill.floodFill(image, 7, 4, result, 50, 8);
//		printImage(result);
		
		for (int y = 0; y < height; y++) {
			for (int x = 0; x < width; x++) {
				if (image.get(x, y) == 20)
					assertEquals(50, result.get(x, y));
				else
					assertEquals(255, result.get(x, y));
			}
		}
		
	}
 
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: 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 18
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 19
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 20
Source File: RegionalExtremaByFlooding.java    From MorphoLibJ with GNU Lesser General Public License v3.0 4 votes vote down vote up
/**
 * Computes regional extrema in current input image, using
 * flood-filling-like algorithm with 4 connectivity.
 * Computations are made with double values.
 */
private ImageProcessor regionalExtremaC4(ImageProcessor image) 
{
	// get image size
	int sizeX = image.getWidth();
	int sizeY = image.getHeight();

	// allocate memory for output, and fill with 255
	ImageProcessor result = new ByteProcessor(sizeX, sizeY);
	result.setValue(255);
	result.fill();
	
	// initialize local data depending on extrema type
	int sign = 1;
	if (this.extremaType == ExtremaType.MAXIMA) 
	{
		sign = -1;
	}
	
	// Iterate over image pixels
	for (int y = 0; y < sizeY; y++) 
	{
		for (int x = 0; x < sizeX; x++) 
		{
			// Check if current pixel was already processed
			if (result.getf(x, y) == 0)
				continue;
			
			// current value
			float currentValue = image.getf(x, y);
			
			// compute extrema value in 4-neighborhood (computes max value
			// if sign is -1)
			float value = currentValue * sign;
			if (x > 0) 
				value = min(value, image.getf(x-1, y) * sign); 
			if (y > 0) 
				value = min(value, image.getf(x, y-1) * sign); 
			if (x < sizeX - 1) 
				value = min(value, image.getf(x+1, y) * sign); 
			if (y < sizeY - 1) 
				value = min(value, image.getf(x, y+1) * sign);
			
			// if one of the neighbors of current pixel has a lower (resp.
			// greater) value, the the current pixel is not an extremum.
			// Consequently, the current pixel, and all its connected 
			// neighbors with same value are set to 0 in the output image. 
			if (value < currentValue * sign)
			{
				FloodFill.floodFillFloat(image, x, y, result, 0.f, 4);
			}
		}
	}
	
	return result;
}