Example #1
0
 void scale(ImageProcessor ip) {
   if (newWindow) {
     Rectangle r = ip.getRoi();
     ImagePlus imp2 = imp.createImagePlus();
     imp2.setProcessor(title, ip.resize(newWidth, newHeight));
     Calibration cal = imp2.getCalibration();
     if (cal.scaled()) {
       cal.pixelWidth *= 1.0 / xscale;
       cal.pixelHeight *= 1.0 / yscale;
     }
     imp2.show();
     imp.trimProcessor();
     imp2.trimProcessor();
     imp2.changes = true;
   } else {
     if (processStack && imp.getStackSize() > 1) {
       Undo.reset();
       StackProcessor sp = new StackProcessor(imp.getStack(), ip);
       sp.scale(xscale, yscale, bgValue);
     } else {
       ip.snapshot();
       Undo.setup(Undo.FILTER, imp);
       ip.setSnapshotCopyMode(true);
       ip.scale(xscale, yscale);
       ip.setSnapshotCopyMode(false);
     }
     imp.killRoi();
     imp.updateAndDraw();
     imp.changes = true;
   }
 }
Example #2
0
 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;
   }
 }
Example #3
0
 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;
 }
  /**
   * Performs particle analysis on the specified ImagePlus and ImageProcessor. Returns false if
   * there is an error.
   */
  public boolean analyze(ImagePlus imp, ImageProcessor ip) {
    if (this.imp == null) this.imp = imp;
    showResults = (options & SHOW_RESULTS) != 0;
    excludeEdgeParticles = (options & EXCLUDE_EDGE_PARTICLES) != 0;
    resetCounter = (options & CLEAR_WORKSHEET) != 0;
    showProgress = (options & SHOW_PROGRESS) != 0;
    floodFill = (options & INCLUDE_HOLES) == 0;
    recordStarts = (options & RECORD_STARTS) != 0;
    addToManager = (options & ADD_TO_MANAGER) != 0;
    displaySummary = (options & DISPLAY_SUMMARY) != 0;
    inSituShow = (options & IN_SITU_SHOW) != 0;
    outputImage = null;
    ip.snapshot();
    ip.setProgressBar(null);
    if (Analyzer.isRedirectImage()) {
      redirectImp = Analyzer.getRedirectImage(imp);
      if (redirectImp == null) return false;
      int depth = redirectImp.getStackSize();
      if (depth > 1 && depth == imp.getStackSize()) {
        ImageStack redirectStack = redirectImp.getStack();
        redirectIP = redirectStack.getProcessor(imp.getCurrentSlice());
      } else redirectIP = redirectImp.getProcessor();
    } else if (imp.getType() == ImagePlus.COLOR_RGB) {
      ImagePlus original = (ImagePlus) imp.getProperty("OriginalImage");
      if (original != null
          && original.getWidth() == imp.getWidth()
          && original.getHeight() == imp.getHeight()) {
        redirectImp = original;
        redirectIP = original.getProcessor();
      }
    }
    if (!setThresholdLevels(imp, ip)) return false;
    width = ip.getWidth();
    height = ip.getHeight();
    if (!(showChoice == NOTHING || showChoice == OVERLAY_OUTLINES || showChoice == OVERLAY_MASKS)) {
      blackBackground = Prefs.blackBackground && inSituShow;
      if (slice == 1) outlines = new ImageStack(width, height);
      if (showChoice == ROI_MASKS) drawIP = new ShortProcessor(width, height);
      else drawIP = new ByteProcessor(width, height);
      drawIP.setLineWidth(lineWidth);
      if (showChoice == ROI_MASKS) {
      } // Place holder for now...
      else if (showChoice == MASKS && !blackBackground) drawIP.invertLut();
      else if (showChoice == OUTLINES) {
        if (!inSituShow) {
          if (customLut == null) makeCustomLut();
          drawIP.setColorModel(customLut);
        }
        drawIP.setFont(new Font("SansSerif", Font.PLAIN, fontSize));
        if (fontSize > 12 && inSituShow) drawIP.setAntialiasedText(true);
      }
      outlines.addSlice(null, drawIP);

      if (showChoice == ROI_MASKS || blackBackground) {
        drawIP.setColor(Color.black);
        drawIP.fill();
        drawIP.setColor(Color.white);
      } else {
        drawIP.setColor(Color.white);
        drawIP.fill();
        drawIP.setColor(Color.black);
      }
    }
    calibration = redirectImp != null ? redirectImp.getCalibration() : imp.getCalibration();

    if (rt == null) {
      rt = Analyzer.getResultsTable();
      analyzer = new Analyzer(imp);
    } else analyzer = new Analyzer(imp, measurements, rt);
    if (resetCounter && slice == 1) {
      if (!Analyzer.resetCounter()) return false;
    }
    beginningCount = Analyzer.getCounter();

    byte[] pixels = null;
    if (ip instanceof ByteProcessor) pixels = (byte[]) ip.getPixels();
    if (r == null) {
      r = ip.getRoi();
      mask = ip.getMask();
      if (displaySummary) {
        if (mask != null) totalArea = ImageStatistics.getStatistics(ip, AREA, calibration).area;
        else totalArea = r.width * calibration.pixelWidth * r.height * calibration.pixelHeight;
      }
    }
    minX = r.x;
    maxX = r.x + r.width;
    minY = r.y;
    maxY = r.y + r.height;
    if (r.width < width || r.height < height || mask != null) {
      if (!eraseOutsideRoi(ip, r, mask)) return false;
    }
    int offset;
    double value;
    int inc = Math.max(r.height / 25, 1);
    int mi = 0;
    ImageWindow win = imp.getWindow();
    if (win != null) win.running = true;
    if (measurements == 0) measurements = Analyzer.getMeasurements();
    if (showChoice == ELLIPSES) measurements |= ELLIPSE;
    measurements &= ~LIMIT; // ignore "Limit to Threshold"
    roiNeedsImage =
        (measurements & PERIMETER) != 0
            || (measurements & SHAPE_DESCRIPTORS) != 0
            || (measurements & FERET) != 0;
    particleCount = 0;
    wand = new Wand(ip);
    pf = new PolygonFiller();
    if (floodFill) {
      ImageProcessor ipf = ip.duplicate();
      ipf.setValue(fillColor);
      ff = new FloodFiller(ipf);
    }
    roiType = Wand.allPoints() ? Roi.FREEROI : Roi.TRACED_ROI;

    for (int y = r.y; y < (r.y + r.height); y++) {
      offset = y * width;
      for (int x = r.x; x < (r.x + r.width); x++) {
        if (pixels != null) value = pixels[offset + x] & 255;
        else if (imageType == SHORT) value = ip.getPixel(x, y);
        else value = ip.getPixelValue(x, y);
        if (value >= level1 && value <= level2) analyzeParticle(x, y, imp, ip);
      }
      if (showProgress && ((y % inc) == 0)) IJ.showProgress((double) (y - r.y) / r.height);
      if (win != null) canceled = !win.running;
      if (canceled) {
        Macro.abort();
        break;
      }
    }
    if (showProgress) IJ.showProgress(1.0);
    if (showResults) rt.updateResults();
    imp.killRoi();
    ip.resetRoi();
    ip.reset();
    if (displaySummary && IJ.getInstance() != null) updateSliceSummary();
    if (addToManager && roiManager != null) roiManager.setEditMode(imp, true);
    maxParticleCount = (particleCount > maxParticleCount) ? particleCount : maxParticleCount;
    totalCount += particleCount;
    if (!canceled) showResults();
    return true;
  }
Example #5
0
  boolean showDialog(ImageProcessor ip) {
    int bitDepth = imp.getBitDepth();
    boolean isStack = imp.getStackSize() > 1;
    r = ip.getRoi();
    int width = newWidth;
    if (width == 0) width = r.width;
    int height = (int) ((double) width * r.height / r.width);
    xscale = Tools.parseDouble(xstr, 0.0);
    yscale = Tools.parseDouble(ystr, 0.0);
    if (xscale != 0.0 && yscale != 0.0) {
      width = (int) (r.width * xscale);
      height = (int) (r.height * yscale);
    } else {
      xstr = "-";
      ystr = "-";
    }
    GenericDialog gd = new GenericDialog("Scale");
    gd.addStringField("X Scale (0.05-25):", xstr);
    gd.addStringField("Y Scale (0.05-25):", ystr);
    gd.setInsets(5, 0, 5);
    gd.addStringField("Width (pixels):", "" + width);
    gd.addStringField("Height (pixels):", "" + height);
    fields = gd.getStringFields();
    for (int i = 0; i < 3; i++) {
      ((TextField) fields.elementAt(i)).addTextListener(this);
      ((TextField) fields.elementAt(i)).addFocusListener(this);
    }
    xField = (TextField) fields.elementAt(0);
    yField = (TextField) fields.elementAt(1);
    widthField = (TextField) fields.elementAt(2);
    heightField = (TextField) fields.elementAt(3);
    fieldWithFocus = xField;
    gd.addCheckbox("Interpolate", interpolate);
    if (bitDepth == 8 || bitDepth == 24)
      gd.addCheckbox("Fill with Background Color", fillWithBackground);
    if (isStack) gd.addCheckbox("Process Entire Stack", processStack);
    gd.addCheckbox("Create New Window", newWindow);
    title = WindowManager.getUniqueName(imp.getTitle());
    gd.setInsets(10, 0, 0);
    gd.addStringField("Title:", title, 12);
    gd.showDialog();
    if (gd.wasCanceled()) return false;
    xstr = gd.getNextString();
    ystr = gd.getNextString();
    xscale = Tools.parseDouble(xstr, 0.0);
    yscale = Tools.parseDouble(ystr, 0.0);
    String wstr = gd.getNextString();
    newWidth = (int) Tools.parseDouble(wstr, 0);
    newHeight = (int) Tools.parseDouble(gd.getNextString(), 0);
    if (newHeight != 0 && (wstr.equals("-") || wstr.equals("0")))
      newWidth = (int) (newHeight * (double) r.width / r.height);
    if (newWidth == 0 || newHeight == 0) {
      IJ.error("Invalid width or height entered");
      return false;
    }
    if (xscale > 25.0) xscale = 25.0;
    if (yscale > 25.0) yscale = 25.0;
    if (xscale > 0.0 && yscale > 0.0) {
      newWidth = (int) (r.width * xscale);
      newHeight = (int) (r.height * yscale);
    }
    interpolate = gd.getNextBoolean();
    if (bitDepth == 8 || bitDepth == 24) fillWithBackground = gd.getNextBoolean();
    if (isStack) processStack = gd.getNextBoolean();
    newWindow = gd.getNextBoolean();
    if (!newWindow && xscale == 0.0) {
      xscale = (double) newWidth / r.width;
      yscale = (double) newHeight / r.height;
    }
    title = gd.getNextString();

    if (fillWithBackground) {
      Color bgc = Toolbar.getBackgroundColor();
      if (bitDepth == 8) bgValue = ip.getBestIndex(bgc);
      else if (bitDepth == 24) bgValue = bgc.getRGB();
    } else {
      if (bitDepth == 8) bgValue = ip.isInvertedLut() ? 0.0 : 255.0; // white
      else if (bitDepth == 24) bgValue = 0xffffffff; // white
    }
    return true;
  }
Example #6
0
  boolean showDialog(ImageProcessor ip) {
    String macroOptions = Macro.getOptions();
    if (macroOptions != null) {
      if (macroOptions.indexOf(" interpolate") != -1)
        macroOptions.replaceAll(" interpolate", " interpolation=Bilinear");
      else if (macroOptions.indexOf(" interpolation=") == -1)
        macroOptions = macroOptions + " interpolation=None";
      Macro.setOptions(macroOptions);
    }
    int bitDepth = imp.getBitDepth();
    int stackSize = imp.getStackSize();
    boolean isStack = stackSize > 1;
    oldDepth = stackSize;
    if (isStack) {
      xstr = "1.0";
      ystr = "1.0";
      zstr = "1.0";
    }
    r = ip.getRoi();
    int width = newWidth;
    if (width == 0) width = r.width;
    int height = (int) ((double) width * r.height / r.width);
    xscale = Tools.parseDouble(xstr, 0.0);
    yscale = Tools.parseDouble(ystr, 0.0);
    zscale = 1.0;
    if (xscale != 0.0 && yscale != 0.0) {
      width = (int) (r.width * xscale);
      height = (int) (r.height * yscale);
    } else {
      xstr = "-";
      ystr = "-";
    }
    GenericDialog gd = new GenericDialog("Scale");
    gd.addStringField("X Scale:", xstr);
    gd.addStringField("Y Scale:", ystr);
    if (isStack) gd.addStringField("Z Scale:", zstr);
    gd.setInsets(5, 0, 5);
    gd.addStringField("Width (pixels):", "" + width);
    gd.addStringField("Height (pixels):", "" + height);
    if (isStack) {
      String label = "Depth (images):";
      if (imp.isHyperStack()) {
        int slices = imp.getNSlices();
        int frames = imp.getNFrames();
        if (slices == 1 && frames > 1) {
          label = "Depth (frames):";
          oldDepth = frames;
        } else {
          label = "Depth (slices):";
          oldDepth = slices;
        }
      }
      gd.addStringField(label, "" + oldDepth);
    }
    fields = gd.getStringFields();
    for (int i = 0; i < fields.size(); i++) {
      ((TextField) fields.elementAt(i)).addTextListener(this);
      ((TextField) fields.elementAt(i)).addFocusListener(this);
    }
    xField = (TextField) fields.elementAt(0);
    yField = (TextField) fields.elementAt(1);
    if (isStack) {
      zField = (TextField) fields.elementAt(2);
      widthField = (TextField) fields.elementAt(3);
      heightField = (TextField) fields.elementAt(4);
      depthField = (TextField) fields.elementAt(5);
    } else {
      widthField = (TextField) fields.elementAt(2);
      heightField = (TextField) fields.elementAt(3);
    }
    fieldWithFocus = xField;
    gd.addChoice("Interpolation:", methods, methods[interpolationMethod]);
    if (bitDepth == 8 || bitDepth == 24)
      gd.addCheckbox("Fill with background color", fillWithBackground);
    gd.addCheckbox("Average when downsizing", averageWhenDownsizing);
    boolean hyperstack = imp.isHyperStack() || imp.isComposite();
    if (isStack && !hyperstack) gd.addCheckbox("Process entire stack", processStack);
    gd.addCheckbox("Create new window", newWindow);
    title = WindowManager.getUniqueName(imp.getTitle());
    gd.setInsets(10, 0, 0);
    gd.addStringField("Title:", title, 12);
    gd.showDialog();
    if (gd.wasCanceled()) return false;
    xstr = gd.getNextString();
    ystr = gd.getNextString();
    xscale = Tools.parseDouble(xstr, 0.0);
    yscale = Tools.parseDouble(ystr, 0.0);
    if (isStack) {
      zstr = gd.getNextString();
      zscale = Tools.parseDouble(ystr, 0.0);
    }
    String wstr = gd.getNextString();
    newWidth = (int) Tools.parseDouble(wstr, 0);
    newHeight = (int) Tools.parseDouble(gd.getNextString(), 0);
    if (newHeight != 0 && (wstr.equals("-") || wstr.equals("0")))
      newWidth = (int) (newHeight * (double) r.width / r.height);
    if (newWidth == 0 || newHeight == 0) {
      IJ.error("Scaler", "Width or height is 0");
      return false;
    }
    if (xscale > 0.0 && yscale > 0.0) {
      newWidth = (int) (r.width * xscale);
      newHeight = (int) (r.height * yscale);
    }
    if (isStack) newDepth = (int) Tools.parseDouble(gd.getNextString(), 0);
    interpolationMethod = gd.getNextChoiceIndex();
    if (bitDepth == 8 || bitDepth == 24) fillWithBackground = gd.getNextBoolean();
    averageWhenDownsizing = gd.getNextBoolean();
    if (isStack && !hyperstack) processStack = gd.getNextBoolean();
    if (hyperstack) processStack = true;
    newWindow = gd.getNextBoolean();
    if (xscale == 0.0) {
      xscale = (double) newWidth / r.width;
      yscale = (double) newHeight / r.height;
    }
    title = gd.getNextString();

    if (fillWithBackground) {
      Color bgc = Toolbar.getBackgroundColor();
      if (bitDepth == 8) bgValue = ip.getBestIndex(bgc);
      else if (bitDepth == 24) bgValue = bgc.getRGB();
    } else bgValue = 0.0;
    return true;
  }
    void sample() {
      byte[] hsSource, ssSource, bsSource;
      // ImageProcessor ip2;
      // ip2 = imp.getProcessor();
      Rectangle myroi = ip.getRoi();
      int swidth = myroi.width;
      int sheight = myroi.height;
      int sy = myroi.y;
      int sx = myroi.x;
      if (swidth == width && sheight == height) {
        IJ.showMessage("Select a rectangular ROI");
        IJ.beep();
        return;
      }

      IJ.run("Select None");

      int snumPixels = swidth * sheight;

      hsSource = new byte[snumPixels];
      ssSource = new byte[snumPixels];
      bsSource = new byte[snumPixels];

      int[] pixs = new int[snumPixels];
      int[] bin = new int[256];

      int counter = 0, pi = 0, rangePassH = 0, rangeStopH = 0, rangePassL = 0, rangeStopL = 0, i, j;

      for (i = sy; i < sy + sheight; i++) {
        for (j = sx; j < sx + swidth; j++) {
          pixs[counter++] = ip.getPixel(j, i);
        }
      }

      // Get hsb or rgb from roi.
      ColorProcessor cp2 = new ColorProcessor(swidth, sheight, pixs);

      int iminhue = 256, imaxhue = -1, iminsat = 256, imaxsat = -1, iminbri = 256, imaxbri = -1;
      int iminred = 256, imaxred = -1, imingre = 256, imaxgre = -1, iminblu = 256, imaxblu = -1;

      if (isRGB) cp2.getRGB(hsSource, ssSource, bsSource);
      else cp2.getHSB(hsSource, ssSource, bsSource);

      for (i = 0; i < snumPixels; i++) {
        bin[hsSource[i] & 255] = 1;
        if ((hsSource[i] & 255) > imaxhue) imaxhue = (hsSource[i] & 255);
        if ((hsSource[i] & 255) < iminhue) iminhue = (hsSource[i] & 255);
        if ((ssSource[i] & 255) > imaxsat) imaxsat = (ssSource[i] & 255);
        if ((ssSource[i] & 255) < iminsat) iminsat = (ssSource[i] & 255);
        if ((bsSource[i] & 255) > imaxbri) imaxbri = (bsSource[i] & 255);
        if ((bsSource[i] & 255) < iminbri) iminbri = (bsSource[i] & 255);
        // IJ.showMessage("h:"+minhue+"H:"+maxhue+"s:"+minsat+"S:"+maxsat+"b:"+minbri+"B:"+maxbri);
      }

      if (!isRGB) { // get pass or stop filter whichever has a narrower range
        for (i = 0; i < 256; i++) {
          if (bin[i] > 0) {
            rangePassL = i;
            break;
          }
        }
        for (i = 255; i >= 0; i--) {
          if (bin[i] > 0) {
            rangePassH = i;
            break;
          }
        }
        for (i = 0; i < 256; i++) {
          if (bin[i] == 0) {
            rangeStopL = i;
            break;
          }
        }
        for (i = 255; i >= 0; i--) {
          if (bin[i] == 0) {
            rangeStopH = i;
            break;
          }
        }
        if ((rangePassH - rangePassL) < (rangeStopH - rangeStopL)) {
          bandPassH.setState(true);
          bandStopH.setState(false);
          iminhue = rangePassL;
          imaxhue = rangePassH;
        } else {
          bandPassH.setState(false);
          bandStopH.setState(true);
          iminhue = rangeStopL;
          imaxhue = rangeStopH;
        }
      } else {
        bandPassH.setState(true);
        bandStopH.setState(false);
      }

      adjustMinHue(iminhue);
      minSlider.setValue(iminhue);
      adjustMaxHue(imaxhue);
      maxSlider.setValue(imaxhue);
      adjustMinSat(iminsat);
      minSlider2.setValue(iminsat);
      adjustMaxSat(imaxsat);
      maxSlider2.setValue(imaxsat);
      adjustMinBri(iminbri);
      minSlider3.setValue(iminbri);
      adjustMaxBri(imaxbri);
      maxSlider3.setValue(imaxbri);
      originalB.setEnabled(true);
      // IJ.showStatus("done");
    }