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

The following examples show how to use ij.process.ImageProcessor#duplicate() . 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: ImageCalculator.java    From MorphoLibJ with GNU Lesser General Public License v3.0 6 votes vote down vote up
/**
 * Example that computes the maximum over two images:
 * 
 * <pre>
 * <code>
 * ImageProcessor image1 = ...
 * ImageProcessor image2 = ...
 * ImageProcessor result = ImageCalculator.combineImages(
 * 		image1, image2, ImageCalculator.Operation.MAX);  
 * </code>
 * </pre>
 * 
 * @param image1
 *            the first input image
 * @param image2
 *            the second input image
 * @param op
 *            the operation to apply
 * @return the result of operation applied to the pair of input images
 */
public static final ImageProcessor combineImages(ImageProcessor image1, ImageProcessor image2,
        Operation op)
{
    int width = image1.getWidth();
    int height = image1.getHeight();
    
    ImageProcessor result = image1.duplicate();
    
    float v1, v2, vr;
    for (int y = 0; y < height; y++)
    {
        for (int x = 0; x < width; x++)
        {
            v1 = image1.getf(x, y);
            v2 = image2.getf(x, y);
            vr = op.applyTo(v1, v2);
            result.setf(x, y, vr);
        }
    }
    
    return result;
}
 
Example 2
Source File: Distortion_Correction.java    From TrakEM2 with GNU General Public License v3.0 6 votes vote down vote up
ImageProcessor getGradientSobel(final ImageProcessor ip){
	final ImageProcessor ipGrad = ip.duplicate();
	ipGrad.max(0.0);

	for (int i=1; i<ipGrad.getWidth()-1; i++){
		for (int j=1; j<ipGrad.getHeight()-1; j++){
			if(ip.get(i-1,j-1)==0 || ip.get(i-1,j)==0 || ip.get(i-1,j+1)==0 ||
					ip.get(i,j-1)==0 || ip.get(i,j)==0 || ip.get(i,j+1)==0 ||
					ip.get(i+1,j-1)==0 || ip.get(i+1,j)==0 || ip.get(i+1,j+1)==0 )
				continue;

			final double gradX = (double) -ip.get(i-1, j-1) - 2* ip.get(i-1,j) - ip.get(i-1,j+1)
			+ip.get(i+1, j-1) + 2* ip.get(i+1,j) + ip.get(i+1,j+1);

			final double gradY = (double) -ip.get(i-1, j-1) - 2* ip.get(i,j-1) - ip.get(i+1,j-1)
			+ip.get(i-1, j+1) + 2* ip.get(i,j+1) + ip.get(i+1,j+1);

			final double mag = Math.sqrt(gradX*gradX + gradY*gradY);
			ipGrad.setf(i,j,(float) mag);
		}
	}
	return ipGrad;
}
 
Example 3
Source File: ImageJSkeleton.java    From MorphoLibJ with GNU Lesser General Public License v3.0 6 votes vote down vote up
public ImageProcessor process(ImageProcessor image)
{
   
    ImageProcessor result = image.duplicate();
    
    // note: original IJ algo clears pixels on the boundary
    
    int removedPixels;
    do
    {
        removedPixels = thin(result, 1, table1);
        removedPixels += thin(result, 2, table1);
    } while (removedPixels > 0);
    
    // use a second table to remove "stuck" pixels
    do
    {
        removedPixels = thin(result, 1, table2);
        removedPixels += thin(result, 2, table2);
    } while (removedPixels > 0);

    return result;
    
}
 
Example 4
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 5
Source File: Reconstruction.java    From MorphoLibJ with GNU Lesser General Public License v3.0 6 votes vote down vote up
/**
 * Fills the holes in the input image, by (1) inverting the image, (2) 
 * performing a morphological reconstruction initialized with inverted image
 * boundary and (3) by inverting the result.
 * 
 * @see #killBorders(ImageProcessor)
 * 
 * @param image the image to process
 * @return a new image with holes filled
 */
public final static ImageProcessor fillHoles(ImageProcessor image) 
{
	// Image size
	int width = image.getWidth();
	int height = image.getHeight();

	// Initialize marker image with white everywhere except at borders
	ImageProcessor markers = image.duplicate();
	for (int y = 1; y < height-1; y++) 
	{
		for (int x = 1; x < width-1; x++) 
		{
			markers.setf(x, y, Float.MAX_VALUE);
		}
	}
	
	// Reconstruct image from borders to find touching structures
	return reconstructByErosion(markers, image);
}
 
Example 6
Source File: GeodesicDistanceTransformFloatTest.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[] { 3, 4 };
	GeodesicDistanceTransform algo = new GeodesicDistanceTransformFloat(
			weights, true);
	ImageProcessor map = algo.geodesicDistanceMap(marker, mask);

	assertEquals(259, map.getf(190, 211), .01);
}
 
Example 7
Source File: AbstractSeparableStrel.java    From MorphoLibJ with GNU Lesser General Public License v3.0 6 votes vote down vote up
public ImageProcessor dilation(ImageProcessor image)
{
	// Allocate memory for result
	ImageProcessor result = image.duplicate();
	
	// Extract structuring elements
	Collection<InPlaceStrel> strels = this.decompose();
	int n = strels.size();
	
	
	// Dilation
	int i = 1;
	for (InPlaceStrel strel : strels)
	{
		fireStatusChanged(this, createStatusMessage("Dilation", i, n));
		runDilation(result, strel);
		i++;
	}
	
	// clear status bar
	fireStatusChanged(this, "");
	
	return result;
}
 
Example 8
Source File: AbstractSeparableStrel.java    From MorphoLibJ with GNU Lesser General Public License v3.0 6 votes vote down vote up
public ImageProcessor erosion(ImageProcessor image) 
{
	// Allocate memory for result
	ImageProcessor result = image.duplicate();
	
	// Extract structuring elements
	Collection<InPlaceStrel> strels = this.decompose();
	int n = strels.size();
	
	// Erosion
	int i = 1;
	for (InPlaceStrel strel : strels)
	{
		fireStatusChanged(this, createStatusMessage("Erosion", i, n));
		runErosion(result, strel);
		i++;
	}
	
	// clear status bar
	fireStatusChanged(this, "");
	
	return result;
}
 
Example 9
Source File: GeodesicDistanceTransformShort5x5Test.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[] { 5, 7, 11 };
	GeodesicDistanceTransform algo = new GeodesicDistanceTransformShort5x5(
			weights, true);
	ImageProcessor map = algo.geodesicDistanceMap(marker, mask);

	assertEquals(250, map.get(190, 210));
}
 
Example 10
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 11
Source File: MinimaAndMaxima.java    From MorphoLibJ with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * Imposes the minima given by marker image into the input image, using 
 * the specified connectivity.
 * 
 * @param image
 *            the image to process
 * @param minima
 *            a binary image of minima 
 * @param conn
 *            the connectivity for minima, that should be either 4 or 8
 * @return the result of minima imposition
 */
public final static ImageProcessor imposeMinima(ImageProcessor image,
		ImageProcessor minima, int conn)
{
	int width = image.getWidth();
	int height = image.getHeight();
	
	ImageProcessor marker = image.duplicate();
	ImageProcessor mask = image.duplicate();
	for (int y = 0; y < height; y++)
	{
		for (int x = 0; x < width; x++)
		{
			if (minima.get(x, y) > 0)
			{
				marker.set(x, y, 0);
				mask.set(x, y, 0);
			} 
			else
			{
				marker.set(x, y, 255);
				mask.set(x, y, image.get(x, y)+1);
			}
		}
	}
	
	return Reconstruction.reconstructByErosion(marker, mask, conn);
}
 
Example 12
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 13
Source File: MinimaAndMaxima.java    From MorphoLibJ with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * Computes the regional maxima 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 maxima, that should be either 4 or 8
 * @return the regional maxima of input image
 */
public final static ImageProcessor regionalMaximaByReconstruction(
		ImageProcessor image,
		int conn) 
{
	// Compute mask image
	ImageProcessor mask = image.duplicate();
	mask.add(1);
	
	// Call geodesic reconstruction algorithm
	GeodesicReconstructionAlgo algo = new GeodesicReconstructionHybrid(
			GeodesicReconstructionType.BY_DILATION, conn);
	ImageProcessor rec = algo.applyTo(image, mask);
	
	// allocate memory for result
	int width = image.getWidth();
	int height = image.getHeight();
	ImageProcessor result = new ByteProcessor(width, height);
	
	// create binary result image
	for (int y = 0; y < height; y++) {
		for (int x = 0; x < width; x++) {
			if (mask.get(x, y) > rec.get(x, y)) 
				result.set(x,  y, 255);
			else
				result.set(x,  y, 0);
		}
	}
	
	return result;
}
 
Example 14
Source File: FloodFillTest.java    From MorphoLibJ with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Test
	public final void testFloodFillC8_AllCases() {
		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]);
			}
		}
		
		ImageProcessor result = image.duplicate(); 
		FloodFill.floodFill(result, 7, 4, 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(10, result.get(x, y));
			}
		}
		
	}
 
Example 15
Source File: Patch.java    From TrakEM2 with GNU General Public License v3.0 5 votes vote down vote up
/** Returns a PatchImage object containing the bottom-of-transformation-stack image and alpha mask, if any (except the AffineTransform, which is used for direct hw-accel screen rendering). */
public Patch.PatchImage createTransformedImage() {
	final Patch.PatchImage pi = createCoordinateTransformedImage();
	if (null != pi) return pi;
	// else, a new one with the untransformed, original image (a duplicate):
	final ImageProcessor ip = getImageProcessor();
	if (null == ip) return null;
	project.getLoader().releaseToFit(o_width, o_height, type, 3);
	final ImageProcessor copy = ip.duplicate();
	copy.setColorModel(ip.getColorModel()); // one would expect "duplicate" to do this but it doesn't!
	return new PatchImage(copy, getAlphaMask(), null, new Rectangle(0, 0, o_width, o_height), false);
}
 
Example 16
Source File: OrientedLineStrel.java    From MorphoLibJ with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Override
public ImageProcessor dilation(ImageProcessor image)
{
	// Allocate memory for result
	ImageProcessor result = image.duplicate();

	BorderManager bm = new MirroringBorder(image);

	// Iterate on image pixels
	for (int y = 0; y < image.getHeight(); y++)
	{
		for (int x = 0; x < image.getWidth(); x++)
		{
			// reset accumulator
			double res = Double.MIN_VALUE;

			// iterate on neighbors
			for (int i = 0; i < shifts.length; i++) 
			{
				double value = bm.getf(x + shifts[i][0], y + shifts[i][1]);
				res = Math.max(res, value);
			}
			
			// compute result
			result.setf(x, y, (float) res);
		}			
	}
	
	return result;
}
 
Example 17
Source File: Reconstruction.java    From MorphoLibJ with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * Removes the border of the input image, by performing a morphological
 * reconstruction initialized with image boundary.
 * 
 * @see #fillHoles(ImageProcessor)
 * 
 * @param image the image to process
 * @return a new image with borders removed
 */
public final static ImageProcessor killBorders(ImageProcessor image) 
{
	// Image size
	int width = image.getWidth();
	int height = image.getHeight();
	
	// Initialize marker image with zeros everywhere except at borders
	ImageProcessor markers = image.duplicate();
	for (int y = 1; y < height-1; y++) 
	{
	    for (int x = 1; x < width-1; x++) 
	    {
	        markers.setf(x, y, Float.NEGATIVE_INFINITY);
	    }
	}

	// Reconstruct image from borders to find touching structures
	ImageProcessor result = reconstructByDilation(markers, image);
	
	// removes result from original image
	for (int y = 0; y < height; y++) 
	{
		for (int x = 0; x < width; x++) 
		{
			int val = image.get(x, y) - result.get(x, y);
			result.set(x, y, Math.max(val, 0));
		}
	}
	
	return result;
}
 
Example 18
Source File: AbstractInPlaceStrel.java    From MorphoLibJ with GNU Lesser General Public License v3.0 4 votes vote down vote up
public ImageProcessor opening(ImageProcessor image) {
	ImageProcessor result = image.duplicate();
	this.inPlaceErosion(result);
	this.reverse().inPlaceDilation(result);
	return result;
}
 
Example 19
Source File: FSLoader.java    From TrakEM2 with GNU General Public License v3.0 4 votes vote down vote up
/** Returns the last Patch. */
protected Patch importStackAsPatches(final Project project, final Layer first_layer, final double x, final double y, final ImagePlus imp_stack, final boolean as_copy, final String filepath) {
	Utils.log2("FSLoader.importStackAsPatches filepath=" + filepath);
	String target_dir = null;
	if (as_copy) {
		DirectoryChooser dc = new DirectoryChooser("Folder to save images");
		target_dir = dc.getDirectory();
		if (null == target_dir) return null; // user canceled dialog
		if (IJ.isWindows()) target_dir = target_dir.replace('\\', '/');
		if (target_dir.length() -1 != target_dir.lastIndexOf('/')) {
			target_dir += "/";
		}
	}

	// Double.MAX_VALUE is a flag to indicate "add centered"
	double pos_x = Double.MAX_VALUE != x ? x : first_layer.getLayerWidth()/2 - imp_stack.getWidth()/2;
	double pos_y = Double.MAX_VALUE != y ? y : first_layer.getLayerHeight()/2 - imp_stack.getHeight()/2;
	final double thickness = first_layer.getThickness();
	final String title = Utils.removeExtension(imp_stack.getTitle()).replace(' ', '_');
	Utils.showProgress(0);
	Patch previous_patch = null;
	final int n = imp_stack.getStackSize();

	final ImageStack stack = imp_stack.getStack();
	final boolean virtual = stack.isVirtual();
	final VirtualStack vs = virtual ? (VirtualStack)stack : null;

	for (int i=1; i<=n; i++) {
		Layer layer = first_layer;
		double z = first_layer.getZ() + (i-1) * thickness;
		if (i > 1) layer = first_layer.getParent().getLayer(z, thickness, true); // will create new layer if not found
		if (null == layer) {
			Utils.log("Display.importStack: could not create new layers.");
			return null;
		}
		String patch_path = null;

		ImagePlus imp_patch_i = null;
		if (virtual) { // because we love inefficiency, every time all this is done again
			//VirtualStack vs = (VirtualStack)imp_stack.getStack();
			String vs_dir = vs.getDirectory().replace('\\', '/');
			if (!vs_dir.endsWith("/")) vs_dir += "/";
			String iname = vs.getFileName(i);
			patch_path = vs_dir + iname;
			Utils.log2("virtual stack: patch path is " + patch_path);
			releaseToFit(new File(patch_path).length() * 3);
			Utils.log2(i + " : " + patch_path);
			imp_patch_i = openImage(patch_path);
		} else {
			ImageProcessor ip = stack.getProcessor(i);
			if (as_copy) ip = ip.duplicate();
			imp_patch_i = new ImagePlus(title + "__slice=" + i, ip);
		}

		String label = stack.getSliceLabel(i);
		if (null == label) label = "";
		Patch patch = null;
		if (as_copy) {
			patch_path = target_dir + cleanSlashes(imp_patch_i.getTitle()) + ".zip";
			ini.trakem2.io.ImageSaver.saveAsZip(imp_patch_i, patch_path);
			patch = new Patch(project, label + " " + title + " " + i, pos_x, pos_y, imp_patch_i);
		} else if (virtual) {
			patch = new Patch(project, label, pos_x, pos_y, imp_patch_i);
		} else {
			patch_path = filepath + "-----#slice=" + i;
			//Utils.log2("path is "+ patch_path);
			final AffineTransform atp = new AffineTransform();
			atp.translate(pos_x, pos_y);
			patch = new Patch(project, getNextId(), label + " " + title + " " + i, imp_stack.getWidth(), imp_stack.getHeight(), imp_stack.getWidth(), imp_stack.getHeight(), imp_stack.getType(), false, imp_stack.getProcessor().getMin(), imp_stack.getProcessor().getMax(), atp);
			patch.addToDatabase();
			//Utils.log2("type is " + imp_stack.getType());
		}
		Utils.log2("B: " + i + " : " + patch_path);
		addedPatchFrom(patch_path, patch);
		if (!as_copy && !virtual) {
			if (virtual) cache(patch, imp_patch_i); // each slice separately
			else cache(patch, imp_stack); // uses the entire stack, shared among all Patch instances
		}
		if (isMipMapsRegenerationEnabled()) regenerateMipMaps(patch); // submit for regeneration
		if (null != previous_patch) patch.link(previous_patch);
		layer.add(patch);
		previous_patch = patch;
		Utils.showProgress(i * (1.0 / n));
	}
	Utils.showProgress(1.0);

	// update calibration
	// TODO

	// return the last patch
	return previous_patch;
}
 
Example 20
Source File: MinimaAndMaxima.java    From MorphoLibJ with GNU Lesser General Public License v3.0 3 votes vote down vote up
/**
 * Computes the extended minima in grayscale image <code>image</code>, 
 * keeping minima with the specified dynamic, and using the specified 
 * connectivity.
 * 
 * @param image
 *            the image to process
 * @param dynamic
 *            the minimal difference between a minima and its boundary 
 * @param conn
 *            the connectivity for minima, that should be either 4 or 8
 * @return the extended minima of input image
 */
public final static ImageProcessor extendedMinima(ImageProcessor image,
		double dynamic, int conn)
{
	ImageProcessor marker = image.duplicate();
	marker.add(dynamic);
	
	GeodesicReconstructionAlgo algo = new GeodesicReconstructionHybrid(
			GeodesicReconstructionType.BY_EROSION, conn);
	ImageProcessor rec = algo.applyTo(marker, image);

	return regionalMinima(rec, conn);
}