void addScrollbars(ImagePlus imp) { ImageStack s = imp.getStack(); int stackSize = s.getSize(); nSlices = stackSize; hyperStack = imp.getOpenAsHyperStack(); // imp.setOpenAsHyperStack(false); int[] dim = imp.getDimensions(); int nDimensions = 2 + (dim[2] > 1 ? 1 : 0) + (dim[3] > 1 ? 1 : 0) + (dim[4] > 1 ? 1 : 0); if (nDimensions <= 3 && dim[2] != nSlices) hyperStack = false; if (hyperStack) { nChannels = dim[2]; nSlices = dim[3]; nFrames = dim[4]; } // IJ.log("StackWindow: "+hyperStack+" "+nChannels+" "+nSlices+" "+nFrames); if (nSlices == stackSize) hyperStack = false; if (nChannels * nSlices * nFrames != stackSize) hyperStack = false; if (cSelector != null || zSelector != null || tSelector != null) removeScrollbars(); ImageJ ij = IJ.getInstance(); if (nChannels > 1) { cSelector = new ScrollbarWithLabel(this, 1, 1, 1, nChannels + 1, 'c'); add(cSelector); if (ij != null) cSelector.addKeyListener(ij); cSelector.addAdjustmentListener(this); cSelector.setFocusable(false); // prevents scroll bar from blinking on Windows cSelector.setUnitIncrement(1); cSelector.setBlockIncrement(1); } if (nSlices > 1) { char label = nChannels > 1 || nFrames > 1 ? 'z' : 't'; if (stackSize == dim[2] && imp.isComposite()) label = 'c'; zSelector = new ScrollbarWithLabel(this, 1, 1, 1, nSlices + 1, label); if (label == 't') animationSelector = zSelector; add(zSelector); if (ij != null) zSelector.addKeyListener(ij); zSelector.addAdjustmentListener(this); zSelector.setFocusable(false); int blockIncrement = nSlices / 10; if (blockIncrement < 1) blockIncrement = 1; zSelector.setUnitIncrement(1); zSelector.setBlockIncrement(blockIncrement); sliceSelector = zSelector.bar; } if (nFrames > 1) { animationSelector = tSelector = new ScrollbarWithLabel(this, 1, 1, 1, nFrames + 1, 't'); add(tSelector); if (ij != null) tSelector.addKeyListener(ij); tSelector.addAdjustmentListener(this); tSelector.setFocusable(false); int blockIncrement = nFrames / 10; if (blockIncrement < 1) blockIncrement = 1; tSelector.setUnitIncrement(1); tSelector.setBlockIncrement(blockIncrement); } }
/** Performs actual projection using specified method. */ public void doProjection() { if (imp == null) return; sliceCount = 0; if (method < AVG_METHOD || method > MEDIAN_METHOD) method = AVG_METHOD; for (int slice = startSlice; slice <= stopSlice; slice += increment) sliceCount++; if (method == MEDIAN_METHOD) { projImage = doMedianProjection(); return; } // Create new float processor for projected pixels. FloatProcessor fp = new FloatProcessor(imp.getWidth(), imp.getHeight()); ImageStack stack = imp.getStack(); RayFunction rayFunc = getRayFunction(method, fp); if (IJ.debugMode == true) { IJ.log("\nProjecting stack from: " + startSlice + " to: " + stopSlice); } // Determine type of input image. Explicit determination of // processor type is required for subsequent pixel // manipulation. This approach is more efficient than the // more general use of ImageProcessor's getPixelValue and // putPixel methods. int ptype; if (stack.getProcessor(1) instanceof ByteProcessor) ptype = BYTE_TYPE; else if (stack.getProcessor(1) instanceof ShortProcessor) ptype = SHORT_TYPE; else if (stack.getProcessor(1) instanceof FloatProcessor) ptype = FLOAT_TYPE; else { IJ.error("Z Project", "Non-RGB stack required"); return; } // Do the projection. for (int n = startSlice; n <= stopSlice; n += increment) { IJ.showStatus("ZProjection " + color + ": " + n + "/" + stopSlice); IJ.showProgress(n - startSlice, stopSlice - startSlice); projectSlice(stack.getPixels(n), rayFunc, ptype); } // Finish up projection. if (method == SUM_METHOD) { fp.resetMinAndMax(); projImage = new ImagePlus(makeTitle(), fp); } else if (method == SD_METHOD) { rayFunc.postProcess(); fp.resetMinAndMax(); projImage = new ImagePlus(makeTitle(), fp); } else { rayFunc.postProcess(); projImage = makeOutputImage(imp, fp, ptype); } if (projImage == null) IJ.error("Z Project", "Error computing projection."); }
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 void doHyperStackProjection(boolean allTimeFrames) { int start = startSlice; int stop = stopSlice; int firstFrame = 1; int lastFrame = imp.getNFrames(); if (!allTimeFrames) firstFrame = lastFrame = imp.getFrame(); ImageStack stack = new ImageStack(imp.getWidth(), imp.getHeight()); int channels = imp.getNChannels(); int slices = imp.getNSlices(); if (slices == 1) { slices = imp.getNFrames(); firstFrame = lastFrame = 1; } int frames = lastFrame - firstFrame + 1; increment = channels; boolean rgb = imp.getBitDepth() == 24; for (int frame = firstFrame; frame <= lastFrame; frame++) { for (int channel = 1; channel <= channels; channel++) { startSlice = (frame - 1) * channels * slices + (start - 1) * channels + channel; stopSlice = (frame - 1) * channels * slices + (stop - 1) * channels + channel; if (rgb) doHSRGBProjection(imp); else doProjection(); stack.addSlice(null, projImage.getProcessor()); } } projImage = new ImagePlus(makeTitle(), stack); projImage.setDimensions(channels, 1, frames); if (channels > 1) { projImage = new CompositeImage(projImage, 0); ((CompositeImage) projImage).copyLuts(imp); if (method == SUM_METHOD || method == SD_METHOD) ((CompositeImage) projImage).resetDisplayRanges(); } if (frames > 1) projImage.setOpenAsHyperStack(true); Overlay overlay = imp.getOverlay(); if (overlay != null) { startSlice = start; stopSlice = stop; if (imp.getType() == ImagePlus.COLOR_RGB) projImage.setOverlay(projectRGBHyperStackRois(overlay)); else projImage.setOverlay(projectHyperStackRois(overlay)); } IJ.showProgress(1, 1); }
private void doHSRGBProjection(ImagePlus rgbImp) { ImageStack stack = rgbImp.getStack(); ImageStack stack2 = new ImageStack(stack.getWidth(), stack.getHeight()); for (int i = startSlice; i <= stopSlice; i++) stack2.addSlice(null, stack.getProcessor(i)); startSlice = 1; stopSlice = stack2.getSize(); doRGBProjection(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; }
ImagePlus doMedianProjection() { IJ.showStatus("Calculating median..."); ImageStack stack = imp.getStack(); ImageProcessor[] slices = new ImageProcessor[sliceCount]; int index = 0; for (int slice = startSlice; slice <= stopSlice; slice += increment) slices[index++] = stack.getProcessor(slice); ImageProcessor ip2 = slices[0].duplicate(); ip2 = ip2.convertToFloat(); float[] values = new float[sliceCount]; int width = ip2.getWidth(); int height = ip2.getHeight(); int inc = Math.max(height / 30, 1); for (int y = 0; y < height; y++) { if (y % inc == 0) IJ.showProgress(y, height - 1); for (int x = 0; x < width; x++) { for (int i = 0; i < sliceCount; i++) values[i] = slices[i].getPixelValue(x, y); ip2.putPixelValue(x, y, median(values)); } } if (imp.getBitDepth() == 8) ip2 = ip2.convertToByte(false); IJ.showProgress(1, 1); return new ImagePlus(makeTitle(), ip2); }
ImageProcessor setup(ImagePlus imp) { ImageProcessor ip; int type = imp.getType(); if (type != ImagePlus.COLOR_RGB) return null; ip = imp.getProcessor(); int id = imp.getID(); int slice = imp.getCurrentSlice(); if ((id != previousImageID) | (slice != previousSlice) | (flag)) { flag = false; // if true, flags a change from HSB to RGB or viceversa numSlices = imp.getStackSize(); stack = imp.getStack(); width = stack.getWidth(); height = stack.getHeight(); numPixels = width * height; hSource = new byte[numPixels]; sSource = new byte[numPixels]; bSource = new byte[numPixels]; // restore = (int[])ip.getPixelsCopy(); //This runs into trouble sometimes, so do it the // long way: int[] temp = (int[]) ip.getPixels(); restore = new int[numPixels]; for (int i = 0; i < numPixels; i++) restore[i] = temp[i]; fillMask = new int[numPixels]; // Get hsb or rgb from image. ColorProcessor cp = (ColorProcessor) ip; IJ.showStatus("Gathering data"); if (isRGB) cp.getRGB(hSource, sSource, bSource); else cp.getHSB(hSource, sSource, bSource); IJ.showStatus("done"); // Create a spectrum ColorModel for the Hue histogram plot. Color c; byte[] reds = new byte[256]; byte[] greens = new byte[256]; byte[] blues = new byte[256]; for (int i = 0; i < 256; i++) { c = Color.getHSBColor(i / 255f, 1f, 1f); reds[i] = (byte) c.getRed(); greens[i] = (byte) c.getGreen(); blues[i] = (byte) c.getBlue(); } ColorModel cm = new IndexColorModel(8, 256, reds, greens, blues); // Make an image with just the hue from the RGB image and the spectrum LUT. // This is just for a hue histogram for the plot. Do not show it. // ByteProcessor bpHue = new ByteProcessor(width,height,h,cm); ByteProcessor bpHue = new ByteProcessor(width, height, hSource, cm); ImagePlus impHue = new ImagePlus("Hue", bpHue); // impHue.show(); ByteProcessor bpSat = new ByteProcessor(width, height, sSource, cm); ImagePlus impSat = new ImagePlus("Sat", bpSat); // impSat.show(); ByteProcessor bpBri = new ByteProcessor(width, height, bSource, cm); ImagePlus impBri = new ImagePlus("Bri", bpBri); // impBri.show(); plot.setHistogram(impHue, 0); splot.setHistogram(impSat, 1); bplot.setHistogram(impBri, 2); updateLabels(); updatePlot(); updateScrollBars(); imp.updateAndDraw(); } previousImageID = id; previousSlice = slice; return ip; }
/*------------------------------------------------------------------*/ public void run(String arg) { ImagePlus imp = WindowManager.getCurrentImage(); this.imp = imp; if (imp == null) { IJ.noImage(); return; } if ((1 < imp.getStackSize()) && (imp.getType() == imp.COLOR_256)) { IJ.error("Stack of color images not supported (use grayscale)"); return; } if (1 < imp.getStackSize()) { if (imp.getStack().isRGB()) { IJ.error("RGB color images not supported (use grayscale)"); return; } } if (1 < imp.getStackSize()) { if (imp.getStack().isHSB()) { IJ.error("HSB color images not supported (use grayscale)"); return; } } if (imp.getType() == imp.COLOR_256) { IJ.error("Indexed color images not supported (use grayscale)"); return; } if (imp.getType() == imp.COLOR_RGB) { IJ.error("Color images not supported (use grayscale)"); return; } differentialsDialog dialog = new differentialsDialog(IJ.getInstance(), "Differentials", true, operation); GUI.center(dialog); dialog.setVisible(true); cancel = dialog.getCancel(); operation = dialog.getOperation(); dialog.dispose(); if (cancel) { return; } imp.startTiming(); if (1 < imp.getStackSize()) { if (!(imp.getProcessor().getPixels() instanceof float[])) { new StackConverter(imp).convertToGray32(); } } else { if (!(imp.getProcessor().getPixels() instanceof float[])) { new ImageConverter(imp).convertToGray32(); } } ImageStack stack = imp.getStack(); stackSize = stack.getSize(); Undo.reset(); setupProgressBar(); resetProgressBar(); for (int i = 1; (i <= stackSize); i++) { ImageProcessor ip = stack.getProcessor(i); doIt(ip); imp.getProcessor().resetMinAndMax(); imp.setSlice(i); imp.updateAndRepaintWindow(); } imp.getProcessor().resetMinAndMax(); imp.setSlice(1); imp.updateAndRepaintWindow(); cleanUpProgressBar(); IJ.showTime(imp, imp.getStartTime(), "Differentials: "); ImageWindow win = imp.getWindow(); if (win != null) { win.running = false; } } /* end run */