/**
  * Creates a new FloatProcessor using the specified ImageProcessor. Set 'cm' to null to use the
  * default grayscale LUT.
  */
 public SerializableFloatProcessor(ImageProcessor imageProcessor) {
   imageProcessor = imageProcessor.duplicate();
   FloatProcessor floatProcessor = imageProcessor.toFloat(0, null);
   this.width = floatProcessor.getWidth();
   this.height = floatProcessor.getHeight();
   this.pixels = (float[]) floatProcessor.getPixels();
   this.cm = floatProcessor.getColorModel();
   resetRoi();
   if (pixels != null) findMinAndMax();
 }
 boolean eraseOutsideRoi(ImageProcessor ip, Rectangle r, ImageProcessor mask) {
   int width = ip.getWidth();
   int height = ip.getHeight();
   ip.setRoi(r);
   if (excludeEdgeParticles && polygon != null) {
     ImageStatistics stats = ImageStatistics.getStatistics(ip, MIN_MAX, null);
     if (fillColor >= stats.min && fillColor <= stats.max) {
       double replaceColor = level1 - 1.0;
       if (replaceColor < 0.0 || replaceColor == fillColor) {
         replaceColor = level2 + 1.0;
         int maxColor = imageType == BYTE ? 255 : 65535;
         if (replaceColor > maxColor || replaceColor == fillColor) {
           IJ.error("Particle Analyzer", "Unable to remove edge particles");
           return false;
         }
       }
       for (int y = minY; y < maxY; y++) {
         for (int x = minX; x < maxX; x++) {
           int v = ip.getPixel(x, y);
           if (v == fillColor) ip.putPixel(x, y, (int) replaceColor);
         }
       }
     }
   }
   ip.setValue(fillColor);
   if (mask != null) {
     mask = mask.duplicate();
     mask.invert();
     ip.fill(mask);
   }
   ip.setRoi(0, 0, r.x, height);
   ip.fill();
   ip.setRoi(r.x, 0, r.width, r.y);
   ip.fill();
   ip.setRoi(r.x, r.y + r.height, r.width, height - (r.y + r.height));
   ip.fill();
   ip.setRoi(r.x + r.width, 0, width - (r.x + r.width), height);
   ip.fill();
   ip.resetRoi();
   // IJ.log("erase: "+fillColor+"	"+level1+"	"+level2+"	"+excludeEdgeParticles);
   // (new ImagePlus("ip2", ip.duplicate())).show();
   return true;
 }
  @Override
  public Grid2D applyToolToImage(Grid2D imageProcessor) {
    FloatProcessor imp = new FloatProcessor(imageProcessor.getWidth(), imageProcessor.getHeight());
    imp.setPixels(imageProcessor.getBuffer());

    if (!initBead) initializeBead();
    ImageProcessor imp1 = imp.duplicate(); // original

    double[][] beadMean3D = config.getBeadMeanPosition3D(); // [beadNo][x,y,z]
    double[] uv = new double[1];

    SimpleMatrix pMatrix = config.getGeometry().getProjectionMatrix(imageIndex).computeP();
    // [projection #][bead #][u, v, state[0: initial, 1: registered, 2: updated by hough searching]]
    double[][][] beadPosition2D = config.getBeadPosition2D();

    int noBeadRegistered = 0;

    double[][] xy1 = new double[WeightBearingBeadPositionBuilder.beadNo][2]; // original
    double[][] xy2 =
        new double[WeightBearingBeadPositionBuilder.beadNo]
            [2]; // warped	(mapped to the mean), control points, reference

    double[][] xy1_hat = new double[WeightBearingBeadPositionBuilder.beadNo][2]; // original
    double[][] xy2_hat = new double[WeightBearingBeadPositionBuilder.beadNo][2]; // original

    // double distanceReferenceToCurrentBead = 0;

    for (int i = WeightBearingBeadPositionBuilder.currentBeadNo; i >= 0; i--) {

      if (beadMean3D[i][0] != 0
          || beadMean3D[i][1] != 0
          || beadMean3D[i][2] != 0) { // assume bead 3d is registered.

        uv = compute2DCoordinates(beadMean3D[i], pMatrix);

        // find bead location if registered by txt: state 1
        if (beadPosition2D[imageIndex][i][2] == 1) {

          noBeadRegistered++;

          if (isDisplay) {
            imp1.setValue(2);
            imp1.drawLine(
                (int) Math.round(beadPosition2D[imageIndex][i][0] - 10),
                (int) Math.round(beadPosition2D[imageIndex][i][1] - 10),
                (int) Math.round(beadPosition2D[imageIndex][i][0] + 10),
                (int) Math.round(beadPosition2D[imageIndex][i][1] + 10));
            imp1.drawLine(
                (int) Math.round(beadPosition2D[imageIndex][i][0] - 10),
                (int) Math.round(beadPosition2D[imageIndex][i][1] + 10),
                (int) Math.round(beadPosition2D[imageIndex][i][0] + 10),
                (int) Math.round(beadPosition2D[imageIndex][i][1] - 10));
            imp1.drawString(
                "Bead " + i + " (state:" + (int) beadPosition2D[imageIndex][i][2] + ")",
                (int) beadPosition2D[imageIndex][i][0],
                (int) beadPosition2D[imageIndex][i][1] - 10);
          }

          xy1[noBeadRegistered - 1][0] = beadPosition2D[imageIndex][i][0];
          xy1[noBeadRegistered - 1][1] = beadPosition2D[imageIndex][i][1];

          xy2[noBeadRegistered - 1][0] = uv[0];
          xy2[noBeadRegistered - 1][1] = uv[1];

        } else if (imageIndex != 0
            && imageIndex != config.getGeometry().getNumProjectionMatrices() - 1) {

          if (beadPosition2D[imageIndex - 1][i][2] == 1
              && beadPosition2D[imageIndex + 1][i][2] == 1) {

            noBeadRegistered++;

            double xMean =
                (beadPosition2D[imageIndex - 1][i][0] + beadPosition2D[imageIndex - 1][i][0]) / 2;
            double yMean =
                (beadPosition2D[imageIndex + 1][i][1] + beadPosition2D[imageIndex + 1][i][1]) / 2;

            if (isDisplay) {
              imp1.setValue(2);
              imp1.drawLine(
                  (int) Math.round(xMean - 10),
                  (int) Math.round(yMean - 10),
                  (int) Math.round(xMean + 10),
                  (int) Math.round(yMean + 10));
              imp1.drawLine(
                  (int) Math.round(xMean - 10),
                  (int) Math.round(yMean + 10),
                  (int) Math.round(xMean + 10),
                  (int) Math.round(yMean - 10));
              imp1.drawString("Bead " + i + " (state:" + "M)", (int) xMean, (int) yMean - 10);
            }

            xy1[noBeadRegistered - 1][0] = xMean;
            xy1[noBeadRegistered - 1][1] = yMean;

            xy2[noBeadRegistered - 1][0] = uv[0];
            xy2[noBeadRegistered - 1][1] = uv[1];
          }
        }

        // mean projected bead
        //				imp1.drawLine((int) Math.round(uv[0]-10), (int) Math.round(uv[1]), (int)
        // Math.round(uv[0]+10), (int) Math.round(uv[1]));
        //				imp1.drawLine((int) Math.round(uv[0]), (int) Math.round(uv[1]-10), (int)
        // Math.round(uv[0]), (int) Math.round(uv[1]+10));
      }
    }

    if (isDisplay) {
      for (int x = 0; x < config.getGeometry().getDetectorWidth(); x += 50)
        imp1.drawLine(x, 0, x, config.getGeometry().getDetectorHeight());
      for (int y = 0; y < config.getGeometry().getDetectorHeight(); y += 50)
        imp1.drawLine(0, y, config.getGeometry().getDetectorWidth(), y);
    }

    if (isCornerIncluded) {
      xy1[noBeadRegistered + 0][0] = 0;
      xy1[noBeadRegistered + 0][1] = 0;
      xy2[noBeadRegistered + 0][0] = 0;
      xy2[noBeadRegistered + 0][1] = 0;

      xy1[noBeadRegistered + 1][0] = 0;
      xy1[noBeadRegistered + 1][1] = config.getGeometry().getDetectorHeight();
      xy2[noBeadRegistered + 1][0] = 0;
      xy2[noBeadRegistered + 1][1] = config.getGeometry().getDetectorHeight();

      xy1[noBeadRegistered + 2][0] = config.getGeometry().getDetectorWidth();
      xy1[noBeadRegistered + 2][1] = 0;
      xy2[noBeadRegistered + 2][0] = config.getGeometry().getDetectorWidth();
      xy2[noBeadRegistered + 2][1] = 0;

      xy1[noBeadRegistered + 3][0] = config.getGeometry().getDetectorWidth();
      xy1[noBeadRegistered + 3][1] = config.getGeometry().getDetectorHeight();
      xy2[noBeadRegistered + 3][0] = config.getGeometry().getDetectorWidth();
      xy2[noBeadRegistered + 3][1] = config.getGeometry().getDetectorHeight();

      noBeadRegistered = noBeadRegistered + 4;
    }

    boolean fScaling = true;

    double minX = Double.MAX_VALUE;
    double maxX = 0;
    double minY = Double.MAX_VALUE;
    double maxY = 0;
    double c = 0;
    if (fScaling) { // ----- scaling to reduce condition # of A matrix
      for (int i = 0; i < noBeadRegistered; i++) {
        minX = Math.min(minX, xy1[i][0]);
        maxX = Math.max(maxX, xy1[i][0]);
        minY = Math.min(minY, xy1[i][1]);
        maxY = Math.max(maxY, xy1[i][1]);
      }
      c = Math.max(maxX - minX, maxY - minY);

      for (int i = 0; i < noBeadRegistered; i++) {
        xy1_hat[i][0] = (xy1[i][0] - minX) / c;
        xy1_hat[i][1] = (xy1[i][1] - minY) / c;

        xy2_hat[i][0] = (xy2[i][0] - minX) / c;
        xy2_hat[i][1] = (xy2[i][1] - minY) / c;
      }
    } else {
      xy1_hat = xy1;
      xy2_hat = xy2;
    }

    ImageProcessor imp2 = imp1.duplicate(); // warped

    /*
     * A*x = b
     * Matrix A = (n + 3) * (n + 3);
     * n (noBeadRegistered + 4): # of control points + 4 corner points (assume corner points are static)
     */

    int n = noBeadRegistered + 3;

    SimpleMatrix A = new SimpleMatrix(n, n);
    SimpleVector x_x = new SimpleVector(n);
    SimpleVector x_y = new SimpleVector(n);
    SimpleVector b_x = new SimpleVector(n);
    SimpleVector b_y = new SimpleVector(n);

    double rij = 0;
    double valA = 0;
    double valb_x = 0;
    double valb_y = 0;

    // Matrix L formation
    // alpha: mean of distances between control points' xy-projections) is a constant only present
    // on the diagonal of K
    // lambda: TPS smoothing regularization coefficient

    double alpha = 0.0;
    double lambda = 1.6; // 1.6
    for (int i = 0; i < noBeadRegistered; i++) { // i= # of row
      for (int j = i; j < noBeadRegistered; j++) { // j= # of column
        alpha +=
            Math.sqrt(
                Math.pow(xy2_hat[i][0] - xy2_hat[j][0], 2)
                    + Math.pow(xy2_hat[i][1] - xy2_hat[j][1], 2));
      }
    }
    alpha = alpha / Math.pow(noBeadRegistered, 2);

    for (int i = 0; i < n; i++) { // i= # of row
      for (int j = i; j < n; j++) { // j= # of column
        if (i < 3 && j < 3) valA = 0;
        else if (i >= 3 && j >= 3 && i == j) {
          valA = Math.pow(alpha, 2) * lambda;
          // valA = lambda;
          if (imageIndex < 10)
            System.out.println("Regularization = " + valA + ", lambda= " + lambda);
        } else if (i == 0 && j >= 0) valA = 1;
        else if (i == 1 && j >= 3) valA = xy1_hat[j - 3][0];
        else if (i == 2 && j >= 3) valA = xy1_hat[j - 3][1];
        else {
          rij =
              Math.pow(xy1_hat[j - 3][0] - xy1_hat[i - 3][0], 2)
                  + Math.pow(xy1_hat[j - 3][1] - xy1_hat[i - 3][1], 2);
          if (rij == 0) valA = 0;
          else valA = rij * Math.log(rij);
        }

        A.setElementValue(i, j, valA);
        A.setElementValue(j, i, valA);
      }

      if (i < 3) {
        valb_x = 0;
        valb_y = 0;
      } else {
        //				valb_x = xy2_hat[i-3][0]-xy1_hat[i-3][0];
        //				valb_y = xy2_hat[i-3][1]-xy1_hat[i-3][1];
        valb_x = xy2[i - 3][0] - xy1[i - 3][0];
        valb_y = xy2[i - 3][1] - xy1[i - 3][1];
        //				if (imageIndex > 150 && imageIndex < 170)
        //					System.out.println("Idx" + imageIndex + ",Elevation" + (i-3) + ": " + valb_x + "---"
        // + valb_y);
      }

      b_x.setElementValue(i, valb_x);
      b_y.setElementValue(i, valb_y);
    }

    // System.out.println("A condition number=" + A.conditionNumber(MatrixNormType.MAT_NORM_L1));
    // System.out.println("A condition number=" + A.conditionNumber(MatrixNormType.MAT_NORM_LINF));

    x_x = Solvers.solveLinearSysytemOfEquations(A, b_x);
    x_y = Solvers.solveLinearSysytemOfEquations(A, b_y);

    if (fScaling) {
      // ----- pixel space coefficients a, b scaling back
      double tmpA0 =
          x_x.getElement(0) - x_x.getElement(1) * (minX / c) - x_x.getElement(2) * (minY / c);
      for (int j = 0; j < noBeadRegistered; j++) {
        tmpA0 -=
            Math.log(c)
                * 2
                * x_x.getElement(j + 3)
                * (Math.pow(xy1_hat[j][0], 2) + Math.pow(xy1_hat[j][1], 2));
      }
      x_x.setElementValue(0, tmpA0);
      tmpA0 = x_y.getElement(0) - x_y.getElement(1) * (minX / c) - x_y.getElement(2) * (minY / c);
      for (int j = 0; j < noBeadRegistered; j++) {
        tmpA0 -=
            Math.log(c)
                * 2
                * x_y.getElement(j + 3)
                * (Math.pow(xy1_hat[j][0], 2) + Math.pow(xy1_hat[j][1], 2));
      }
      x_y.setElementValue(0, tmpA0);

      x_x.setElementValue(1, x_x.getElement(1) / c);
      x_y.setElementValue(1, x_y.getElement(1) / c);
      x_x.setElementValue(2, x_x.getElement(2) / c);
      x_y.setElementValue(2, x_y.getElement(2) / c);

      for (int i = 3; i < n; i++) {
        x_x.setElementValue(i, x_x.getElement(i) / Math.pow(c, 2));
        x_y.setElementValue(i, x_y.getElement(i) / Math.pow(c, 2));
      }
      // ----- pixel space coefficients a, b scaling back end
    }

    double devU = 0;
    double devV = 0;
    // Do warping
    // if (imageIndex == 0) {
    for (int y = 0; y < config.getGeometry().getDetectorHeight(); y++) {
      // for (int y=252; y<253; y++) {
      for (int x = 0; x < config.getGeometry().getDetectorWidth(); x++) {
        // for (int x=606; x<607; x++) {
        devU = x_x.getElement(0) + x_x.getElement(1) * x + x_x.getElement(2) * y;
        devV = x_y.getElement(0) + x_y.getElement(1) * x + x_y.getElement(2) * y;
        for (int i = 0; i < noBeadRegistered; i++) {
          rij = Math.pow(xy1[i][0] - x, 2) + Math.pow(xy1[i][1] - y, 2);
          if (rij > 0) {
            devU += x_x.getElement(i + 3) * rij * Math.log(rij);
            devV += x_y.getElement(i + 3) * rij * Math.log(rij);
          }
        }

        //					devU = 0;
        //					devV = 0;

        imp2.setf(x, y, (float) imp1.getInterpolatedValue(x - devU, y - devV));

        // System.out.println("x, y=" + x + ", " + y + "\t" + devU + ", " + devV);
        // maxDevU = Math.max(maxDevU, devU);
        // maxDevV = Math.max(maxDevV, devV);
      }
    }

    // Error estimate after transformation
    //			for (int i=0; i<= WeightBearingBeadPositionBuilder.currentBeadNo; i++){
    //
    //				if (beadMean3D[i][0] != 0 || beadMean3D[i][1] != 0 || beadMean3D[i][2] != 0){ // assume
    // bead 3d is registered.
    //
    //					// find bead location if registered by txt: state 1
    //					if (beadPosition2D[imageIndex][i][2] == 1){
    //
    //						// Projected Reference
    //						uv = compute2DCoordinates(beadMean3D[i], pMatrix);
    //						double x = uv[0];
    //						double y = uv[1];
    //						// bead detected position in 2d
    //						// Transform to 2D coordinates, time variant position
    //						//beadPosition2D[imageIndex][i][0];
    //						//beadPosition2D[imageIndex][i][1];
    //
    //						devU = x_x.getElement(0) + x_x.getElement(1)*x + x_x.getElement(2)*y;
    //						devV = x_y.getElement(0) + x_y.getElement(1)*x + x_y.getElement(2)*y;
    //						for (int j=0; j<noBeadRegistered; j++){
    //							rij = Math.pow(xy1[j][0]-x, 2) + Math.pow(xy1[j][1]-y, 2);
    //							if (rij > 0) {
    //								devU += x_x.getElement(j+3)*rij*Math.log(rij);
    //								devV += x_y.getElement(j+3)*rij*Math.log(rij);
    //							}
    //						}
    //
    //						distanceReferenceToCurrentBead +=
    // Math.sqrt(Math.pow(uv[0]-(beadPosition2D[imageIndex][i][0]+devU),
    // 2)+Math.pow(uv[1]-(beadPosition2D[imageIndex][i][1]+devV), 2));
    //
    //					}
    //				}
    //			}
    //			System.out.println("Euclidean distance\t" + imageIndex + "\t" +
    // distanceReferenceToCurrentBead/noBeadRegistered);

    // }

    if (isDisplay) {
      for (int i = WeightBearingBeadPositionBuilder.currentBeadNo; i >= 0; i--) {

        if (beadMean3D[i][0] != 0
            || beadMean3D[i][1] != 0
            || beadMean3D[i][2] != 0) { // assume bead 3d is registered.

          uv = compute2DCoordinates(beadMean3D[i], pMatrix);

          imp2.setValue(2);
          // mean projected bead
          imp2.drawLine(
              (int) Math.round(uv[0] - 10),
              (int) Math.round(uv[1]),
              (int) Math.round(uv[0] + 10),
              (int) Math.round(uv[1]));
          imp2.drawLine(
              (int) Math.round(uv[0]),
              (int) Math.round(uv[1] - 10),
              (int) Math.round(uv[0]),
              (int) Math.round(uv[1] + 10));
        }
      }
    }
    Grid2D result = new Grid2D((float[]) imp2.getPixels(), imp2.getWidth(), imp2.getHeight());
    return result;
  }
  // Particle finding routine based on spots enhancement with
  // 2D PSF Gaussian approximated convolution/backgrounds subtraction, thresholding
  // and particle filtering
  void detectParticles(
      ImageProcessor ip, SMLDialog fdg, int nFrame, Overlay SpotsPositions_, Roi RoiActive_) {
    int nThreshold;
    FloatProcessor dupip = null; // duplicate of image
    ImageProcessor dushort; // duplicate of image
    ByteProcessor dubyte = null; // tresholded image
    TypeConverter tc;

    dupip = (FloatProcessor) ip.duplicate().convertToFloat();

    SMLblur1Direction(
        dupip, fdg.dPSFsigma * 0.5, 0.0002, true, (int) Math.ceil(5 * fdg.dPSFsigma * 0.5));
    SMLblur1Direction(dupip, fdg.dPSFsigma * 0.5, 0.0002, false, 0);

    // new ImagePlus("gassconvoluted", dupip.convertToFloat().duplicate()).show();
    // low-pass filtering by gaussian blurring
    // lowpassGauss.blurGaussian(dupip, fdg.dPSFsigma*0.5, fdg.dPSFsigma*0.5, 0.0002);

    // convolution with gaussian PSF kernel
    SMLconvolveFloat(dupip, fConKernel, fdg.nKernelSize, fdg.nKernelSize);

    // new ImagePlus("convoluted", dupip.duplicate()).show();
    tc = new TypeConverter(dupip, true);
    dushort = tc.convertToShort();
    // new ImagePlus("convoluted", dushort.duplicate()).show();

    // thresholding

    // old straightforward thresholding
    // imgstat = ImageStatistics.getStatistics(dupip, 22, null); //6 means MEAN + STD_DEV, look at
    // ij.measure.Measurements
    // nThreshold = (int)(imgstat.mean + 3.0*imgstat.stdDev);

    // new smart thresholding
    nThreshold = getThreshold(dushort);

    dushort.threshold(nThreshold);
    // convert to byte
    dubyte = (ByteProcessor) dushort.convertToByte(false);
    // new ImagePlus("threshold", dubyte.duplicate()).show();

    // morphological operations on thresholded image
    // dubyte.dilate(2, 0);
    // dubyte.erode(2, 0);

    // cleaning up image a bit
    if (fdg.nKernelSize > 3) {
      dubyte.dilate();
      // new ImagePlus("dilated", dubyte.duplicate()).show();
      dubyte.erode();
      // new ImagePlus("erosion", dubyte.duplicate()).show();
    }
    // dupip.invert();

    labelParticles(
        dubyte,
        ip,
        nFrame,
        fdg.dPixelSize,
        fdg.nAreaCut,
        fdg.dPSFsigma,
        SpotsPositions_,
        fdg.bShowParticles,
        RoiActive_); // , fdg.bIgnoreFP);//, fdg.dSymmetry/100);
  }
  /**
   * 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;
  }
  /** Ask for parameters and then execute. */
  public void run(String arg) {
    // 1 - Obtain the currently active image:
    ImagePlus imp = IJ.getImage();

    if (null == imp) {
      IJ.showMessage("There must be at least one image open");
      return;
    }

    if (imp.getBitDepth() != 8) {
      IJ.showMessage("Error", "Only 8-bit images are supported");
      return;
    }

    // 2 - Ask for parameters:
    GenericDialog gd = new GenericDialog("Auto Local Threshold");
    String[] methods = {
      "Try all",
      "Bernsen",
      "Contrast",
      "Mean",
      "Median",
      "MidGrey",
      "Niblack",
      "Otsu",
      "Phansalkar",
      "Sauvola"
    };
    gd.addMessage("Auto Local Threshold v1.5");
    gd.addChoice("Method", methods, methods[0]);
    gd.addNumericField("Radius", 15, 0);
    gd.addMessage("Special paramters (if different from default)");
    gd.addNumericField("Parameter_1", 0, 0);
    gd.addNumericField("Parameter_2", 0, 0);
    gd.addCheckbox("White objects on black background", true);
    if (imp.getStackSize() > 1) {
      gd.addCheckbox("Stack", false);
    }
    gd.addMessage("Thresholded result is always shown in white [255].");
    gd.showDialog();
    if (gd.wasCanceled()) return;

    // 3 - Retrieve parameters from the dialog
    String myMethod = gd.getNextChoice();
    int radius = (int) gd.getNextNumber();
    double par1 = (double) gd.getNextNumber();
    double par2 = (double) gd.getNextNumber();
    boolean doIwhite = gd.getNextBoolean();
    boolean doIstack = false;

    int stackSize = imp.getStackSize();
    if (stackSize > 1) doIstack = gd.getNextBoolean();

    // 4 - Execute!
    // long start = System.currentTimeMillis();
    if (myMethod.equals("Try all")) {
      ImageProcessor ip = imp.getProcessor();
      int xe = ip.getWidth();
      int ye = ip.getHeight();
      int ml = methods.length;
      ImagePlus imp2, imp3;
      ImageStack tstack = null, stackNew;
      if (stackSize > 1 && doIstack) {
        boolean doItAnyway = true;
        if (stackSize > 25) {
          YesNoCancelDialog d =
              new YesNoCancelDialog(
                  IJ.getInstance(),
                  "Auto Local Threshold",
                  "You might run out of memory.\n \nDisplay "
                      + stackSize
                      + " slices?\n \n \'No\' will process without display and\noutput results to the log window.");
          if (!d.yesPressed()) {
            //						doIlog=true; //will show in the log window
            doItAnyway = false;
          }
          if (d.cancelPressed()) return;
        }

        for (int j = 1; j <= stackSize; j++) {
          imp.setSlice(j);
          ip = imp.getProcessor();
          tstack = new ImageStack(xe, ye);
          for (int k = 1; k < ml; k++) tstack.addSlice(methods[k], ip.duplicate());
          imp2 = new ImagePlus("Auto Threshold", tstack);
          imp2.updateAndDraw();

          for (int k = 1; k < ml; k++) {
            imp2.setSlice(k);
            Object[] result = exec(imp2, methods[k], radius, par1, par2, doIwhite);
          }
          // if (doItAnyway){
          CanvasResizer cr = new CanvasResizer();
          stackNew = cr.expandStack(tstack, (xe + 2), (ye + 18), 1, 1);
          imp3 = new ImagePlus("Auto Threshold", stackNew);
          imp3.updateAndDraw();
          MontageMaker mm = new MontageMaker();
          mm.makeMontage(imp3, 3, 3, 1.0, 1, (ml - 1), 1, 0, true); // 3 columns and 3 rows
        }
        imp.setSlice(1);
        // if (doItAnyway)
        IJ.run("Images to Stack", "method=[Copy (center)] title=Montage");
        return;
      } else { // single image try all
        tstack = new ImageStack(xe, ye);
        for (int k = 1; k < ml; k++) tstack.addSlice(methods[k], ip.duplicate());
        imp2 = new ImagePlus("Auto Threshold", tstack);
        imp2.updateAndDraw();

        for (int k = 1; k < ml; k++) {
          imp2.setSlice(k);
          // IJ.log("analyzing slice with "+methods[k]);
          Object[] result = exec(imp2, methods[k], radius, par1, par2, doIwhite);
        }
        // imp2.setSlice(1);
        CanvasResizer cr = new CanvasResizer();
        stackNew = cr.expandStack(tstack, (xe + 2), (ye + 18), 1, 1);
        imp3 = new ImagePlus("Auto Threshold", stackNew);
        imp3.updateAndDraw();
        MontageMaker mm = new MontageMaker();
        mm.makeMontage(imp3, 3, 3, 1.0, 1, (ml - 1), 1, 0, true);
        return;
      }
    } else { // selected a method
      if (stackSize > 1 && doIstack) { // whole stack
        //				if (doIstackHistogram) {// one global histogram
        //					Object[] result = exec(imp, myMethod, noWhite, noBlack, doIwhite, doIset, doIlog,
        // doIstackHistogram );
        //				}
        //				else{ // slice by slice
        for (int k = 1; k <= stackSize; k++) {
          imp.setSlice(k);
          Object[] result = exec(imp, myMethod, radius, par1, par2, doIwhite);
        }
        //				}
        imp.setSlice(1);
      } else { // just one slice
        Object[] result = exec(imp, myMethod, radius, par1, par2, doIwhite);
      }
      // 5 - If all went well, show the image:
      // not needed here as the source image is binarised
    }
  }
  /**
   * @param ip
   * @param v
   */
  public static void run(final ImageProcessor ip, final float v) {
    final int w = ip.getWidth();
    final int h = ip.getHeight();
    final int wh = w * h;

    final ImageProcessor ipTarget = ip.duplicate();

    int numSaturatedPixels = 0;
    do {
      numSaturatedPixels = 0;
      for (int i = 0; i < wh; ++i)
        if (ip.getf(i) == v) {
          ++numSaturatedPixels;

          final int y = i / w;
          final int x = i % w;

          float s = 0;
          float n = 0;
          if (y > 0) {
            if (x > 0) {
              final float tl = ip.getf(x - 1, y - 1);
              if (tl != v) {
                s += 0.5f * tl;
                n += 0.5f;
              }
            }
            final float t = ip.getf(x, y - 1);
            if (t != v) {
              s += t;
              n += 1;
            }
            if (x < w - 1) {
              final float tr = ip.getf(x + 1, y - 1);
              if (tr != v) {
                s += 0.5f * tr;
                n += 0.5f;
              }
            }
          }

          if (x > 0) {
            final float l = ip.getf(x - 1, y);
            if (l != v) {
              s += l;
              n += 1;
            }
          }
          if (x < w - 1) {
            final float r = ip.getf(x + 1, y);
            if (r != v) {
              s += r;
              n += 1;
            }
          }

          if (y < h - 1) {
            if (x > 0) {
              final float bl = ip.getf(x - 1, y + 1);
              if (bl != v) {
                s += 0.5f * bl;
                n += 0.5f;
              }
            }
            final float b = ip.getf(x, y + 1);
            if (b != v) {
              s += b;
              n += 1;
            }
            if (x < w - 1) {
              final float br = ip.getf(x + 1, y + 1);
              if (br != v) {
                s += 0.5f * br;
                n += 0.5f;
              }
            }
          }

          if (n > 0) ipTarget.setf(i, s / n);
        }
      ip.setPixels(ipTarget.getPixelsCopy());
    } while (numSaturatedPixels > 0);
  }
  /*------------------------------------------------------------------*/
  void doIt(ImageProcessor ip) {
    int width = ip.getWidth();
    int height = ip.getHeight();
    double hLine[] = new double[width];
    double vLine[] = new double[height];

    if (!(ip.getPixels() instanceof float[])) {
      throw new IllegalArgumentException("Float image required");
    }
    switch (operation) {
      case GRADIENT_MAGNITUDE:
        {
          ImageProcessor h = ip.duplicate();
          ImageProcessor v = ip.duplicate();
          float[] floatPixels = (float[]) ip.getPixels();
          float[] floatPixelsH = (float[]) h.getPixels();
          float[] floatPixelsV = (float[]) v.getPixels();

          getHorizontalGradient(h, FLT_EPSILON);
          getVerticalGradient(v, FLT_EPSILON);
          for (int y = 0, k = 0; (y < height); y++) {
            for (int x = 0; (x < width); x++, k++) {
              floatPixels[k] =
                  (float)
                      Math.sqrt(
                          floatPixelsH[k] * floatPixelsH[k] + floatPixelsV[k] * floatPixelsV[k]);
            }
            stepProgressBar();
          }
        }
        break;
      case GRADIENT_DIRECTION:
        {
          ImageProcessor h = ip.duplicate();
          ImageProcessor v = ip.duplicate();
          float[] floatPixels = (float[]) ip.getPixels();
          float[] floatPixelsH = (float[]) h.getPixels();
          float[] floatPixelsV = (float[]) v.getPixels();

          getHorizontalGradient(h, FLT_EPSILON);
          getVerticalGradient(v, FLT_EPSILON);
          for (int y = 0, k = 0; (y < height); y++) {
            for (int x = 0; (x < width); x++, k++) {
              floatPixels[k] = (float) Math.atan2(floatPixelsH[k], floatPixelsV[k]);
            }
            stepProgressBar();
          }
        }
        break;
      case LAPLACIAN:
        {
          ImageProcessor hh = ip.duplicate();
          ImageProcessor vv = ip.duplicate();
          float[] floatPixels = (float[]) ip.getPixels();
          float[] floatPixelsHH = (float[]) hh.getPixels();
          float[] floatPixelsVV = (float[]) vv.getPixels();

          getHorizontalHessian(hh, FLT_EPSILON);
          getVerticalHessian(vv, FLT_EPSILON);
          for (int y = 0, k = 0; (y < height); y++) {
            for (int x = 0; (x < width); x++, k++) {
              floatPixels[k] = (float) (floatPixelsHH[k] + floatPixelsVV[k]);
            }
            stepProgressBar();
          }
        }
        break;
      case LARGEST_HESSIAN:
        {
          ImageProcessor hh = ip.duplicate();
          ImageProcessor vv = ip.duplicate();
          ImageProcessor hv = ip.duplicate();
          float[] floatPixels = (float[]) ip.getPixels();
          float[] floatPixelsHH = (float[]) hh.getPixels();
          float[] floatPixelsVV = (float[]) vv.getPixels();
          float[] floatPixelsHV = (float[]) hv.getPixels();

          getHorizontalHessian(hh, FLT_EPSILON);
          getVerticalHessian(vv, FLT_EPSILON);
          getCrossHessian(hv, FLT_EPSILON);
          for (int y = 0, k = 0; (y < height); y++) {
            for (int x = 0; (x < width); x++, k++) {
              floatPixels[k] =
                  (float)
                      (0.5
                          * (floatPixelsHH[k]
                              + floatPixelsVV[k]
                              + Math.sqrt(
                                  4.0 * floatPixelsHV[k] * floatPixelsHV[k]
                                      + (floatPixelsHH[k] - floatPixelsVV[k])
                                          * (floatPixelsHH[k] - floatPixelsVV[k]))));
            }
            stepProgressBar();
          }
        }
        break;
      case SMALLEST_HESSIAN:
        {
          ImageProcessor hh = ip.duplicate();
          ImageProcessor vv = ip.duplicate();
          ImageProcessor hv = ip.duplicate();
          float[] floatPixels = (float[]) ip.getPixels();
          float[] floatPixelsHH = (float[]) hh.getPixels();
          float[] floatPixelsVV = (float[]) vv.getPixels();
          float[] floatPixelsHV = (float[]) hv.getPixels();

          getHorizontalHessian(hh, FLT_EPSILON);
          getVerticalHessian(vv, FLT_EPSILON);
          getCrossHessian(hv, FLT_EPSILON);
          for (int y = 0, k = 0; (y < height); y++) {
            for (int x = 0; (x < width); x++, k++) {
              floatPixels[k] =
                  (float)
                      (0.5
                          * (floatPixelsHH[k]
                              + floatPixelsVV[k]
                              - Math.sqrt(
                                  4.0 * floatPixelsHV[k] * floatPixelsHV[k]
                                      + (floatPixelsHH[k] - floatPixelsVV[k])
                                          * (floatPixelsHH[k] - floatPixelsVV[k]))));
            }
            stepProgressBar();
          }
        }
        break;
      case HESSIAN_ORIENTATION:
        {
          ImageProcessor hh = ip.duplicate();
          ImageProcessor vv = ip.duplicate();
          ImageProcessor hv = ip.duplicate();
          float[] floatPixels = (float[]) ip.getPixels();
          float[] floatPixelsHH = (float[]) hh.getPixels();
          float[] floatPixelsVV = (float[]) vv.getPixels();
          float[] floatPixelsHV = (float[]) hv.getPixels();

          getHorizontalHessian(hh, FLT_EPSILON);
          getVerticalHessian(vv, FLT_EPSILON);
          getCrossHessian(hv, FLT_EPSILON);
          for (int y = 0, k = 0; (y < height); y++) {
            for (int x = 0; (x < width); x++, k++) {
              if (floatPixelsHV[k] < 0.0) {
                floatPixels[k] =
                    (float)
                        (-0.5
                            * Math.acos(
                                (floatPixelsHH[k] - floatPixelsVV[k])
                                    / Math.sqrt(
                                        4.0 * floatPixelsHV[k] * floatPixelsHV[k]
                                            + (floatPixelsHH[k] - floatPixelsVV[k])
                                                * (floatPixelsHH[k] - floatPixelsVV[k]))));
              } else {
                floatPixels[k] =
                    (float)
                        (0.5
                            * Math.acos(
                                (floatPixelsHH[k] - floatPixelsVV[k])
                                    / Math.sqrt(
                                        4.0 * floatPixelsHV[k] * floatPixelsHV[k]
                                            + (floatPixelsHH[k] - floatPixelsVV[k])
                                                * (floatPixelsHH[k] - floatPixelsVV[k]))));
              }
            }
            stepProgressBar();
          }
        }
        break;
      default:
        throw new IllegalArgumentException("Invalid operation");
    }
    ip.resetMinAndMax();
    imp.updateAndDraw();
  } /* end doIt */