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

The following examples show how to use ij.process.ImageProcessor#setRoi() . 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: PatchStack.java    From TrakEM2 with GNU General Public License v3.0 6 votes vote down vote up
/** Does not respect local transform of the patches, this is intended for confocal stacks. */
public ImagePlus createColor256Copy() {
	final Rectangle box = patch[0].getBoundingBox();
	final int width = box.width;
	final int height = box.height;
	Loader loader = patch[0].getProject().getLoader();
	patch[0].getProject().getLoader().releaseToFit(4 * patch.length * width * height); // the montage, in RGB
	final ColorProcessor montage = new ColorProcessor(width*patch.length, height);
	for (int i=0; i<patch.length; i++) {
		montage.insert(this.stack.getProcessor(i+1), i*width, 0);
	}
	final MedianCut mc = new MedianCut(montage);
	loader.releaseToFit(patch.length * width * height);
	ImageProcessor m2 = mc.convertToByte(256);
	final ImageStack st = new ImageStack(width, height);
	for (int i=0; i<patch.length; i++) {
		m2.setRoi(i*width, 0, width, height);
		loader.releaseToFit(width * height);
		st.addSlice(null, m2.crop());
       	}
	ImagePlus imp = new ImagePlus("color256", st);
	imp.setCalibration(patch[0].getLayer().getParent().getCalibrationCopy());
	//imp.getCalibration().pixelDepth = patch[0].getLayer().getThickness();
	return imp;
}
 
Example 2
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 3
Source File: RankFiltersOrbit.java    From orbit-image-analysis with GNU General Public License v3.0 5 votes vote down vote up
/** Filters an image by any method except 'despecle' (for 'despeckle', use 'median' and radius=1)
 * @param ip The image subject to filtering
 * @param radius The kernel radius
 * @param filterType as defined above; DESPECKLE is not a valid type here; use median and
 *		  a radius of 1.0 instead
 * @param whichOutliers BRIGHT_OUTLIERS or DARK_OUTLIERS for 'outliers' filter
 * @param threshold Threshold for 'outliers' filter
 */
public void rank(ImageProcessor ip, double radius, int filterType, int whichOutliers, float threshold) {
	Rectangle roi = ip.getRoi();
	ImageProcessor mask = ip.getMask();
	Rectangle roi1 = null;
	int[] lineRadii = makeLineRadii(radius);

	float minMaxOutliersSign = filterType==MIN ? -1f : 1f;
	if (filterType == OUTLIERS)		//sign is -1 for high outliers: compare number with minimum
		minMaxOutliersSign = (ip.isInvertedLut()==(whichOutliers==DARK_OUTLIERS)) ? -1f : 1f;

	boolean isImagePart = (roi.width<ip.getWidth()) || (roi.height<ip.getHeight());
	boolean[] aborted = new boolean[1];						// returns whether interrupted during preview or ESC pressed
	for (int ch=0; ch<ip.getNChannels(); ch++) {
		int filterType1 = filterType;
		if (isMultiStepFilter(filterType)) {
			filterType1 = (filterType==OPEN) ? MIN : MAX;
			if (isImagePart) { //composite filters ('open maxima' etc.) need larger area in first step
				int kRadius = kRadius(lineRadii);
				int kHeight = kHeight(lineRadii);
				Rectangle roiClone = (Rectangle)roi.clone();
				roiClone.grow(kRadius, kHeight/2);
				roi1 = roiClone.intersection(new Rectangle(ip.getWidth(), ip.getHeight()));
				ip.setRoi(roi1);
			}
		}
		doFiltering(ip, lineRadii, filterType1, minMaxOutliersSign, threshold, ch, aborted);
		if (aborted[0]) break;
		if (isMultiStepFilter(filterType)) {
			ip.setRoi(roi);
			ip.setMask(mask);
			int filterType2 = (filterType==OPEN) ? MAX : MIN;
			doFiltering(ip, lineRadii, filterType2, minMaxOutliersSign, threshold, ch, aborted);
			if (aborted[0]) break;
			if (isImagePart)
				resetRoiBoundary(ip, roi, roi1);
		}
	}
}
 
Example 4
Source File: BiplaneAnalysisPlugIn.java    From thunderstorm with GNU General Public License v3.0 5 votes vote down vote up
private FloatProcessor getRoiProcessor(ImagePlus imp, Roi roi, int index) {
    ImageProcessor ip = imp.getStack().getProcessor(index);
    ip.setRoi(roi.getBounds());
    FloatProcessor fp = subtract(ip.crop().convertToFloatProcessor(), (float) CameraSetupPlugIn.getOffset());
    float minVal = VectorMath.min((float[]) fp.getPixels());
    if(minVal < 0) {
        IJ.log("\\Update:Camera base level is set higher than values in the image!");
        fp = add(-minVal, fp);
    }
    fp.setMask(roi.getMask());
    return fp;
}
 
Example 5
Source File: FakeImagePlus.java    From TrakEM2 with GNU General Public License v3.0 5 votes vote down vote up
public ImageStatistics getStatistics(int mOptions, int nBins, double histMin, double histMax) {
	Displayable active = display.getActive();
	if (null == active || !(active instanceof Patch)) {
		Utils.log("No patch selected.");
		return super.getStatistics(mOptions, nBins, histMin, histMax); // TODO can't return null, but something should be done about it.
	}
	ImagePlus imp = active.getProject().getLoader().fetchImagePlus((Patch)active);
	ImageProcessor ip = imp.getProcessor(); // don't create a new onw every time // ((Patch)active).getProcessor();
	Roi roi = super.getRoi();
	if (null != roi) {
		// translate ROI to be meaningful for the Patch
		int patch_x = (int)active.getX();
		int patch_y = (int)active.getY();
		Rectangle r = roi.getBounds();
		roi.setLocation(patch_x - r.x, patch_y - r.y);
	}
	ip.setRoi(roi); // even if null, to reset
	ip.setHistogramSize(nBins);
	Calibration cal = getCalibration();
	if (getType()==GRAY16&& !(histMin==0.0&&histMax==0.0))
		{histMin=cal.getRawValue(histMin); histMax=cal.getRawValue(histMax);}
	ip.setHistogramRange(histMin, histMax);
	ImageStatistics stats = ImageStatistics.getStatistics(ip, mOptions, cal);
	ip.setHistogramSize(256);
	ip.setHistogramRange(0.0, 0.0);
	return stats;
}
 
Example 6
Source File: PatchStack.java    From TrakEM2 with GNU General Public License v3.0 5 votes vote down vote up
public ImageProcessor getMask() {
	ImagePlus imp = patch[currentSlice-1].getProject().getLoader().fetchImagePlus(patch[currentSlice-1]);
	ImageProcessor ip = imp.getProcessor();
	Roi roi = getRoi();
	if (null == roi) {
		ip.resetRoi();
		return null;
	}
	ImageProcessor mask = roi.getMask();
	if (null==mask) return null;
	ip.setMask(mask);
	ip.setRoi(roi.getBounds());
	return mask;
}
 
Example 7
Source File: PatchStack.java    From TrakEM2 with GNU General Public License v3.0 5 votes vote down vote up
public void setRoi(Rectangle r) {
	if (null==r) { killRoi(); return; }
	killRoi();
	this.roi = new Roi(r.x, r.y, r.width, r.height);
	this.roi.setImage(this);
	ImagePlus imp = patch[currentSlice-1].getProject().getLoader().fetchImagePlus(patch[currentSlice-1]);
	ImageProcessor ip = imp.getProcessor();
	if (null != ip) {
		ip.setMask(null);
		ip.setRoi(r);
	}
	//draw(); // not needed
}
 
Example 8
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 9
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;
}