Ejemplo n.º 1
0
 void createEllipse(ImagePlus imp) {
   IJ.showStatus("Fitting ellipse");
   Roi roi = imp.getRoi();
   if (roi == null) {
     noRoi("Fit Ellipse");
     return;
   }
   if (roi.isLine()) {
     IJ.error("Fit Ellipse", "\"Fit Ellipse\" does not work with line selections");
     return;
   }
   ImageProcessor ip = imp.getProcessor();
   ip.setRoi(roi);
   int options = Measurements.CENTROID + Measurements.ELLIPSE;
   ImageStatistics stats = ImageStatistics.getStatistics(ip, options, null);
   double dx = stats.major * Math.cos(stats.angle / 180.0 * Math.PI) / 2.0;
   double dy = -stats.major * Math.sin(stats.angle / 180.0 * Math.PI) / 2.0;
   double x1 = stats.xCentroid - dx;
   double x2 = stats.xCentroid + dx;
   double y1 = stats.yCentroid - dy;
   double y2 = stats.yCentroid + dy;
   double aspectRatio = stats.minor / stats.major;
   imp.killRoi();
   imp.setRoi(new EllipseRoi(x1, y1, x2, y2, aspectRatio));
 }
Ejemplo n.º 2
0
 void lineToArea(ImagePlus imp) {
   Roi roi = imp.getRoi();
   if (roi == null || !roi.isLine()) {
     IJ.error("Line to Area", "Line selection required");
     return;
   }
   Undo.setup(Undo.ROI, imp);
   Roi roi2 = null;
   if (roi.getType() == Roi.LINE) {
     double width = roi.getStrokeWidth();
     if (width <= 1.0) roi.setStrokeWidth(1.0000001);
     FloatPolygon p = roi.getFloatPolygon();
     roi.setStrokeWidth(width);
     roi2 = new PolygonRoi(p, Roi.POLYGON);
     roi2.setDrawOffset(roi.getDrawOffset());
   } else {
     ImageProcessor ip2 = new ByteProcessor(imp.getWidth(), imp.getHeight());
     ip2.setColor(255);
     roi.drawPixels(ip2);
     // new ImagePlus("ip2", ip2.duplicate()).show();
     ip2.setThreshold(255, 255, ImageProcessor.NO_LUT_UPDATE);
     ThresholdToSelection tts = new ThresholdToSelection();
     roi2 = tts.convert(ip2);
   }
   transferProperties(roi, roi2);
   roi2.setStrokeWidth(0);
   Color c = roi2.getStrokeColor();
   if (c != null) // remove any transparency
   roi2.setStrokeColor(new Color(c.getRed(), c.getGreen(), c.getBlue()));
   imp.setRoi(roi2);
   Roi.previousRoi = (Roi) roi.clone();
 }
Ejemplo n.º 3
0
 void interpolate() {
   Roi roi = imp.getRoi();
   if (roi == null) {
     noRoi("Interpolate");
     return;
   }
   if (roi.getType() == Roi.POINT) return;
   if (IJ.isMacro() && Macro.getOptions() == null) Macro.setOptions("interval=1");
   GenericDialog gd = new GenericDialog("Interpolate");
   gd.addNumericField("Interval:", 1.0, 1, 4, "pixel");
   gd.addCheckbox("Smooth", IJ.isMacro() ? false : smooth);
   gd.showDialog();
   if (gd.wasCanceled()) return;
   double interval = gd.getNextNumber();
   smooth = gd.getNextBoolean();
   Undo.setup(Undo.ROI, imp);
   FloatPolygon poly = roi.getInterpolatedPolygon(interval, smooth);
   int t = roi.getType();
   int type = roi.isLine() ? Roi.FREELINE : Roi.FREEROI;
   if (t == Roi.POLYGON && interval > 1.0) type = Roi.POLYGON;
   if ((t == Roi.RECTANGLE || t == Roi.OVAL || t == Roi.FREEROI) && interval >= 5.0)
     type = Roi.POLYGON;
   if ((t == Roi.LINE || t == Roi.FREELINE) && interval >= 5.0) type = Roi.POLYLINE;
   if (t == Roi.POLYLINE && interval >= 1.0) type = Roi.POLYLINE;
   ImageCanvas ic = imp.getCanvas();
   if (poly.npoints <= 150 && ic != null && ic.getMagnification() >= 12.0)
     type = roi.isLine() ? Roi.POLYLINE : Roi.POLYGON;
   Roi p = new PolygonRoi(poly, type);
   if (roi.getStroke() != null) p.setStrokeWidth(roi.getStrokeWidth());
   p.setStrokeColor(roi.getStrokeColor());
   p.setName(roi.getName());
   transferProperties(roi, p);
   imp.setRoi(p);
 }
Ejemplo n.º 4
0
  /** Generate output image whose type is same as input image. */
  private ImagePlus makeOutputImage(ImagePlus imp, FloatProcessor fp, int ptype) {
    int width = imp.getWidth();
    int height = imp.getHeight();
    float[] pixels = (float[]) fp.getPixels();
    ImageProcessor oip = null;

    // Create output image consistent w/ type of input image.
    int size = pixels.length;
    switch (ptype) {
      case BYTE_TYPE:
        oip = imp.getProcessor().createProcessor(width, height);
        byte[] pixels8 = (byte[]) oip.getPixels();
        for (int i = 0; i < size; i++) pixels8[i] = (byte) pixels[i];
        break;
      case SHORT_TYPE:
        oip = imp.getProcessor().createProcessor(width, height);
        short[] pixels16 = (short[]) oip.getPixels();
        for (int i = 0; i < size; i++) pixels16[i] = (short) pixels[i];
        break;
      case FLOAT_TYPE:
        oip = new FloatProcessor(width, height, pixels, null);
        break;
    }

    // Adjust for display.
    // Calling this on non-ByteProcessors ensures image
    // processor is set up to correctly display image.
    oip.resetMinAndMax();

    // Create new image plus object. Don't use
    // ImagePlus.createImagePlus here because there may be
    // attributes of input image that are not appropriate for
    // projection.
    return new ImagePlus(makeTitle(), oip);
  }
Ejemplo n.º 5
0
 // Added by Marcel Boeglin 2013.09.22
 private Overlay projectHyperStackRois(Overlay overlay) {
   if (overlay == null) return null;
   int t1 = imp.getFrame();
   int channels = projImage.getNChannels();
   int slices = 1;
   int frames = projImage.getNFrames();
   Overlay overlay2 = new Overlay();
   Roi roi;
   int c, z, t;
   int size = channels * slices * frames;
   for (Roi r : overlay.toArray()) {
     c = r.getCPosition();
     z = r.getZPosition();
     t = r.getTPosition();
     roi = (Roi) r.clone();
     if (size == channels) { // current time frame
       if (z >= startSlice && z <= stopSlice && t == t1 || c == 0) {
         roi.setPosition(c);
         overlay2.add(roi);
       }
     } else if (size == frames * channels) { // all time frames
       if (z >= startSlice && z <= stopSlice) roi.setPosition(c, 1, t);
       else if (z == 0) roi.setPosition(c, 0, t);
       else continue;
       overlay2.add(roi);
     }
   }
   return overlay2;
 }
Ejemplo n.º 6
0
 void fitSpline() {
   Roi roi = imp.getRoi();
   if (roi == null) {
     noRoi("Spline");
     return;
   }
   int type = roi.getType();
   boolean segmentedSelection = type == Roi.POLYGON || type == Roi.POLYLINE;
   if (!(segmentedSelection
       || type == Roi.FREEROI
       || type == Roi.TRACED_ROI
       || type == Roi.FREELINE)) {
     IJ.error("Spline", "Polygon or polyline selection required");
     return;
   }
   if (roi instanceof EllipseRoi) return;
   PolygonRoi p = (PolygonRoi) roi;
   if (!segmentedSelection) {
     if (p.subPixelResolution()) p = trimFloatPolygon(p, p.getUncalibratedLength());
     else p = trimPolygon(p, p.getUncalibratedLength());
   }
   String options = Macro.getOptions();
   if (options != null && options.indexOf("straighten") != -1) p.fitSplineForStraightening();
   else if (options != null && options.indexOf("remove") != -1) p.removeSplineFit();
   else p.fitSpline();
   imp.draw();
   LineWidthAdjuster.update();
 }
Ejemplo n.º 7
0
 private ImagePlus duplicateImage(ImageProcessor iProcessor) {
   int w = iProcessor.getWidth();
   int h = iProcessor.getHeight();
   ImagePlus iPlus = NewImage.createByteImage("Image", w, h, 1, NewImage.FILL_BLACK);
   ImageProcessor imageProcessor = iPlus.getProcessor();
   imageProcessor.copyBits(iProcessor, 0, 0, Blitter.COPY);
   return iPlus;
 }
Ejemplo n.º 8
0
 /**
  * Builds dialog to query users for projection parameters.
  *
  * @param start starting slice to display
  * @param stop last slice
  */
 protected GenericDialog buildControlDialog(int start, int stop) {
   GenericDialog gd = new GenericDialog("ZProjection", IJ.getInstance());
   gd.addNumericField("Start slice:", startSlice, 0 /*digits*/);
   gd.addNumericField("Stop slice:", stopSlice, 0 /*digits*/);
   gd.addChoice("Projection type", METHODS, METHODS[method]);
   if (isHyperstack && imp.getNFrames() > 1 && imp.getNSlices() > 1)
     gd.addCheckbox("All time frames", allTimeFrames);
   return gd;
 }
Ejemplo n.º 9
0
  void Bernsen(ImagePlus imp, int radius, double par1, double par2, boolean doIwhite) {
    // Bernsen recommends WIN_SIZE = 31 and CONTRAST_THRESHOLD = 15.
    //  1) Bernsen J. (1986) "Dynamic Thresholding of Grey-Level Images"
    //    Proc. of the 8th Int. Conf. on Pattern Recognition, pp. 1251-1255
    //  2) Sezgin M. and Sankur B. (2004) "Survey over Image Thresholding
    //   Techniques and Quantitative Performance Evaluation" Journal of
    //   Electronic Imaging, 13(1): 146-165
    //  http://citeseer.ist.psu.edu/sezgin04survey.html
    // Ported to ImageJ plugin from E Celebi's fourier_0.8 routines
    // This version uses a circular local window, instead of a rectagular one
    ImagePlus Maximp, Minimp;
    ImageProcessor ip = imp.getProcessor(), ipMax, ipMin;
    int contrast_threshold = 15;
    int local_contrast;
    int mid_gray;
    byte object;
    byte backg;
    int temp;

    if (par1 != 0) {
      IJ.log("Bernsen: changed contrast_threshold from :" + contrast_threshold + "  to:" + par1);
      contrast_threshold = (int) par1;
    }

    if (doIwhite) {
      object = (byte) 0xff;
      backg = (byte) 0;
    } else {
      object = (byte) 0;
      backg = (byte) 0xff;
    }

    Maximp = duplicateImage(ip);
    ipMax = Maximp.getProcessor();
    RankFilters rf = new RankFilters();
    rf.rank(ipMax, radius, rf.MAX); // Maximum
    // Maximp.show();
    Minimp = duplicateImage(ip);
    ipMin = Minimp.getProcessor();
    rf.rank(ipMin, radius, rf.MIN); // Minimum
    // Minimp.show();
    byte[] pixels = (byte[]) ip.getPixels();
    byte[] max = (byte[]) ipMax.getPixels();
    byte[] min = (byte[]) ipMin.getPixels();

    for (int i = 0; i < pixels.length; i++) {
      local_contrast = (int) ((max[i] & 0xff) - (min[i] & 0xff));
      mid_gray = (int) ((min[i] & 0xff) + (max[i] & 0xff)) / 2;
      temp = (int) (pixels[i] & 0x0000ff);
      if (local_contrast < contrast_threshold)
        pixels[i] = (mid_gray >= 128) ? object : backg; // Low contrast region
      else pixels[i] = (temp >= mid_gray) ? object : backg;
    }
    // imp.updateAndDraw();
    return;
  }
Ejemplo n.º 10
0
 void toBoundingBox(ImagePlus imp) {
   Roi roi = imp.getRoi();
   if (roi == null) {
     noRoi("To Bounding Box");
     return;
   }
   Rectangle r = roi.getBounds();
   imp.killRoi();
   imp.setRoi(new Roi(r.x, r.y, r.width, r.height));
 }
Ejemplo n.º 11
0
  /** 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.");
  }
Ejemplo n.º 12
0
 private boolean ignoreArrowKeys(ImagePlus imp) {
   Frame frame = WindowManager.getFrontWindow();
   String title = frame.getTitle();
   if (title != null && title.equals("ROI Manager")) return true;
   // Control Panel?
   if (frame != null && frame instanceof javax.swing.JFrame) return true;
   ImageWindow win = imp.getWindow();
   // LOCI Data Browser window?
   if (imp.getStackSize() > 1 && win != null && win.getClass().getName().startsWith("loci"))
     return true;
   return false;
 }
Ejemplo n.º 13
0
 void invert(ImagePlus imp) {
   Roi roi = imp.getRoi();
   if (roi == null || !roi.isArea()) {
     IJ.error("Inverse", "Area selection required");
     return;
   }
   ShapeRoi s1, s2;
   if (roi instanceof ShapeRoi) s1 = (ShapeRoi) roi;
   else s1 = new ShapeRoi(roi);
   s2 = new ShapeRoi(new Roi(0, 0, imp.getWidth(), imp.getHeight()));
   imp.setRoi(s1.xor(s2));
 }
Ejemplo n.º 14
0
 void toBoundingBox(ImagePlus imp) {
   Roi roi = imp.getRoi();
   if (roi == null) {
     noRoi("To Bounding Box");
     return;
   }
   Undo.setup(Undo.ROI, imp);
   Rectangle r = roi.getBounds();
   imp.deleteRoi();
   Roi roi2 = new Roi(r.x, r.y, r.width, r.height);
   transferProperties(roi, roi2);
   imp.setRoi(roi2);
 }
Ejemplo n.º 15
0
 void lineWidth() {
   int width = (int) IJ.getNumber("Line Width:", Line.getWidth());
   if (width == IJ.CANCELED) return;
   Line.setWidth(width);
   LineWidthAdjuster.update();
   ImagePlus imp = WindowManager.getCurrentImage();
   if (imp != null && imp.isProcessor()) {
     ImageProcessor ip = imp.getProcessor();
     ip.setLineWidth(Line.getWidth());
     Roi roi = imp.getRoi();
     if (roi != null && roi.isLine()) imp.draw();
   }
 }
Ejemplo n.º 16
0
 void createMaskFromThreshold(ImagePlus imp) {
   ImageProcessor ip = imp.getProcessor();
   if (ip.getMinThreshold() == ImageProcessor.NO_THRESHOLD) {
     IJ.error("Create Mask", "Area selection or thresholded image required");
     return;
   }
   double t1 = ip.getMinThreshold();
   double t2 = ip.getMaxThreshold();
   IJ.run("Duplicate...", "title=mask");
   ImagePlus imp2 = WindowManager.getCurrentImage();
   ImageProcessor ip2 = imp2.getProcessor();
   ip2.setThreshold(t1, t2, ImageProcessor.NO_LUT_UPDATE);
   IJ.run("Convert to Mask");
 }
Ejemplo n.º 17
0
 void convexHull(ImagePlus imp) {
   Roi roi = imp.getRoi();
   int type = roi != null ? roi.getType() : -1;
   if (!(type == Roi.FREEROI
       || type == Roi.TRACED_ROI
       || type == Roi.POLYGON
       || type == Roi.POINT)) {
     IJ.error("Convex Hull", "Polygonal or point selection required");
     return;
   }
   if (roi instanceof EllipseRoi) return;
   Polygon p = roi.getConvexHull();
   if (p != null) imp.setRoi(new PolygonRoi(p.xpoints, p.ypoints, p.npoints, roi.POLYGON));
 }
Ejemplo n.º 18
0
 void addToRoiManager(ImagePlus imp) {
   if (IJ.macroRunning() && Interpreter.isBatchModeRoiManager())
     IJ.error("run(\"Add to Manager\") may not work in batch mode macros");
   Frame frame = WindowManager.getFrame("ROI Manager");
   if (frame == null) IJ.run("ROI Manager...");
   if (imp == null) return;
   Roi roi = imp.getRoi();
   if (roi == null) return;
   frame = WindowManager.getFrame("ROI Manager");
   if (frame == null || !(frame instanceof RoiManager)) IJ.error("ROI Manager not found");
   RoiManager rm = (RoiManager) frame;
   boolean altDown = IJ.altKeyDown();
   IJ.setKeyUp(IJ.ALL_KEYS);
   if (altDown && !IJ.macroRunning()) IJ.setKeyDown(KeyEvent.VK_SHIFT);
   if (roi.getState() == Roi.CONSTRUCTING) { // wait (up to 2 sec.) until ROI finished
     long start = System.currentTimeMillis();
     while (true) {
       IJ.wait(10);
       if (roi.getState() != Roi.CONSTRUCTING) break;
       if ((System.currentTimeMillis() - start) > 2000) {
         IJ.beep();
         IJ.error("Add to Manager", "Selection is not complete");
         return;
       }
     }
   }
   rm.runCommand("add");
   IJ.setKeyUp(IJ.ALL_KEYS);
 }
  public String[] getOpenImageNames() {
    int[] imageIDList = WindowManager.getIDList();
    String[] imageNames = new String[imageIDList.length];
    // imageNames[0] = "None";

    if (!imageIDList.equals(null) && imageIDList.length > 1) {
      for (int i = 0; i < imageIDList.length; i++) {
        ImagePlus tempIMP = WindowManager.getImage(imageIDList[i]);
        imageNames[i] = tempIMP.getTitle();
      }
      return imageNames;
    } else {
      imageNames[0] = "None";
      return imageNames;
    }
  }
Ejemplo n.º 20
0
 void runMacro(String arg) {
   Roi roi = imp.getRoi();
   if (IJ.macroRunning()) {
     String options = Macro.getOptions();
     if (options != null
         && (options.indexOf("grid=") != -1 || options.indexOf("interpolat") != -1)) {
       IJ.run("Rotate... ", options); // run Image>Transform>Rotate
       return;
     }
   }
   if (roi == null) {
     noRoi("Rotate>Selection");
     return;
   }
   roi = (Roi) roi.clone();
   if (arg.equals("rotate")) {
     double d = Tools.parseDouble(angle);
     if (Double.isNaN(d)) angle = "15";
     String value = IJ.runMacroFile("ij.jar:RotateSelection", angle);
     if (value != null) angle = value;
   } else if (arg.equals("enlarge")) {
     String value = IJ.runMacroFile("ij.jar:EnlargeSelection", enlarge);
     if (value != null) enlarge = value;
     Roi.previousRoi = roi;
   }
 }
Ejemplo n.º 21
0
 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);
 }
Ejemplo n.º 22
0
 private void enlarge(ImagePlus imp) {
   Roi roi = imp.getRoi();
   if (roi != null) {
     Undo.setup(Undo.ROI, imp);
     roi = (Roi) roi.clone();
     (new RoiEnlarger()).run("");
   } else noRoi("Enlarge");
 }
Ejemplo n.º 23
0
 void lineToArea(ImagePlus imp) {
   Roi roi = imp.getRoi();
   if (roi == null || !roi.isLine()) {
     IJ.error("Line to Area", "Line selection required");
     return;
   }
   ImageProcessor ip2 = new ByteProcessor(imp.getWidth(), imp.getHeight());
   ip2.setColor(255);
   if (roi.getType() == Roi.LINE && roi.getStrokeWidth() > 1) ip2.fillPolygon(roi.getPolygon());
   else roi.drawPixels(ip2);
   // new ImagePlus("ip2", ip2.duplicate()).show();
   ip2.setThreshold(255, 255, ImageProcessor.NO_LUT_UPDATE);
   ThresholdToSelection tts = new ThresholdToSelection();
   Roi roi2 = tts.convert(ip2);
   imp.setRoi(roi2);
   Roi.previousRoi = (Roi) roi.clone();
 }
Ejemplo n.º 24
0
  void Contrast(ImagePlus imp, int radius, double par1, double par2, boolean doIwhite) {
    // G. Landini, 2013
    // Based on a simple contrast toggle. This procedure does not have user-provided paramters other
    // than the kernel radius
    // Sets the pixel value to either white or black depending on whether its current value is
    // closest to the local Max or Min respectively
    // The procedure is similar to Toggle Contrast Enhancement (see Soille, Morphological Image
    // Analysis (2004), p. 259

    ImagePlus Maximp, Minimp;
    ImageProcessor ip = imp.getProcessor(), ipMax, ipMin;
    int c_value = 0;
    int mid_gray;
    byte object;
    byte backg;

    if (doIwhite) {
      object = (byte) 0xff;
      backg = (byte) 0;
    } else {
      object = (byte) 0;
      backg = (byte) 0xff;
    }

    Maximp = duplicateImage(ip);
    ipMax = Maximp.getProcessor();
    RankFilters rf = new RankFilters();
    rf.rank(ipMax, radius, rf.MAX); // Maximum
    // Maximp.show();
    Minimp = duplicateImage(ip);
    ipMin = Minimp.getProcessor();
    rf.rank(ipMin, radius, rf.MIN); // Minimum
    // Minimp.show();
    byte[] pixels = (byte[]) ip.getPixels();
    byte[] max = (byte[]) ipMax.getPixels();
    byte[] min = (byte[]) ipMin.getPixels();
    for (int i = 0; i < pixels.length; i++) {
      pixels[i] =
          ((Math.abs((int) (max[i] & 0xff - pixels[i] & 0xff))
                  <= Math.abs((int) (pixels[i] & 0xff - min[i] & 0xff))))
              ? object
              : backg;
    }
    // imp.updateAndDraw();
    return;
  }
Ejemplo n.º 25
0
  /**
   * Execute the plugin functionality: duplicate and scale the given image.
   *
   * @return an Object[] array with the name and the scaled ImagePlus. Does NOT show the new, image;
   *     just returns it.
   */
  public Object[] exec(
      ImagePlus imp, String myMethod, int radius, double par1, double par2, boolean doIwhite) {

    // 0 - Check validity of parameters
    if (null == imp) return null;
    ImageProcessor ip = imp.getProcessor();
    int xe = ip.getWidth();
    int ye = ip.getHeight();

    // int [] data = (ip.getHistogram());

    IJ.showStatus("Thresholding...");
    long startTime = System.currentTimeMillis();
    // 1 Do it
    if (imp.getStackSize() == 1) {
      ip.snapshot();
      Undo.setup(Undo.FILTER, imp);
    }
    // Apply the selected algorithm
    if (myMethod.equals("Bernsen")) {
      Bernsen(imp, radius, par1, par2, doIwhite);
    } else if (myMethod.equals("Contrast")) {
      Contrast(imp, radius, par1, par2, doIwhite);
    } else if (myMethod.equals("Mean")) {
      Mean(imp, radius, par1, par2, doIwhite);
    } else if (myMethod.equals("Median")) {
      Median(imp, radius, par1, par2, doIwhite);
    } else if (myMethod.equals("MidGrey")) {
      MidGrey(imp, radius, par1, par2, doIwhite);
    } else if (myMethod.equals("Niblack")) {
      Niblack(imp, radius, par1, par2, doIwhite);
    } else if (myMethod.equals("Otsu")) {
      Otsu(imp, radius, par1, par2, doIwhite);
    } else if (myMethod.equals("Phansalkar")) {
      Phansalkar(imp, radius, par1, par2, doIwhite);
    } else if (myMethod.equals("Sauvola")) {
      Sauvola(imp, radius, par1, par2, doIwhite);
    }
    // IJ.showProgress((double)(255-i)/255);
    imp.updateAndDraw();
    imp.getProcessor().setThreshold(255, 255, ImageProcessor.NO_LUT_UPDATE);
    // 2 - Return the threshold and the image
    IJ.showStatus("\nDone " + (System.currentTimeMillis() - startTime) / 1000.0);
    return new Object[] {imp};
  }
Ejemplo n.º 26
0
 void createMask(ImagePlus imp) {
   Roi roi = imp.getRoi();
   boolean useInvertingLut = Prefs.useInvertingLut;
   Prefs.useInvertingLut = false;
   if (roi == null || !(roi.isArea() || roi.getType() == Roi.POINT)) {
     createMaskFromThreshold(imp);
     Prefs.useInvertingLut = useInvertingLut;
     return;
   }
   ImagePlus maskImp = null;
   Frame frame = WindowManager.getFrame("Mask");
   if (frame != null && (frame instanceof ImageWindow))
     maskImp = ((ImageWindow) frame).getImagePlus();
   if (maskImp == null) {
     ImageProcessor ip = new ByteProcessor(imp.getWidth(), imp.getHeight());
     if (!Prefs.blackBackground) ip.invertLut();
     maskImp = new ImagePlus("Mask", ip);
     maskImp.show();
   }
   ImageProcessor ip = maskImp.getProcessor();
   ip.setRoi(roi);
   ip.setValue(255);
   ip.fill(ip.getMask());
   maskImp.updateAndDraw();
   Prefs.useInvertingLut = useInvertingLut;
 }
Ejemplo n.º 27
0
  void MidGrey(ImagePlus imp, int radius, double par1, double par2, boolean doIwhite) {
    // See: Image Processing Learning Resourches HIPR2
    // http://homepages.inf.ed.ac.uk/rbf/HIPR2/adpthrsh.htm
    ImagePlus Maximp, Minimp;
    ImageProcessor ip = imp.getProcessor(), ipMax, ipMin;
    int c_value = 0;
    int mid_gray;
    byte object;
    byte backg;

    if (par1 != 0) {
      IJ.log("MidGrey: changed c_value from :" + c_value + "  to:" + par1);
      c_value = (int) par1;
    }

    if (doIwhite) {
      object = (byte) 0xff;
      backg = (byte) 0;
    } else {
      object = (byte) 0;
      backg = (byte) 0xff;
    }

    Maximp = duplicateImage(ip);
    ipMax = Maximp.getProcessor();
    RankFilters rf = new RankFilters();
    rf.rank(ipMax, radius, rf.MAX); // Maximum
    // Maximp.show();
    Minimp = duplicateImage(ip);
    ipMin = Minimp.getProcessor();
    rf.rank(ipMin, radius, rf.MIN); // Minimum
    // Minimp.show();
    byte[] pixels = (byte[]) ip.getPixels();
    byte[] max = (byte[]) ipMax.getPixels();
    byte[] min = (byte[]) ipMin.getPixels();

    for (int i = 0; i < pixels.length; i++) {
      pixels[i] =
          ((int) (pixels[i] & 0xff) > (int) (((max[i] & 0xff) + (min[i] & 0xff)) / 2) - c_value)
              ? object
              : backg;
    }
    // imp.updateAndDraw();
    return;
  }
Ejemplo n.º 28
0
 public void run(String arg) {
   imp = WindowManager.getCurrentImage();
   if (arg.equals("add")) {
     addToRoiManager(imp);
     return;
   }
   if (imp == null) {
     IJ.noImage();
     return;
   }
   if (arg.equals("all")) imp.setRoi(0, 0, imp.getWidth(), imp.getHeight());
   else if (arg.equals("none")) imp.killRoi();
   else if (arg.equals("restore")) imp.restoreRoi();
   else if (arg.equals("spline")) fitSpline();
   else if (arg.equals("circle")) fitCircle(imp);
   else if (arg.equals("ellipse")) createEllipse(imp);
   else if (arg.equals("hull")) convexHull(imp);
   else if (arg.equals("mask")) createMask(imp);
   else if (arg.equals("from")) createSelectionFromMask(imp);
   else if (arg.equals("inverse")) invert(imp);
   else if (arg.equals("toarea")) lineToArea(imp);
   else if (arg.equals("toline")) areaToLine(imp);
   else if (arg.equals("properties")) {
     setProperties("Properties ", imp.getRoi());
     imp.draw();
   } else if (arg.equals("band")) makeBand(imp);
   else if (arg.equals("tobox")) toBoundingBox(imp);
   else runMacro(arg);
 }
Ejemplo n.º 29
0
 // Finds the index of the upper right point that is guaranteed to be on convex hull
 int findFirstPoint(int[] xCoordinates, int[] yCoordinates, int n, ImagePlus imp) {
   int smallestY = imp.getHeight();
   int x, y;
   for (int i = 0; i < n; i++) {
     y = yCoordinates[i];
     if (y < smallestY) smallestY = y;
   }
   int smallestX = imp.getWidth();
   int p1 = 0;
   for (int i = 0; i < n; i++) {
     x = xCoordinates[i];
     y = yCoordinates[i];
     if (y == smallestY && x < smallestX) {
       smallestX = x;
       p1 = i;
     }
   }
   return p1;
 }
Ejemplo n.º 30
0
  private boolean showDialog() {
    String[] types = {"RAW", "JPEG", "ZLIB"};
    GenericDialog gd = new GenericDialog("Generate Bricks");
    gd.addChoice("FileType", types, filetype);
    gd.addNumericField("JPEG quality", jpeg_quality, 0);
    gd.addNumericField("Max file size (MB)", bdsizelimit, 0);

    int[] wlist = WindowManager.getIDList();
    if (wlist == null) return false;

    String[] titles = new String[wlist.length];
    for (int i = 0; i < wlist.length; i++) titles[i] = "";

    int tnum = 0;
    for (int i = 0; i < wlist.length; i++) {
      ImagePlus imp = WindowManager.getImage(wlist[i]);
      if (imp != null) {
        titles[tnum] = imp.getTitle();
        tnum++;
      }
    }
    gd.addChoice("Source image: ", titles, titles[0]);

    gd.showDialog();
    if (gd.wasCanceled()) return false;

    filetype = types[gd.getNextChoiceIndex()];
    jpeg_quality = (int) gd.getNextNumber();
    if (jpeg_quality > 100) jpeg_quality = 100;
    if (jpeg_quality < 0) jpeg_quality = 0;
    bdsizelimit = (int) gd.getNextNumber();

    int id = gd.getNextChoiceIndex();
    lvImgTitle = new ArrayList<String>();
    lvImgTitle.add(titles[id]);

    Prefs.set("filetype.string", filetype);
    Prefs.set("jpeg_quality.int", jpeg_quality);
    Prefs.set("bdsizelimit.int", bdsizelimit);

    return true;
  }