示例#1
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.");
  }
  /**
   * Create ImagePlusExtended object from primary array (byte, short ....)
   *
   * @param title : title of the image
   * @param object : array
   * @param showImage : display image if true
   * @return ImagePlusExtended
   */
  public static ImagePlusExtended createImage(String title, Object object, boolean showImage) {
    ImagePlusExtended imp = null;
    int i = 0;
    if (object instanceof byte[][]) {
      byte[][] is = (byte[][]) object;
      int height = is.length;
      int width = is[0].length;
      ByteProcessor byteprocessor = new ByteProcessor(width, height);
      byte[] bp = (byte[]) byteprocessor.getPixels();
      int h = 0;
      while (h < height) {
        int w = 0;
        while (w < width) {
          bp[i] = is[h][w];
          w++;
          i++;
        }
        i = ++h * width;
      }
      imp = new ImagePlusExtended(title, byteprocessor);

    } else if (object instanceof short[][]) {
      short[][] is = (short[][]) object;
      int height = is.length;
      int width = is[0].length;
      ShortProcessor shortprocessor = new ShortProcessor(width, height);
      short[] sp = (short[]) shortprocessor.getPixels();
      int h = 0;
      while (h < height) {
        int w = 0;
        while (w < width) {
          sp[i] = is[h][w];
          w++;
          i++;
        }
        i = ++h * width;
      }
      imp = new ImagePlusExtended(title, shortprocessor);

    } else if (object instanceof int[][]) {
      int[][] is = (int[][]) object;
      int height = is.length;
      int width = is[0].length;
      ShortProcessor shortprocessor = new ShortProcessor(width, height);
      short[] sp = (short[]) shortprocessor.getPixels();
      int h = 0;
      while (h < height) {
        int w = 0;
        while (w < width) {
          sp[i] = (short) is[h][w];
          w++;
          i++;
        }
        i = ++h * width;
      }
      imp = new ImagePlusExtended(title, shortprocessor);
    } else if (object instanceof float[][]) {
      float[][] fs = (float[][]) object;
      int height = fs.length;
      int width = fs[0].length;
      FloatProcessor floatprocessor = new FloatProcessor(width, height);
      float[] fp = (float[]) floatprocessor.getPixels();
      int h = 0;
      while (h < height) {
        int w = 0;
        while (w < width) {
          fp[i] = fs[h][w];
          w++;
          i++;
        }
        i = ++h * width;
      }
      floatprocessor.resetMinAndMax();
      imp = new ImagePlusExtended(title, floatprocessor);

    } else if (object instanceof double[][]) {
      double[][] ds = (double[][]) object;
      int height = ds.length;
      int width = ds[0].length;
      FloatProcessor floatprocessor = new FloatProcessor(width, height);
      float[] fp = (float[]) floatprocessor.getPixels();
      int h = 0;
      while (h < height) {
        int w = 0;
        while (w < width) {
          fp[i] = (float) ds[h][w];
          w++;
          i++;
        }
        i = ++h * width;
      }
      floatprocessor.resetMinAndMax();
      imp = new ImagePlusExtended(title, floatprocessor);

    } else if (object instanceof byte[][][]) {
      byte[][][] is = (byte[][][]) object;
      int height = is.length;
      int width = is[0].length;
      int stackSize = is[0][0].length;
      ImageStack imagestack = new ImageStack(width, height);
      for (int sz = 0; sz < stackSize; sz++) {
        ByteProcessor byteprocessor = new ByteProcessor(width, height);
        byte[] bp = (byte[]) byteprocessor.getPixels();
        i = 0;
        int h = 0;
        while (h < height) {
          int w = 0;
          while (w < width) {
            bp[i] = is[h][w][sz];
            w++;
            i++;
          }
          i = ++h * width;
        }
        imagestack.addSlice("", byteprocessor);
      }
      imp = new ImagePlusExtended(title, imagestack);

    } else if (object instanceof short[][][]) {
      short[][][] is = (short[][][]) object;
      int height = is.length;
      int width = is[0].length;
      int stackSize = is[0][0].length;
      ImageStack imagestack = new ImageStack(width, height);
      for (int sz = 0; sz < stackSize; sz++) {
        ShortProcessor shortprocessor = new ShortProcessor(width, height);
        short[] sp = (short[]) shortprocessor.getPixels();
        i = 0;
        int h = 0;
        while (h < height) {
          int w = 0;
          while (w < width) {
            sp[i] = is[h][w][sz];
            w++;
            i++;
          }
          i = ++h * width;
        }
        imagestack.addSlice("", shortprocessor);
      }
      imp = new ImagePlusExtended(title, imagestack);

    } else if (object instanceof int[][][]) {
      int[][][] is = (int[][][]) object;
      int height = is.length;
      int width = is[0].length;
      int stackSize = is[0][0].length;
      ImageStack imagestack = new ImageStack(width, height);
      for (int sz = 0; sz < stackSize; sz++) {
        ShortProcessor shortprocessor = new ShortProcessor(width, height);
        short[] sp = (short[]) shortprocessor.getPixels();
        i = 0;
        int h = 0;
        while (h < height) {
          int w = 0;
          while (w < width) {
            sp[i] = (short) is[h][w][sz];
            w++;
            i++;
          }
          i = ++h * width;
        }
        if (sz == 0) shortprocessor.resetMinAndMax();
        imagestack.addSlice("", shortprocessor);
      }
      imp = new ImagePlusExtended(title, imagestack);

    } else if (object instanceof float[][][]) {
      float[][][] fs = (float[][][]) object;
      int height = fs.length;
      int width = fs[0].length;
      int stackSize = fs[0][0].length;
      ImageStack imagestack = new ImageStack(width, height);
      for (int sz = 0; sz < stackSize; sz++) {
        FloatProcessor floatprocessor = new FloatProcessor(width, height);
        float[] fp = (float[]) floatprocessor.getPixels();
        i = 0;
        int h = 0;
        while (h < height) {
          int w = 0;
          while (w < width) {
            fp[i] = fs[h][w][sz];
            w++;
            i++;
          }
          i = ++h * width;
        }
        if (sz == 0) floatprocessor.resetMinAndMax();
        imagestack.addSlice("", floatprocessor);
      }
      imp = new ImagePlusExtended(title, imagestack);

    } else if (object instanceof double[][][]) {
      double[][][] ds = (double[][][]) object;
      int height = ds.length;
      int width = ds[0].length;
      int stackSize = ds[0][0].length;
      ImageStack imagestack = new ImageStack(width, height);
      for (int sz = 0; sz < stackSize; sz++) {
        FloatProcessor floatprocessor = new FloatProcessor(width, height);
        float[] fp = (float[]) floatprocessor.getPixels();
        i = 0;
        int h = 0;
        while (h < height) {
          int w = 0;
          while (w < width) {
            fp[i] = (float) ds[h][w][sz];
            w++;
            i++;
          }
          i = ++h * width;
        }
        if (sz == 0) floatprocessor.resetMinAndMax();
        imagestack.addSlice("", floatprocessor);
      }
      imp = new ImagePlusExtended(title, imagestack);

    } else {
      System.out.println("MIJ Error message: Unknow type of images or volumes.");
      return null;
    }

    if (showImage) {
      imp.show();
      imp.updateAndDraw();
    }
    return imp;
  }