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

The following examples show how to use ij.process.ImageProcessor#createProcessor() . 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: NeighborLabelsPlugin.java    From MorphoLibJ with GNU Lesser General Public License v3.0 6 votes vote down vote up
/**
 * Creates a new image containing only the specified labels.
 * 
 * @param image
 *            a planar image of labels
 * @param labels
 *            the list of values to keep
 * @return a new binary image containing 255 only for pixels with values
 *         belonging to the list of labels.
 */
private static final ImageProcessor selectedLabelsToMask(ImageProcessor image, int[] labels) 
{
    int sizeX = image.getWidth();
    int sizeY = image.getHeight();
    
    ImageProcessor result = image.createProcessor(sizeX,  sizeY);
    
    TreeSet<Integer> labelSet = new TreeSet<Integer>();
    for (int i = 0; i < labels.length; i++) 
    {
        labelSet.add(labels[i]);
    }
    
    for (int y = 0; y < sizeY; y++) 
    {
        for (int x = 0; x < sizeX; x++)
        {
            int value = (int) image.getf(x, y); 
            if (labelSet.contains(value)) 
                result.setf(x, y, 255);
        }
    }
    
    return result;
}
 
Example 2
Source File: ExtendBordersPlugin.java    From MorphoLibJ with GNU Lesser General Public License v3.0 6 votes vote down vote up
/**
 * Adds the specified number of pixels around the input image, and returns
 * the resulting image. 
 * 
 * @param image
 *            the input image
 * @param left
 *            the number of pixels to add to the left of the image
 * @param right
 *            the number of pixels to add to the right of the image
 * @param top
 *            the number of pixels to add on top of the image
 * @param bottom
 *            the number of pixels to add at the bottom of the image
 * @param border
 *            an instance of BorderManager that specifies the value of
 *            pixels to be added
 * @return a new image with extended borders
 */
public static final ImageProcessor process(ImageProcessor image, 
		int left, int right, int top, int bottom, BorderManager border)
{
	// get image dimensions
	int width = image.getWidth(); 
	int height = image.getHeight(); 
	
	// compute result dimensions
	int width2 = width + left + right;
	int height2 = height + top + bottom;
	ImageProcessor result = image.createProcessor(width2 , height2);
	
	// fill result image
	for (int x = 0; x < width2; x++)
	{
		for (int y = 0; y < height2; y++)
		{
			result.set(x, y, border.get(x - left, y - top));
		}
	}
	
	return result;
}
 
Example 3
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 4
Source File: ShiftedCross3x3Strel_RightTest.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.RIGHT;
	
	ImageProcessor expected = image.createProcessor(10, 10);
	for (int y = 4; y < 6; y++) {
		for (int x = 3; x < 5; 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 5
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 6
Source File: NonLinearTransform.java    From TrakEM2 with GNU General Public License v3.0 6 votes vote down vote up
public ImageProcessor[] transform(final ImageProcessor ip){
	if (!precalculated)
		this.precalculateTransfom();

	final ImageProcessor newIp = ip.createProcessor(ip.getWidth(), ip.getHeight());
	if (ip instanceof ColorProcessor) ip.max(0);
	final ImageProcessor maskIp = new ByteProcessor(ip.getWidth(),ip.getHeight());

	for (int x=0; x < width; x++){
		for (int y=0; y < height; y++){
			if (transField[x][y][0] == -1){
				continue;
			}
			newIp.set(x, y, (int) ip.getInterpolatedPixel((int)transField[x][y][0],(int)transField[x][y][1]));
			maskIp.set(x,y,255);
		}
	}
	return new ImageProcessor[]{newIp, maskIp};
}
 
Example 7
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 8
Source File: LabelImages.java    From MorphoLibJ with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * Creates a new image containing only the specified labels.
 * 
 * @param image
 *            a planar label image
 * @param labels
 *            the list of values to keep
 * @return a new label image containing only the specified labels
 */
public static final ImageProcessor keepLabels(ImageProcessor image, int[] labels)
{
	int sizeX = image.getWidth();
	int sizeY = image.getHeight();
	
	ImageProcessor result = image.createProcessor(sizeX,  sizeY);
	
	TreeSet<Integer> labelSet = new TreeSet<Integer>();
	for (int i = 0; i < labels.length; i++) 
	{
		labelSet.add(labels[i]);
	}
	
	for (int y = 0; y < sizeY; y++) 
	{
		for (int x = 0; x < sizeX; x++)
		{
			int value = (int) image.getf(x, y); 
			if (value == 0)
				continue;
			if (labelSet.contains(value)) 
				result.setf(x, y, value);
		}
	}
	
	return result;
}
 
Example 9
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 10
Source File: ShiftedCross3x3Strel_RightTest.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 = ShiftedCross3x3Strel.RIGHT;
	
	ImageProcessor expected = image.createProcessor(10, 10);
	for (int x = 2; x < 6; x++) {
		expected.set(x, 2, 255);
		expected.set(x, 7, 255);
	}
	for (int y = 3; y < 7; y++) {
		for (int x = 1; x < 7; 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 11
Source File: ShiftedCross3x3Strel_LeftTest.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 = ShiftedCross3x3Strel.LEFT;
	
	ImageProcessor expected = image.createProcessor(10, 10);
	for (int x = 4; x < 8; x++) {
		expected.set(x, 2, 255);
		expected.set(x, 7, 255);
	}
	for (int y = 3; y < 7; y++) {
		for (int x = 3; 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 12
Source File: Coloc_2.java    From Colocalisation_Analysis with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Creates appropriate data structures from the ROI information
 * passed. If an irregular ROI is found, it will be put into a
 * frame of its bounding box size and put into an {@code Image<T>}.
 *
 * In the end the members ROIs, masks and maskBBs will be
 * filled if ROIs or masks were found. They will be null
 * otherwise.
 */
protected void createMasksAndRois(final Roi[] rois, final int width,
	final int height)
{
	// create empty list
	masks.clear();

	for (final Roi r : rois) {
		final MaskInfo mi = new MaskInfo();
		// add it to the list of masks/ROIs
		masks.add(mi);
		// get the ROIs/masks bounding box
		final Rectangle rect = r.getBounds();
		mi.roi = new BoundingBox(new long[] { rect.x, rect.y }, new long[] {
			rect.width, rect.height });
		final ImageProcessor ipMask = r.getMask();
		// check if we got a regular ROI and return if so
		if (ipMask == null) {
			continue;
		}

		// create a mask processor of the same size as a slice
		final ImageProcessor ipSlice = ipMask.createProcessor(width, height);
		// fill the new slice with black
		ipSlice.setValue(0.0);
		ipSlice.fill();
		// position the mask on the new mask processor
		ipSlice.copyBits(ipMask, (int) mi.roi.offset[0], (int) mi.roi.offset[1],
			Blitter.COPY);
		// create an Image<T> out of it
		final ImagePlus maskImp = new ImagePlus("Mask", ipSlice);
		// and remember it and the masks bounding box
		mi.mask = ImagePlusAdapter.<T> wrap(maskImp);
	}
}
 
Example 13
Source File: ExpandLabelsPlugin.java    From MorphoLibJ with GNU Lesser General Public License v3.0 4 votes vote down vote up
/**
 * Expand labels by a given factor
 * @param image  input label image
 * @param ratio  percentage of expansion (values between 0 and 100)
 * @return expanded image
 */
public static final ImageProcessor expandLabels(ImageProcessor image,
		float ratio) 
{
	// size of input image
	int sizeX = image.getWidth();
	int sizeY = image.getHeight();
	
	// size of result image
	int sizeX2 = (int) Math.round(sizeX * (1.0 + ratio / 100.0));
	int sizeY2 = (int) Math.round(sizeY * (1.0 + ratio / 100.0));
	
	// allocate memory for result
	ImageProcessor result = image.createProcessor(sizeX2, sizeY2);

	// compute centroids of labels
	int[] labels = LabelImages.findAllLabels(image);
	double[][] centroids = Centroid.centroids(image, labels);
	
	// compute shift associated to each label
	int nLabels = labels.length;
	int[][] shifts = new int[nLabels][2];
	for (int i = 0; i < nLabels; i++)
	{
		shifts[i][0] = (int) Math.floor(centroids[i][0] * ratio / 100.0);
		shifts[i][1] = (int) Math.floor(centroids[i][1] * ratio / 100.0);
	}
	
       // create associative array to know index of each label
	HashMap<Integer, Integer> labelIndices = new HashMap<Integer, Integer>();
       for (int i = 0; i < nLabels; i++) 
       {
       	labelIndices.put(labels[i], i);
       }

	for (int y = 0; y < sizeY; y++)
	{
		for (int x = 0; x < sizeX; x++)
		{
			float label = image.getf(x, y);
			if ( Float.compare( label, 0f ) == 0 )
				continue;

			int index = labelIndices.get((int)label);
			int x2 = x + shifts[index][0];
			int y2 = y + shifts[index][1];
			result.setf( x2, y2, label );
		}
	}
	
	return result;
}
 
Example 14
Source File: AreaList.java    From TrakEM2 with GNU General Public License v3.0 4 votes vote down vote up
/** Returns a stack of images representing the pixel data of this LayerSet inside this AreaList. */
public ImagePlus getStack(final int type, final double scale) {
	final ImageProcessor ref_ip = Utils.createProcessor(type, 2, 2);
	if (null == ref_ip) {
		Utils.log("AreaList.getStack: Unknown type " + type);
		return null;
	}
	final Rectangle b = getBoundingBox();
	final int w = (int)(0.5 + b.width * scale);
	final int h = (int)(0.5 + b.height * scale);
	final ImageStack stack = new ImageStack(w, h);
	for (final Layer la : getLayerRange()) {
		final Area area = getArea(la);
		final double z = layer.getZ();
		project.getLoader().releaseToFit(w * h * 10);
		final ImageProcessor ip = ref_ip.createProcessor(w, h);
		if (null == area) {
			stack.addSlice(Double.toString(z), ip);
			continue;
		}
		// Create a ROI from the area at Layer la:
		final AffineTransform aff = getAffineTransformCopy();
		aff.translate(-b.x, -b.y);
		aff.scale(scale, scale);
		final ShapeRoi roi = new ShapeRoi(area.createTransformedArea(aff));
		// Create a cropped snapshot of the images at Layer la under the area:
		final ImageProcessor flat = Patch.makeFlatImage(type, la, b, scale, la.getAll(Patch.class), Color.black);
		flat.setRoi(roi);
		final Rectangle rb = roi.getBounds();
		ip.insert(flat.crop(), rb.x, rb.y);
		// Clear the outside
		final ImagePlus bimp = new ImagePlus("", ip);
		bimp.setRoi(roi);
		ip.setValue(0);
		ip.setBackgroundValue(0);
		IJ.run(bimp, "Clear Outside", "");

		stack.addSlice(Double.toString(z), ip);
	}

	final ImagePlus imp = new ImagePlus("AreaList stack for " + this, stack);
	imp.setCalibration(layer_set.getCalibrationCopy());
	return imp;
}
 
Example 15
Source File: ExportMultilevelTiles.java    From TrakEM2 with GNU General Public License v3.0 4 votes vote down vote up
/** Will flush the prior_snapshot. */
private final ImageProcessor strategySnapshot(
		final ImageProcessor prior_snapshot,
		final List<Patch> patches,
		final double scale,
		final int scale_pow)
{
	// Acquire image of whole srcRect
	final ImageProcessor snapshot;
	if (null != prior_snapshot) {
		snapshot = Downsampler.downsampleImageProcessor(prior_snapshot);
		prior_snapshot.setPixels(null); // flush
	} else {
		snapshot = layer.getProject().getLoader().getFlatImage(layer, srcRect, scale, c_alphas, type, Patch.class, patches, false, Color.black).getProcessor();
	}
	// Iterate tiles
	final Rectangle tile_src = new Rectangle(0, 0, tileSide, tileSide);
	for (int i = 0, row = 0; i < snapshot.getHeight(); i += tileSide, ++row) {
		for (int j = 0, col = 0; j < snapshot.getWidth(); j += tileSide, ++col) {
			final String path = makeTilePath(directory_structure_type, dir, index, row, col, scale_pow);
			// The srcRect for the tile
			tile_src.x = tileSide * col;
			tile_src.y = tileSide * row;
			snapshot.setRoi(tile_src);
			ImageProcessor ip = snapshot.crop();
			// Adjust dimensions: necessary for croppings over the edges of the snapshot
			if (ip.getWidth() < tileSide || ip.getHeight() < tileSide) {
				ImageProcessor ip2 = ip.createProcessor(tileSide, tileSide);
				ip2.insert(ip, 0, 0);
				ip.setPixels(null); // flush
				ip = ip2;
				ip2 = null;
			}
			if (skip_empty_tiles && isEmptyTile(ip)) continue;
			ImagePlus imp = new ImagePlus(path.substring(path.lastIndexOf("/")), ip);
			saver.save(imp, path);
			imp.flush();
			ip = null;
			imp = null;
		}
	}
	return snapshot;
}