Esempio n. 1
0
 int[] smooth(int[] a, int n) {
   FloatProcessor fp = new FloatProcessor(n, 1);
   for (int i = 0; i < n; i++) fp.putPixelValue(i, 0, a[i]);
   GaussianBlur gb = new GaussianBlur();
   gb.blur1Direction(fp, 2.0, 0.01, true, 0);
   for (int i = 0; i < n; i++) a[i] = (int) Math.round(fp.getPixelValue(i, 0));
   return a;
 }
Esempio n. 2
0
  public static double[] getPixels(ImagePlus imp, int[] slices, int[] frames, int[] channels) {

    ImagePlus subImp = AccessImage.getSubHyperStack(imp, slices, frames, channels);
    ImageStack imageStack = subImp.getImageStack();

    double[] pixels = new double[subImp.getWidth() * subImp.getHeight() * imageStack.getSize()];

    for (int i = 0; i < imageStack.getSize(); i++) {
      switch (subImp.getType()) {
        case ImagePlus.GRAY8:
          ByteProcessor byteProcessor = (ByteProcessor) imageStack.getProcessor(i + 1);
          for (int iX = 0; iX < subImp.getWidth(); iX++) {
            for (int iY = 0; iY < subImp.getHeight(); iY++) {
              pixels[i * subImp.getWidth() * subImp.getHeight() + iY * subImp.getWidth() + iX] =
                  (double) byteProcessor.getPixelValue(iX, iY);
            }
          }
          break;
        case ImagePlus.GRAY16:
          ShortProcessor shortProcessor = (ShortProcessor) imageStack.getProcessor(i + 1);
          for (int iX = 0; iX < subImp.getWidth(); iX++) {
            for (int iY = 0; iY < subImp.getHeight(); iY++) {
              pixels[i * subImp.getWidth() * subImp.getHeight() + iY * subImp.getWidth() + iX] =
                  (double) shortProcessor.getPixelValue(iX, iY);
            }
          }
          break;
        case ImagePlus.GRAY32:
          FloatProcessor floatProcessor = (FloatProcessor) imageStack.getProcessor(i + 1);
          for (int iX = 0; iX < subImp.getWidth(); iX++) {
            for (int iY = 0; iY < subImp.getHeight(); iY++) {
              pixels[i * subImp.getWidth() * subImp.getHeight() + iY * subImp.getWidth() + iX] =
                  (double) floatProcessor.getPixelValue(iX, iY);
            }
          }
          break;
        case ImagePlus.COLOR_RGB:
          ColorProcessor colorProcessor = (ColorProcessor) imageStack.getProcessor(i + 1);
          int nX = subImp.getWidth();
          int nY = subImp.getHeight();
          byte[] red = new byte[nX * nY];
          byte[] green = new byte[nX * nY];
          byte[] blue = new byte[nX * nY];
          colorProcessor.getRGB(red, green, blue);
          int r, g, b;

          for (int j = 0; j < nX * nY; j++) {
            r = red[j] << 16;
            g = green[j] << 8;
            b = blue[j] << 0;
            pixels[i * nX * nY + j] = (double) (r + g + b);
          }
          break;
      }
    }

    return (pixels);
  }
  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));
          }
        }
      }
    }
  }