boolean setThresholdLevels(ImagePlus imp, ImageProcessor ip) {
   double t1 = ip.getMinThreshold();
   double t2 = ip.getMaxThreshold();
   boolean invertedLut = imp.isInvertedLut();
   boolean byteImage = ip instanceof ByteProcessor;
   if (ip instanceof ShortProcessor) imageType = SHORT;
   else if (ip instanceof FloatProcessor) imageType = FLOAT;
   else imageType = BYTE;
   if (t1 == ImageProcessor.NO_THRESHOLD) {
     ImageStatistics stats = imp.getStatistics();
     if (imageType != BYTE || (stats.histogram[0] + stats.histogram[255] != stats.pixelCount)) {
       IJ.error(
           "Particle Analyzer",
           "A thresholded image or 8-bit binary image is\n"
               + "required. Threshold levels can be set using\n"
               + "the Image->Adjust->Threshold tool.");
       canceled = true;
       return false;
     }
     boolean threshold255 = invertedLut;
     if (Prefs.blackBackground) threshold255 = !threshold255;
     if (threshold255) {
       level1 = 255;
       level2 = 255;
       fillColor = 64;
     } else {
       level1 = 0;
       level2 = 0;
       fillColor = 192;
     }
   } else {
     level1 = t1;
     level2 = t2;
     if (imageType == BYTE) {
       if (level1 > 0) fillColor = 0;
       else if (level2 < 255) fillColor = 255;
     } else if (imageType == SHORT) {
       if (level1 > 0) fillColor = 0;
       else if (level2 < 65535) fillColor = 65535;
     } else if (imageType == FLOAT) fillColor = -Float.MAX_VALUE;
     else return false;
   }
   imageType2 = imageType;
   if (redirectIP != null) {
     if (redirectIP instanceof ShortProcessor) imageType2 = SHORT;
     else if (redirectIP instanceof FloatProcessor) imageType2 = FLOAT;
     else if (redirectIP instanceof ColorProcessor) imageType2 = RGB;
     else imageType2 = BYTE;
   }
   return true;
 }
Example #2
0
 void createEllipse(ImagePlus imp) {
   IJ.showStatus("Fitting ellipse");
   Roi roi = imp.getRoi();
   if (roi == null) {
     IJ.error("Fit Ellipse", "Selection required");
     return;
   }
   if (roi.isLine()) {
     IJ.error("Fit Ellipse", "\"Fit Ellipse\" does not work with line selections");
     return;
   }
   ImageStatistics stats = imp.getStatistics(Measurements.CENTROID + Measurements.ELLIPSE);
   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));
 }