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

The following examples show how to use ij.process.ImageProcessor#set() . 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: ShiftedCross3x3Strel_LeftTest.java    From MorphoLibJ with GNU Lesser General Public License v3.0 6 votes vote down vote up
@Test
public void testErosion_Square4x4() {
	ImageProcessor image = createImage_Square4x4();
	Strel strel = ShiftedCross3x3Strel.LEFT;
	
	ImageProcessor expected = image.createProcessor(10, 10);
	for (int y = 4; y < 6; y++) {
		for (int x = 5; x < 7; x++) {
			expected.set(x, y, 255);
		}
	}

	ImageProcessor result = strel.erosion(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 2
Source File: GeodesicDistanceTransformShortTest.java    From MorphoLibJ with GNU Lesser General Public License v3.0 6 votes vote down vote up
@Test
public void testGeodesicDistanceMap_Borgefors()
{
	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);

	short[] weights = new short[] { 3, 4 };
	GeodesicDistanceTransform algo = new GeodesicDistanceTransformShort(
			weights, true);
	ImageProcessor map = algo.geodesicDistanceMap(marker, mask);

	assertEquals(258, map.get(190, 210));
}
 
Example 3
Source File: ImageCalculator.java    From MorphoLibJ with GNU Lesser General Public License v3.0 6 votes vote down vote up
public static final ImageProcessor not(ImageProcessor image)
{
    int width = image.getWidth();
    int height = image.getHeight();
    
    ImageProcessor result = image.duplicate();
    
    for (int y = 0; y < height; y++)
    {
        for (int x = 0; x < width; x++)
        {
            result.set(x, y, ~image.get(x, y));
        }
    }
    return result;
}
 
Example 4
Source File: DirectionalFilteringPlugin.java    From MorphoLibJ with GNU Lesser General Public License v3.0 6 votes vote down vote up
@Override
public void run(ImageProcessor image)
{
	IJ.log("Run directional filter");
	
	DirectionalFilter filter = new DirectionalFilter(this.type, this.op, this.lineLength, this.nDirections);
	DefaultAlgoListener.monitor(filter);
	
	this.result = filter.process(image);

	if (previewing)
	{
		// Fill up the values of original image with values of the result
		for (int i = 0; i < image.getPixelCount(); i++)
		{
   			image.set(i, result.get(i));
   		}
       }
}
 
Example 5
Source File: IntrinsicVolumes2DTest.java    From MorphoLibJ with GNU Lesser General Public License v3.0 6 votes vote down vote up
/**
 * Test method for {@link inra.ijpb.measure.IntrinsicVolumes2D#eulerNumber(ij.process.ImageProcessor, int)}.
 */
@Test
public final void testEulerNumber_crossTouchingBordersC8()
{
	// create a binary image containing a square
	ImageProcessor image = new ByteProcessor(6, 6);
	for (int i = 0; i < 6; i++)
	{
		image.set(i, 2, 255);
		image.set(i, 3, 255);
		image.set(2, i, 255);
		image.set(3, i, 255);
	}
	
	int euler = IntrinsicVolumes2D.eulerNumber(image, 8);
	assertEquals(1, euler);
}
 
Example 6
Source File: GeodesicDiameterShortTest.java    From MorphoLibJ with GNU Lesser General Public License v3.0 6 votes vote down vote up
/**
 * Test method for {@link inra.ijpb.binary.geodesic.GeodesicDiameterShort#longestGeodesicPaths(ij.process.ImageProcessor)}.
 */
@Test
public void testLongestGeodesicPaths_Rect()
{
	ImageProcessor image = new ByteProcessor(10, 3);
	for (int x = 1; x < 8; x++)
	{
		image.set(x, 1, 255);
	}

	GeodesicDiameterShort algo = new GeodesicDiameterShort(ChamferWeights.BORGEFORS);
	algo.analyzeImage(image);
	Map<Integer, List<Point>> pathMap = algo.longestGeodesicPaths();

	assertEquals(1, pathMap.size());
	List<Point> path1 = pathMap.get(255);
	assertEquals(7, path1.size());
}
 
Example 7
Source File: GeodesicDistanceTransformFloat5x5Test.java    From MorphoLibJ with GNU Lesser General Public License v3.0 6 votes vote down vote up
@Test
public void testGeodesicDistanceMap_Borgefors()
{
	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);

	float[] weights = new float[] { 5, 7, 11 };
	GeodesicDistanceTransform algo = new GeodesicDistanceTransformFloat5x5(
			weights, true);
	ImageProcessor map = algo.geodesicDistanceMap(marker, mask);

	assertEquals(250.8, map.getf(190, 210), .01);
}
 
Example 8
Source File: IntrinsicVolumes2DTest.java    From MorphoLibJ with GNU Lesser General Public License v3.0 6 votes vote down vote up
/**
 * Test method for {@link inra.ijpb.measure.IntrinsicVolumes2D#eulerNumber(ij.process.ImageProcessor, int)}.
 */
@Test
public final void testEulerNumber_singleSquareC8()
{
	// create a binary image containing a square
	ImageProcessor image = new ByteProcessor(8, 8);
	for (int i = 2; i < 6; i++)
	{
		for (int j = 2; j < 6; j++)
		{
			image.set(i, j, 255);
		}
	}
	
	int euler = IntrinsicVolumes2D.eulerNumber(image, 8);
	assertEquals(1, euler);
}
 
Example 9
Source File: BinaryImagesTest.java    From MorphoLibJ with GNU Lesser General Public License v3.0 6 votes vote down vote up
/**
 * Checks that the maximum number of labels is greater than 2^8 when labels
 * are coded with byte.
 */
@Test
public final void testComputeLabels_Byte() 
{
	ImageProcessor image = new ByteProcessor(300, 300);
	for (int y = 0; y < 15; y++)
	{
		for (int x = 0; x < 15; x++)
		{
			image.set(20 * x + 1, 20 * y + 1, 255);
		}
	}
	
	ImageProcessor labels = BinaryImages.componentsLabeling(image, 4, 8);
	
	assertEquals(15*15, labels.get(281, 281), .1);
}
 
Example 10
Source File: DiamondStrelTest.java    From MorphoLibJ with GNU Lesser General Public License v3.0 6 votes vote down vote up
@Test
public void testErosion_Square10x10() {
	ImageProcessor image = createImage_Square10x10();
	Strel strel = new DiamondStrel(5);
	
	ImageProcessor expected = image.createProcessor(30, 30);
	for (int y = 12; y < 18; y++) {
		for (int x = 12; x < 18; x++) {
			expected.set(x, y, 255);
		}
	}

	ImageProcessor result = strel.erosion(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 11
Source File: GeometricMeasures2DTest.java    From MorphoLibJ with GNU Lesser General Public License v3.0 6 votes vote down vote up
/**
 * Test method for {@link ijt.measure.geometric.GeometricMeasures2D#maxInscribedCircle(ij.process.ImageProcessor)}.
 */
@Test
@Deprecated
public final void testMaxInscribedCircle() 
{
	ImageProcessor image = new ByteProcessor(14, 9);
	image.set(1, 1, 1);
	fillRect(image, 3, 4, 1, 2, 2);		// radius 2
	fillRect(image, 1, 1+3, 4, 4+3, 3); // radius 4
	fillRect(image, 6, 6+6, 1, 1+6, 4); // radius 7
	
	ResultsTable table = GeometricMeasures2D.maximumInscribedCircle(image);
	
	assertEquals(1, table.getValue("InscrCircle.Radius", 0), .1);
	assertEquals(1, table.getValue("InscrCircle.Radius", 1), .1);
	assertEquals(2, table.getValue("InscrCircle.Radius", 2), .1);
	assertEquals(4, table.getValue("InscrCircle.Radius", 3), .1);
}
 
Example 12
Source File: MinimaAndMaxima.java    From MorphoLibJ with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * Computes the regional minima in grayscale image <code>image</code>, 
 * using the specified connectivity, and a slower algorithm (used for testing).
 * 
 * @param image
 *            the image to process
 * @param conn
 *            the connectivity for minima, that should be either 4 or 8
 * @return the regional minima of input image
 */
public final static ImageProcessor regionalMinimaByReconstruction(ImageProcessor image,
		int conn)
{
	ImageProcessor marker = image.duplicate();
	marker.add(1);
	
	GeodesicReconstructionAlgo algo = new GeodesicReconstructionHybrid(
			GeodesicReconstructionType.BY_EROSION, conn);
	ImageProcessor rec = algo.applyTo(marker, 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++)
		{
			if (marker.get(x, y) > rec.get(x, y)) 
				result.set(x,  y, 0);
			else
				result.set(x,  y, 255);
		}
	}
	
	return result;
}
 
Example 13
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 14
Source File: MorphologicalFilterPlugin.java    From MorphoLibJ with GNU Lesser General Public License v3.0 5 votes vote down vote up
private void resetPreview()
{
	ImageProcessor image = this.imagePlus.getProcessor();
	if (image instanceof FloatProcessor)
	{
		for (int i = 0; i < image.getPixelCount(); i++)
			image.setf(i, this.baseImage.getf(i));
	}
	else
	{
		for (int i = 0; i < image.getPixelCount(); i++)
			image.set(i, this.baseImage.get(i));
	}
	imagePlus.updateAndDraw();
}
 
Example 15
Source File: DiamondStrelTest.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 = new DiamondStrel(5);
	
	ImageProcessor expected = image.createProcessor(10, 10);
	for (int x = 3; x < 7; x++) {
		expected.set(x, 1, 255);
		expected.set(x, 8, 255);
	}
	for (int x = 2; x < 8; x++) {
		expected.set(x, 2, 255);
		expected.set(x, 7, 255);
	}
	for (int y = 3; y < 7; y++) {
		for (int x = 1; x < 9; 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 16
Source File: IntrinsicVolumes2DTest.java    From MorphoLibJ with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * Test method for {@link inra.ijpb.measure.IntrinsicVolumes2D#perimeters(ij.process.ImageProcessor, int[], Calibration, int)}.
 */
@Test
public final void testPerimeters_disks_D2()
{
	// define several disks of increasing radius
	double xc1 = 25.2, yc1 = 25.3, r1 = 11.0;
	double xc2 = 75.2, yc2 = 25.3, r2 = 16.0;
	double xc3 = 21.2, yc3 = 75.3, r3 = 21.0;
	// the last one touching borders
	double xc4 = 73.2, yc4 = 73.3, r4 = 26.0;

	// create a binary image containing a square
	ImageProcessor image = new ByteProcessor(100, 100);
	for (int y = 0; y < 100; y++)
	{
		for (int x = 0; x < 100; x++)
		{
			if(Math.hypot(x-xc1, y-yc1) < r1) image.set(x, y, 1);
			if(Math.hypot(x-xc2, y-yc2) < r2) image.set(x, y, 2);
			if(Math.hypot(x-xc3, y-yc3) < r3) image.set(x, y, 3);
			if(Math.hypot(x-xc4, y-yc4) < r4) image.set(x, y, 4);
		}
	}
	
	// compute perimeter with default (1,1) calibration
	Calibration calib = new Calibration();
	int[] labels = new int[] {1, 2, 3, 4};
	double[] perims = IntrinsicVolumes2D.perimeters(image, labels, calib, 2);
	
	// check to expected values with a tolerance of 5 percents
	double exp1 = 2 * Math.PI * r1;
	assertEquals(exp1, perims[0], exp1 * 0.05);
	double exp2 = 2 * Math.PI * r2;
	assertEquals(exp2, perims[1], exp2 * 0.05);
	double exp3 = 2 * Math.PI * r3;
	assertEquals(exp3, perims[2], exp3 * 0.05);
	double exp4 = 2 * Math.PI * r4;
	assertEquals(exp4, perims[3], exp4 * 0.05);
}
 
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_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 18
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;
}
 
Example 19
Source File: Distortion_Correction.java    From TrakEM2 with GNU General Public License v3.0 4 votes vote down vote up
static	double getXcorrBlackOut(ImageProcessor ip1, ImageProcessor ip2){

		ip1 = ip1.convertToFloat();
		ip2 = ip2.convertToFloat();

		//If this is not done, the black area from the transformed image influences xcorr result
		//better alternative would be to use mask images and only calculate xcorr of
		//the region present in both images.
		for (int i=0; i<ip1.getWidth(); i++){
			for (int j=0; j<ip1.getHeight(); j++){
				if (ip1.get(i,j) == 0)
					ip2.set(i,j,0);
				if (ip2.get(i,j) == 0)
					ip1.set(i,j,0);
			}
		}

		//		FloatProcessor ip1f = (FloatProcessor)ip1.convertToFloat();
		//		FloatProcessor ip2f = (FloatProcessor)ip2.convertToFloat();

		final float[] data1 = ( float[] )ip1.getPixels();
		final float[] data2 = ( float[] )ip2.getPixels();

		final double[] data1b = new double[data1.length];
		final double[] data2b = new double[data2.length];

		int count = 0;
		double mean1 = 0.0, mean2 = 0.0;

		for (int i=0; i < data1.length; i++){
			//if ((data1[i] == 0) || (data2[i] == 0))
			//continue;
			data1b[i] = data1[i];
			data2b[i] = data2[i];
			mean1 += data1b[i];
			mean2 += data2b[i];
			count++;
		}

		mean1 /= (double) count;
		mean2 /= (double) count;

		double L2_1 = 0.0, L2_2 = 0.0;
		for (int i=0; i < count; i++){
			L2_1 += (data1b[i] - mean1) * (data1b[i] - mean1);
			L2_2 += (data2b[i] - mean2) * (data2b[i] - mean2);
		}

		L2_1 = Math.sqrt(L2_1);
		L2_2 = Math.sqrt(L2_2);

		double xcorr = 0.0;
		for (int i=0; i < count; i++){
			xcorr += ((data1b[i]-mean1) / L2_1) * ((data2b[i]-mean2) / L2_2);
		}

		//System.out.println("XcorrVal: " + xcorr);
		return xcorr;
	}
 
Example 20
Source File: FloodFill.java    From MorphoLibJ with GNU Lesser General Public License v3.0 3 votes vote down vote up
/**
 * In the input image, replaces all pixels in row <code>y</code> located
 * between <code>x1</code> and <code>x2</code> (inclusive) by the given
 * value.
 * 
 * @param ip
 *            the input image to modify
 * @param y
 *            the index of the row to modify
 * @param x1
 *            the column index of the first pixel to modify
 * @param x2
 *            the column index of the first pixel to modify
 * @param value
 *            the new value of the pixels
 */
private final static void fillLine(ImageProcessor ip, int y, int x1,
		int x2, int value)
{
	if (x1 > x2)
	{
		int t = x1;
		x1 = x2;
		x2 = t;
	}
	
	for (int x = x1; x <= x2; x++)
		ip.set(x, y, value);
}