/**
   * Retrieve byte data of 8 bits image
   *
   * @param imp : image to analyze
   * @return byte array with data for each pixel
   */
  public static byte[] getByteData(ImagePlus imp) {
    byte[] imagebyte = null;
    for (int i = 0; i < imp.getStackSize(); i++) {
      ByteProcessor fp = (ByteProcessor) imp.getImageStack().getProcessor(i + 1).duplicate();
      if (i == 0) imagebyte = (byte[]) fp.getPixels();
      else
        imagebyte =
            concatByte(imagebyte, (byte[]) fp.getPixels()); // (byte[]) ArrayUtils.addAll(imagebyte,
      // (byte[])fp.getPixels());
      System.out.println("slice : " + (i + 1) + "/" + imp.getStackSize());
    }

    return imagebyte;
  }
Exemplo n.º 2
0
  protected static final FloatProcessor scaleByte(final ByteProcessor bp) {
    final FloatProcessor fp = new FloatProcessor(bp.getWidth(), bp.getHeight());
    final byte[] bytes = (byte[]) bp.getPixels();
    final float[] floats = (float[]) fp.getPixels();
    for (int i = 0; i < bytes.length; ++i) floats[i] = (bytes[i] & 0xff) / 255.0f;

    return fp;
  }
  /**
   * 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;
  }