public void reduceHyperstack(ImagePlus imp, int factor, boolean reduceSlices) { int channels = imp.getNChannels(); int slices = imp.getNSlices(); int frames = imp.getNFrames(); int zfactor = reduceSlices ? factor : 1; int tfactor = reduceSlices ? 1 : factor; ImageStack stack = imp.getStack(); ImageStack stack2 = new ImageStack(imp.getWidth(), imp.getHeight()); boolean virtual = stack.isVirtual(); int slices2 = slices / zfactor + ((slices % zfactor) != 0 ? 1 : 0); int frames2 = frames / tfactor + ((frames % tfactor) != 0 ? 1 : 0); int n = channels * slices2 * frames2; int count = 1; for (int t = 1; t <= frames; t += tfactor) { for (int z = 1; z <= slices; z += zfactor) { for (int c = 1; c <= channels; c++) { int i = imp.getStackIndex(c, z, t); IJ.showProgress(i, n); ImageProcessor ip = stack.getProcessor(imp.getStackIndex(c, z, t)); // IJ.log(count++ +" "+i+" "+c+" "+z+" "+t); stack2.addSlice(stack.getSliceLabel(i), ip); } } } imp.setStack(stack2, channels, slices2, frames2); Calibration cal = imp.getCalibration(); if (cal.scaled()) cal.pixelDepth *= zfactor; if (virtual) imp.setTitle(imp.getTitle()); IJ.showProgress(1.0); }
public String getImageInfo(ImagePlus imp, ImageProcessor ip) { String infoProperty = null; if (imp.getStackSize() > 1) { ImageStack stack = imp.getStack(); String label = stack.getSliceLabel(imp.getCurrentSlice()); if (label != null && label.indexOf('\n') > 0) infoProperty = label; } if (infoProperty == null) { infoProperty = (String) imp.getProperty("Info"); if (infoProperty == null) infoProperty = getExifData(imp); } String info = getInfo(imp, ip); if (infoProperty != null) return infoProperty + "\n------------------------\n" + info; else return info; }
public void reduceStack(ImagePlus imp, int factor) { ImageStack stack = imp.getStack(); boolean virtual = stack.isVirtual(); int n = stack.getSize(); ImageStack stack2 = new ImageStack(stack.getWidth(), stack.getHeight()); for (int i = 1; i <= n; i += factor) { if (virtual) IJ.showProgress(i, n); stack2.addSlice(stack.getSliceLabel(i), stack.getProcessor(i)); } imp.setStack(null, stack2); if (virtual) { IJ.showProgress(1.0); imp.setTitle(imp.getTitle()); } Calibration cal = imp.getCalibration(); if (cal.scaled()) cal.pixelDepth *= factor; }
void createNewStack(ImagePlus imp, ImageProcessor ip) { int nSlices = imp.getStackSize(); int w = imp.getWidth(), h = imp.getHeight(); ImagePlus imp2 = imp.createImagePlus(); Rectangle r = ip.getRoi(); boolean crop = r.width != imp.getWidth() || r.height != imp.getHeight(); ImageStack stack1 = imp.getStack(); ImageStack stack2 = new ImageStack(newWidth, newHeight); ImageProcessor ip1, ip2; int method = interpolationMethod; if (w == 1 || h == 1) method = ImageProcessor.NONE; for (int i = 1; i <= nSlices; i++) { IJ.showStatus("Scale: " + i + "/" + nSlices); ip1 = stack1.getProcessor(i); String label = stack1.getSliceLabel(i); if (crop) { ip1.setRoi(r); ip1 = ip1.crop(); } ip1.setInterpolationMethod(method); ip2 = ip1.resize(newWidth, newHeight, averageWhenDownsizing); if (ip2 != null) stack2.addSlice(label, ip2); IJ.showProgress(i, nSlices); } imp2.setStack(title, stack2); Calibration cal = imp2.getCalibration(); if (cal.scaled()) { cal.pixelWidth *= 1.0 / xscale; cal.pixelHeight *= 1.0 / yscale; } IJ.showProgress(1.0); int[] dim = imp.getDimensions(); imp2.setDimensions(dim[2], dim[3], dim[4]); if (imp.isComposite()) { imp2 = new CompositeImage(imp2, ((CompositeImage) imp).getMode()); ((CompositeImage) imp2).copyLuts(imp); } if (imp.isHyperStack()) imp2.setOpenAsHyperStack(true); if (newDepth > 0 && newDepth != oldDepth) imp2 = (new Resizer()).zScale(imp2, newDepth, interpolationMethod); if (imp2 != null) { imp2.show(); imp2.changes = true; } }
public ImageStack expandStack(ImageStack stackOld, int wNew, int hNew, int xOff, int yOff) { int nFrames = stackOld.getSize(); ImageProcessor ipOld = stackOld.getProcessor(1); java.awt.Color colorBack = Toolbar.getBackgroundColor(); ImageStack stackNew = new ImageStack(wNew, hNew, stackOld.getColorModel()); ImageProcessor ipNew; for (int i = 1; i <= nFrames; i++) { IJ.showProgress((double) i / nFrames); ipNew = ipOld.createProcessor(wNew, hNew); if (zeroFill) ipNew.setValue(0.0); else ipNew.setColor(colorBack); ipNew.fill(); ipNew.insert(stackOld.getProcessor(i), xOff, yOff); stackNew.addSlice(stackOld.getSliceLabel(i), ipNew); } return stackNew; }
// extract range of slices ImagePlus stackRange(ImagePlus imp, int first, int last, int inc, String title) throws Exception { ImageStack stack = imp.getStack(); ImageStack stack2 = null; Roi roi = imp.getRoi(); for (int i = first, j = 0; i <= last; i += inc) { // IJ.log(first+" "+last+" "+inc+" "+i); IJ.showProgress(i - first, last - first); int currSlice = i - j; ImageProcessor ip2 = stack.getProcessor(currSlice); // ip2.setRoi(roi); // ip2 = ip2.crop(); if (stack2 == null) stack2 = new ImageStack(ip2.getWidth(), ip2.getHeight()); stack2.addSlice(stack.getSliceLabel(currSlice), ip2); } ImagePlus substack = imp.createImagePlus(); substack.setStack(title, stack2); substack.setCalibration(imp.getCalibration()); return substack; }
ImagePlus duplicateStack(ImagePlus img1) { ImageStack stack1 = img1.getStack(); int width = stack1.getWidth(); int height = stack1.getHeight(); int n = stack1.getSize(); ImageStack stack2 = img1.createEmptyStack(); try { for (int i = 1; i <= n; i++) { ImageProcessor ip1 = stack1.getProcessor(i); ip1.resetRoi(); ImageProcessor ip2 = ip1.crop(); stack2.addSlice(stack1.getSliceLabel(i), ip2); } } catch (OutOfMemoryError e) { stack2.trim(); stack2 = null; return null; } return new ImagePlus("Duplicate", stack2); }
void createNewStack(ImagePlus imp, ImageProcessor ip) { Rectangle r = ip.getRoi(); boolean crop = r.width != imp.getWidth() || r.height != imp.getHeight(); int nSlices = imp.getStackSize(); ImageStack stack1 = imp.getStack(); ImageStack stack2 = new ImageStack(newWidth, newHeight); ImageProcessor ip1, ip2; boolean interp = interpolate; if (imp.getWidth() == 1 || imp.getHeight() == 1) interp = false; for (int i = 1; i <= nSlices; i++) { IJ.showStatus("Scale: " + i + "/" + nSlices); ip1 = stack1.getProcessor(i); String label = stack1.getSliceLabel(i); if (crop) { ip1.setRoi(r); ip1 = ip1.crop(); } ip1.setInterpolate(interp); ip2 = ip1.resize(newWidth, newHeight); if (ip2 != null) stack2.addSlice(label, ip2); IJ.showProgress(i, nSlices); } ImagePlus imp2 = imp.createImagePlus(); imp2.setStack(title, stack2); Calibration cal = imp2.getCalibration(); if (cal.scaled()) { cal.pixelWidth *= 1.0 / xscale; cal.pixelHeight *= 1.0 / yscale; } int[] dim = imp.getDimensions(); imp2.setDimensions(dim[2], dim[3], dim[4]); IJ.showProgress(1.0); if (imp.isComposite()) { imp2 = new CompositeImage(imp2, 0); ((CompositeImage) imp2).copyLuts(imp); } if (imp.isHyperStack()) imp2.setOpenAsHyperStack(true); imp2.show(); imp2.changes = true; }