Java Code Examples for ij.process.FloatProcessor#setf()

The following examples show how to use ij.process.FloatProcessor#setf() . 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: 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 2
Source File: DistanceTransform5x5Float.java    From MorphoLibJ with GNU Lesser General Public License v3.0 6 votes vote down vote up
private void normalizeResult(FloatProcessor distMap, ImageProcessor labelImage)
{
	this.fireStatusChanged(new AlgoEvent(this, "Normalization"));
	
	// size of image
	int sizeX = labelImage.getWidth();
	int sizeY = labelImage.getHeight();

	// normalization weight
	float w0 = weights[0];
	
	for (int y = 0; y < sizeY; y++)
	{
		for (int x = 0; x < sizeX; x++)
		{
			if ((int) labelImage.getf(x, y) > 0)
			{
				distMap.setf(x, y, distMap.getf(x, y) / w0);
			}
		}
	}
}
 
Example 3
Source File: DistanceTransform3x3Float.java    From MorphoLibJ with GNU Lesser General Public License v3.0 6 votes vote down vote up
private FloatProcessor 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
	FloatProcessor distMap = new FloatProcessor(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.setf(x, y, label == 0 ? 0 : Float.POSITIVE_INFINITY);
		}
	}
	
	return distMap;
}
 
Example 4
Source File: ImageMath.java    From thunderstorm with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Add a scalar value to an image.
 * 
 * @param val an input value will be added to the input image ({@code fp})
 * @param fp an input image
 * @return a <strong>new instance</strong> of FloatProcessor: {@mathjax fpv = val + fp}
 */
public static FloatProcessor add(float val, FloatProcessor fp) {
    FloatProcessor out = new FloatProcessor(fp.getWidth(), fp.getHeight());
    out.setMask(fp.getMask());
    for (int i = 0, im = fp.getWidth(); i < im; i++) {
        for (int j = 0, jm = fp.getHeight(); j < jm; j++) {
            out.setf(i, j, val + fp.getPixelValue(i, j));
        }
    }

    return out;
}
 
Example 5
Source File: GeodesicReconstructionHybridTest.java    From MorphoLibJ with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Test
public void testReconstructByDilationFloatC8() {
	// size of images
	int width = 16;
	int height = 10;

	FloatProcessor mask 		= new FloatProcessor(16, 10);
	FloatProcessor marker 		= new FloatProcessor(16, 10);
	FloatProcessor expected 	= new FloatProcessor(16, 10);

	// initialize mask, marker, and expected images
	int[] maskProfile = {10, 10, 40, 40, 40, 40, 20, 20, 30, 30, 10, 10, 30, 30, 0, 0};
	int[] markerProfile = {0, 0, 0, 30, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
	int[] expectedProfile = {10, 10, 30, 30, 30, 30, 20, 20, 20, 20, 10, 10, 10, 10, 0, 0};
	for (int y = 0; y < height; y++) {
		for (int x = 0; x < width; x++) {
			mask.setf(x, y, maskProfile[x]);
			marker.setf(x, y, markerProfile[x]);
			expected.setf(x, y, expectedProfile[x]);
		}
	}

	// Compute geodesic reconstruction by dilation
	GeodesicReconstructionHybrid algo = new GeodesicReconstructionHybrid(
			GeodesicReconstructionType.BY_DILATION, 8);
	ImageProcessor result = algo.applyTo(marker, mask);
	//		printImage(result);

	for (int y = 0; y < height; y++) {
		for (int x = 0; x < width; x++) {
			assertEquals(expectedProfile[x], result.getf(x, y), .01);
		}
	}

}
 
Example 6
Source File: ImageMath.java    From thunderstorm with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Calculate a {@code val}-th power of an image {@code fp}.
 * 
 * @param val {@code val}-th power of {@code fp}
 * @param fp an input image
 * @return a <strong>new instance</strong> of FloatProcessor: {@mathjax fpv = fp ^ val}
 */
public static FloatProcessor power(FloatProcessor fp, float val) {
    FloatProcessor out = new FloatProcessor(fp.getWidth(), fp.getHeight());
    out.setMask(fp.getMask());
    for (int i = 0, im = fp.getWidth(); i < im; i++) {
        for (int j = 0, jm = fp.getHeight(); j < jm; j++) {
            out.setf(i, j, (float)pow((double)fp.getPixelValue(i, j), (double)val));
        }
    }

    return out;
}
 
Example 7
Source File: ImageMath.java    From thunderstorm with GNU General Public License v3.0 5 votes vote down vote up
public static FloatProcessor logOr(FloatProcessor a, FloatProcessor b) {
    if((a.getWidth() != b.getWidth()) || (a.getHeight()!= b.getHeight())) {
        throw new IllegalArgumentException("Error during evaluation of `a|b` expression! Both operands must be of the same size!");
    }
    FloatProcessor res = new FloatProcessor(a.getWidth(), a.getHeight());
    res.setMask(a.getMask() != null ? a.getMask(): b.getMask());
    for(int x = 0; x < a.getWidth(); x++) {
        for(int y = 0; y < a.getHeight(); y++) {
            res.setf(x, y, (((a.getf(x, y) != 0.0f) || (b.getf(x, y) != 0.0f)) ? 1.0f : 0.0f));
        }
    }
    return res;
}
 
Example 8
Source File: ImageMath.java    From thunderstorm with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Divide values of one image by values of the other image.
 * 
 * The images are required to be of the same size.
 *
 * @param fp1 an input image which the other input image ({@code fp2}) will be divided by
 * @param fp2 another input image which will divide the {@code fp1}
 * @return a <strong>new instance</strong> of FloatProcessor: {@mathjax fp3 = fp1 / fp2}
 */
public static FloatProcessor divide(FloatProcessor fp1, FloatProcessor fp2) {
    assert (fp1.getWidth() == fp2.getWidth());
    assert (fp1.getHeight() == fp2.getHeight());

    FloatProcessor out = new FloatProcessor(fp1.getWidth(), fp1.getHeight());
    out.setMask(fp1.getMask() != null ? fp1.getMask(): fp2.getMask());
    for (int i = 0, im = fp1.getWidth(); i < im; i++) {
        for (int j = 0, jm = fp1.getHeight(); j < jm; j++) {
            out.setf(i, j, fp1.getPixelValue(i, j) / fp2.getPixelValue(i, j));
        }
    }

    return out;
}
 
Example 9
Source File: GeodesicReconstructionScanningTest.java    From MorphoLibJ with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Test
public void testReconstructByDilationFloatC8() {
	// size of images
	int width = 16;
	int height = 10;

	FloatProcessor mask 		= new FloatProcessor(16, 10);
	FloatProcessor marker 		= new FloatProcessor(16, 10);
	FloatProcessor expected 	= new FloatProcessor(16, 10);

	// initialize mask, marker, and expected images
	int[] maskProfile = {10, 10, 40, 40, 40, 40, 20, 20, 30, 30, 10, 10, 30, 30, 0, 0};
	int[] markerProfile = {0, 0, 0, 30, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
	int[] expectedProfile = {10, 10, 30, 30, 30, 30, 20, 20, 20, 20, 10, 10, 10, 10, 0, 0};
	for (int y = 0; y < height; y++) {
		for (int x = 0; x < width; x++) {
			mask.setf(x, y, maskProfile[x]);
			marker.setf(x, y, markerProfile[x]);
			expected.setf(x, y, expectedProfile[x]);
		}
	}

	// Compute geodesic reconstruction by dilation
	GeodesicReconstructionScanning algo = new GeodesicReconstructionScanning(
			GeodesicReconstructionType.BY_DILATION, 8);
	ImageProcessor result = algo.applyTo(marker, mask);
	//		printImage(result);

	for (int y = 0; y < height; y++) {
		for (int x = 0; x < width; x++) {
			assertEquals(expectedProfile[x], result.getf(x, y), .01);
		}
	}

}
 
Example 10
Source File: ImageMath.java    From thunderstorm with GNU General Public License v3.0 5 votes vote down vote up
public static FloatProcessor relNeq(Double val, FloatProcessor mat) {
    float v = val.floatValue();
    FloatProcessor res = new FloatProcessor(mat.getWidth(), mat.getHeight());
    res.setMask(mat.getMask());
    for(int x = 0; x < mat.getWidth(); x++) {
        for(int y = 0; y < mat.getHeight(); y++) {
            res.setf(x, y, ((mat.getf(x, y) != v) ? 1.0f : 0.0f));
        }
    }
    return res;
}
 
Example 11
Source File: ImageMath.java    From thunderstorm with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Subtract one image to the other.
 * 
 * The images are required to be of the same size.
 *
 * @param fp1 an input image which the other input image ({@code fp2}) will be subtracted from
 * @param fp2 another input image which will be subtracted from {@code fp1}
 * @return a <strong>new instance</strong> of FloatProcessor: {@mathjax fp3 = fp1 - fp2}
 */
public static FloatProcessor subtract(FloatProcessor fp1, FloatProcessor fp2) {
    assert (fp1.getWidth() == fp2.getWidth());
    assert (fp1.getHeight() == fp2.getHeight());

    FloatProcessor out = new FloatProcessor(fp1.getWidth(), fp1.getHeight());
    out.setMask(fp1.getMask() != null ? fp1.getMask(): fp2.getMask());
    for (int i = 0, im = fp1.getWidth(); i < im; i++) {
        for (int j = 0, jm = fp1.getHeight(); j < jm; j++) {
            out.setf(i, j, fp1.getPixelValue(i, j) - fp2.getPixelValue(i, j));
        }
    }

    return out;
}
 
Example 12
Source File: ValueToNoise.java    From TrakEM2 with GNU General Public License v3.0 5 votes vote down vote up
final static private void processFloat(final FloatProcessor ip, final float value, final double min, final double max) {
	final double scale = max - min;
	final Random rnd = new Random();
	final int n = ip.getWidth() * ip.getHeight();
	for (int i =0; i < n; ++i) {
		final float v = ip.getf(i);
		if (v == value)
			ip.setf(i, (float)(rnd.nextDouble() * scale + min));
	}
}
 
Example 13
Source File: DataGenerator.java    From thunderstorm with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Replaces each pixel value with a sample from a Gamma distribution with shape equal to the original pixel value and scale equal to the gain parameter.
 */
FloatProcessor sampleGamma(FloatProcessor fp, double gain){
    for(int i = 0; i < fp.getPixelCount(); i ++){
        double value = fp.getf(i);
        value = rand.nextGamma(value + 1e-10, gain);
        fp.setf(i, (float)value);
    }
    return fp;
}
 
Example 14
Source File: DataGenerator.java    From thunderstorm with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Replaces each pixel value with a sample from a poisson distribution with mean value equal to the pixel original value.
 */
FloatProcessor samplePoisson(FloatProcessor fp){
    for(int i = 0; i < fp.getPixelCount(); i ++){
        float mean =  fp.getf(i);

        double value = mean > 0 ? (rand.nextPoisson(mean)) : 0;
        fp.setf(i, (float)value);
    }
    return fp;
}
 
Example 15
Source File: DataGenerator.java    From thunderstorm with GNU General Public License v3.0 5 votes vote down vote up
public FloatProcessor generateBackground(int width, int height, Drift drift, Range bkg) {
    // padd the background image; crop the center of the image later, after the drift is applied
    FloatProcessor img = new FloatProcessor(width + 2*(int)ceil(drift.dist), height + 2*(int)ceil(drift.dist));
    for(int x = 0, w = img.getWidth(); x < w; x++)
        for(int y = 0, h = img.getHeight(); y < h; y++)
            img.setf(x, y, (float)getNextUniform(bkg.from, bkg.to));
    IFilter filter = new BoxFilter(1+2*(int)(((double)Math.min(width, width))/8.0));
    return filter.filterImage(img);
}
 
Example 16
Source File: DataGenerator.java    From thunderstorm with GNU General Public License v3.0 5 votes vote down vote up
public FloatProcessor generatePoissonNoise(int width, int height, double stddev_photons) {
    FloatProcessor img = new FloatProcessor(width, height);
        for(int x = 0; x < width; x++)
            for(int y = 0; y < height; y++)
                img.setf(x, y, (float)(rand.nextPoisson(stddev_photons))) ;
    return img;
}
 
Example 17
Source File: ImageMath.java    From thunderstorm with GNU General Public License v3.0 5 votes vote down vote up
public static FloatProcessor modulo(float val, FloatProcessor mat) {
    FloatProcessor res = new FloatProcessor(mat.getWidth(), mat.getHeight());
    res.setMask(mat.getMask());
    float tmp;
    for (int i = 0, im = mat.getWidth(); i < im; i++) {
        for (int j = 0, jm = mat.getHeight(); j < jm; j++) {
            tmp = val / mat.getf(i, j);
            res.setf(i, j, val - (((float)((int)tmp)) * mat.getf(i, j)));
        }
    }
    return res;
}
 
Example 18
Source File: ScatterRendering.java    From thunderstorm with GNU General Public License v3.0 5 votes vote down vote up
/**
 *
 * @param x
 * @param y
 * @param z
 * @param dx
 * @param dz ignored
 */
@Override
protected void drawPoint(double x, double y, double z, double dx, double dz) {
    if(isInBounds(x, y)) {
        int u = (int) ((x - xmin) / resolution);
        int v = (int) ((y - ymin) / resolution);
        int w = (int) ((z - zFrom) / zStep);
        if(w >= 0 && w < zSlices) {
            FloatProcessor img = (FloatProcessor) slices[w];
            img.setf(u, v, 1);
        }
    }
}
 
Example 19
Source File: DistanceTransform5x5Float.java    From MorphoLibJ with GNU Lesser General Public License v3.0 4 votes vote down vote up
private void forwardScan(FloatProcessor distMap, ImageProcessor labelImage) 
{
	this.fireStatusChanged(new AlgoEvent(this, "Forward Scan"));
	
	// Initialize pairs of offset and weights
	int[] dx = new int[]{-1, +1,  -2, -1,  0, +1, +2,  -1};
	int[] dy = new int[]{-2, -2,  -1, -1, -1, -1, -1,   0};
	
	float[] dw = new float[] { 
			weights[2], weights[2], 
			weights[2], weights[1], weights[0], weights[1], weights[2], 
			weights[0] };
	
	//  size of image
	int sizeX = labelImage.getWidth();
	int sizeY = labelImage.getHeight();

	// Iterate over pixels
	for (int y = 0; y < sizeY; y++)
	{
		this.fireProgressChanged(this, y, sizeY);
		for (int x = 0; x < sizeX; x++)
		{
			// get current label
			int label = (int) labelImage.getf(x, y);
			
			// do not process background pixels
			if (label == 0)
				continue;
			
			// current distance value
			float currentDist = distMap.getf(x, y);
			float newDist = currentDist;
			
			// iterate over neighbors
			for (int i = 0; i < dx.length; i++)
			{
				// compute neighbor coordinates
				int x2 = x + dx[i];
				int y2 = y + dy[i];
				
				// check bounds
				if (x2 < 0 || x2 >= sizeX)
					continue;
				if (y2 < 0 || y2 >= sizeY)
					continue;
				
				if ((int) labelImage.getf(x2, y2) != label)
				{
					// Update with distance to nearest different label
				    newDist = Math.min(newDist, dw[i]);
				}
				else
				{
					// Increment distance
					newDist = Math.min(newDist, distMap.getf(x2, y2) + dw[i]);
				}
			}
			
			if (newDist < currentDist) 
			{
				distMap.setf(x, y, newDist);
			}
		}
	}
	
	this.fireProgressChanged(this, sizeY, sizeY);
}
 
Example 20
Source File: ImageMath.java    From thunderstorm with GNU General Public License v3.0 4 votes vote down vote up
/**
 * Apply a {@code mask} to an input {@code image}.
 * 
 * The masking works simply by checking where is a 0 in the mask image and
 * setting the corresponding pixel in the input image to 0 as well. Hence the
 * mask does not have to be binary.
 * 
 * For example let's have the following input image:
 * <pre>
 * {@code
 * 123
 * 456
 * 789}
 * </pre>
 * and the following mask:
 * <pre>
 * {@code
 * 560
 * 804
 * 032}
 * </pre>
 * Then the result of applying the mask is:
 * <pre>
 * {@code
 * 120
 * 406
 * 089}
 * </pre>
 * 
 * @param image an input image
 * @param mask a mask image
 * @return a <strong>new instance</strong> of FloatProcessor that contains the input image after the mask was applied
 */
public static FloatProcessor applyMask(FloatProcessor image, FloatProcessor mask) {
    assert (image.getWidth() == mask.getWidth());
    assert (image.getHeight() == mask.getHeight());

    FloatProcessor result = new FloatProcessor(image.getWidth(), image.getHeight(), (float[]) image.getPixelsCopy(), null);
    for (int x = 0, xm = image.getWidth(); x < xm; x++) {
        for (int y = 0, ym = image.getHeight(); y < ym; y++) {
            if (mask.getf(x, y) == 0.0f) {
                result.setf(x, y, 0.0f);
            }
        }
    }
    
    return result;
}