Ejemplo n.º 1
0
  @Override
  public boolean run(JEXEntry optionalEntry) {
    if (imageData == null || !imageData.getTypeName().getType().equals(JEXData.IMAGE)) {
      return false;
    }

    // Run the function
    TreeMap<DimensionMap, String> images = ImageReader.readObjectToImagePathTable(imageData);
    TreeMap<DimensionMap, String> outputMap = new TreeMap<DimensionMap, String>();

    int count = 0;
    int total = images.size();
    for (DimensionMap dim : images.keySet()) {

      if (this.isCanceled()) {
        return false;
      }

      String path = images.get(dim);
      // File f = new File(path);

      // get the image
      ImagePlus im = new ImagePlus(path);
      ij.process.ImageProcessor imProc = im.getProcessor();
      if (imProc == null) continue;
      FloatProcessor imp =
          (FloatProcessor) im.getProcessor().convertToFloat(); // should be a float processor

      int newWidth = (int) (imp.getWidth() * scale);
      imp.setInterpolationMethod(ImageProcessor.BILINEAR);
      imp = (FloatProcessor) imp.resize(newWidth);

      ImagePlus toSave = FunctionUtility.makeImageToSave(imp, "false", bitDepth);
      String finalPath = JEXWriter.saveImage(toSave);

      outputMap.put(dim.copy(), finalPath);
      Logs.log("Finished processing " + count + " of " + total + ".", 1, this);
      count++;

      // Status bar
      int percentage = (int) (100 * ((double) count / (double) images.size()));
      JEXStatics.statusBar.setProgressPercentage(percentage);
    }

    // Set the outputs
    output = ImageWriter.makeImageStackFromPaths("temp", outputMap);
    output.setDataObjectInfo("Stack binned using binning function");

    // Return status
    return true;
  }
  public void runStuff(
      DimensionMap map,
      TreeMap<DimensionMap, ROIPlus> maximaMap,
      TreeMap<DimensionMap, String> segMap,
      TreeMap<DimensionMap, String> maskMap,
      TreeMap<DimensionMap, String> imageMap,
      TreeMap<DimensionMap, Double> results,
      Canceler canceler) {
    // Get the Maxima
    ROIPlus maxima = maximaMap.get(map);

    // Make the mask image impMask
    // ByteProcessor impMask = (ByteProcessor) (new
    // ImagePlus(maskMap.get(map)).getProcessor().convertToByte(false));
    // ByteProcessor impSeg = (ByteProcessor) (new
    // ImagePlus(segMap.get(map)).getProcessor().convertToByte(false));
    ByteProcessor impSeg = (ByteProcessor) (new ImagePlus(segMap.get(map))).getProcessor();
    ByteProcessor impMask = (ByteProcessor) (new ImagePlus(maskMap.get(map))).getProcessor();
    ByteBlitter blit = new ByteBlitter(impSeg);
    blit.copyBits(impMask, 0, 0, Blitter.AND);
    FloatProcessor impImage =
        (FloatProcessor) (new ImagePlus(imageMap.get(map))).getProcessor().convertToFloat();
    Wand wand = new Wand(impSeg);
    Wand wand2 = new Wand(impMask);
    Vector<Double> measurements;
    for (IdPoint p : maxima.getPointList()) {
      if (canceler.isCanceled()) {
        return;
      }
      if (impSeg.getPixel(p.x, p.y)
          == 255) // if we land on a cell that made it through thresholding
      {
        wand.autoOutline(p.x, p.y); // outline it
        wand2.autoOutline(p.x, p.y);
        boolean partOfCellClump =
            !this.selectionsAreEqual(
                wand,
                wand2); // If the segmented and unsegmented masks do not agree on the roi, then this
        // cell is part of a clump.
        if (wand.npoints > 0) {
          Roi roi =
              new PolygonRoi(
                  wand.xpoints,
                  wand.ypoints,
                  wand.npoints,
                  Roi.POLYGON); // The roi helps for using getLength() (DON'T USE Roi.TRACED_ROI.,
          // IT SCREWS UP THE Polygon OBJECTS!!!! Bug emailed to ImageJ
          // folks)
          Polygon poly =
              new Polygon(
                  wand.xpoints,
                  wand.ypoints,
                  wand.npoints); // The polygon helps for using contains()
          Rectangle r = roi.getBounds();
          measurements = new Vector<Double>();
          for (int i = r.x; i < r.x + r.width; i++) {
            for (int j = r.y; j < r.y + r.height; j++) {
              // innerBoundary
              if (poly.contains(i, j) && impSeg.getPixelValue(i, j) == 255) {
                measurements.add((double) impImage.getPixelValue(i, j));
                // Logs.log("In - " + innerT, this);
              }
            }
          }

          impMask.setRoi(roi);
          ImageStatistics stats =
              ImageStatistics.getStatistics(
                  impMask,
                  ImageStatistics.AREA
                      & ImageStatistics.PERIMETER
                      & ImageStatistics.CIRCULARITY
                      & ImageStatistics.ELLIPSE,
                  null);
          if (measurements.size() > 0) {
            DimensionMap resultsMap = map.copy();
            resultsMap.put("Id", "" + p.id);

            resultsMap.put("Measurement", "X");
            results.put(resultsMap.copy(), (double) p.x);
            resultsMap.put("Measurement", "Y");
            results.put(resultsMap.copy(), (double) p.y);
            resultsMap.put("Measurement", "AREA");
            results.put(resultsMap.copy(), stats.area);
            resultsMap.put("Measurement", "PERIMETER");
            results.put(resultsMap.copy(), roi.getLength());
            resultsMap.put("Measurement", "CIRCULARITY");
            results.put(
                resultsMap.copy(), 4.0 * Math.PI * (stats.area / (Math.pow(roi.getLength(), 2))));
            resultsMap.put("Measurement", "ELLIPSE MAJOR");
            results.put(resultsMap.copy(), stats.major);
            resultsMap.put("Measurement", "ELLIPSE MINOR");
            results.put(resultsMap.copy(), stats.minor);
            resultsMap.put("Measurement", "MEAN");
            results.put(resultsMap.copy(), StatisticsUtility.mean(measurements));
            resultsMap.put("Measurement", "MEDIAN");
            results.put(resultsMap.copy(), StatisticsUtility.median(measurements));
            resultsMap.put("Measurement", "SUM");
            results.put(resultsMap.copy(), StatisticsUtility.sum(measurements));
            resultsMap.put("Measurement", "MIN");
            results.put(resultsMap.copy(), StatisticsUtility.min(measurements));
            resultsMap.put("Measurement", "MAX");
            results.put(resultsMap.copy(), StatisticsUtility.max(measurements));
            resultsMap.put("Measurement", "STDDEV");
            results.put(resultsMap.copy(), StatisticsUtility.stdDev(measurements));
            resultsMap.put("Measurement", "VARIANCE");
            results.put(resultsMap.copy(), StatisticsUtility.variance(measurements));
            resultsMap.put("Measurement", "CLUMP");
            results.put(resultsMap.copy(), (double) (partOfCellClump ? 1 : 0));
          }
        }
      }
    }
  }