Exemple #1
0
  public void run(String arg) {
    ImagePlus imp = WindowManager.getCurrentImage();
    if (imp == null) {
      IJ.noImage();
      return;
    }

    ImageStack stack1 = imp.getStack();
    String fileName = imp.getTitle();
    int endslice = stack1.getSize();

    ImagePlus imp2 = duplicateStack(imp);
    imp2.show();
    String duplicateName = imp2.getTitle();
    // IJ.showMessage("Box",fileName);
    ImageStack stack2 = imp2.getStack();
    stack1.deleteSlice(1);
    stack2.deleteSlice(endslice);

    String calculatorstring =
        ("image1='"
            + fileName
            + "' operation=Subtract image2="
            + imp2.getTitle()
            + " create stack");

    IJ.run("Image Calculator...", calculatorstring);
    ImagePlus imp3 = WindowManager.getCurrentImage();
    imp3.setTitle(fileName + " DeltaF up");
    imp2.getWindow().close();
    imp.getWindow().close();
  }
  public void run(ImageProcessor ip) {

    dimz = stack.getSize();
    dimy = stack.getWidth();
    dimx = stack.getHeight();

    SaveDialog sd = new SaveDialog("Save Measurements as Text...", "res", ".dat");
    String name = sd.getFileName();
    if (name == null) return;

    String directory = sd.getDirectory();

    nb = calculnb(stack); // -1;
    IJ.showStatus("Measure parameters for the " + nb + " objects ...");
    if (nb < 1) {
      IJ.showMessage("volume must be labeled");
    } else {
      double[] volume_m = new double[nb];
      int[] volume_p = new int[nb];
      double[] surface = new double[nb];
      double[] surfacenb = new double[nb];
      double[][][] I = new double[3][3][nb];
      double[][] J = new double[3][nb];
      double[][][] dir = new double[3][3][nb];
      double[] xg = new double[nb];
      double[] yg = new double[nb];
      double[] zg = new double[nb];
      byte[] bord = new byte[nb];
      //       		double[] a = new double[nb];
      //       		double[] b = new double[nb];
      //       		double[] c = new double[nb];
      //       		double[] Fab = new double[nb];
      //       		double[] Fac = new double[nb];
      //       		double[] Fbc = new double[nb];
      //       		double[] sp = new double[nb];
      double[][] lmin = new double[nb][3];
      double[][] lmax = new double[nb][3];
      IJ.showStatus("Measure surfaces ...");
      calculmarchsurfstack(stack, nb, surface, volume_m);
      calculmarchsurfstacknb(stack, nb, surfacenb);
      // calculvolumestack(stack,nb,volume_p);
      IJ.showStatus("Measure volumes and inertia ...");
      calculcgstack(stack, nb, volume_p, xg, yg, zg);
      calculinertiestack(stack, nb, xg, yg, zg, I);
      inertie(nb, I, J, dir);
      IJ.showStatus("Measure bounding boxes ...");
      boitestack(stack, nb, xg, yg, zg, dir, lmin, lmax);
      borderstack(stack, nb, bord);
      IJ.showStatus("Save results ...");
      sauvegarde(
          volume_p, volume_m, surface, surfacenb, xg, yg, zg, J, dir, nb, bord, lmin, lmax,
          directory, name);
      volume_m = null;
      volume_p = null;
      surface = null;
      xg = null;
      yg = null;
      zg = null;
    }
  }
 /**
  * Splits the specified RGB stack into three 8-bit grayscale stacks. Deletes the source stack if
  * keepSource is false.
  */
 public static ImageStack[] splitRGB(ImageStack rgb, boolean keepSource) {
   int w = rgb.getWidth();
   int h = rgb.getHeight();
   ImageStack[] channels = new ImageStack[3];
   for (int i = 0; i < 3; i++) channels[i] = new ImageStack(w, h);
   byte[] r, g, b;
   ColorProcessor cp;
   int slice = 1;
   int inc = keepSource ? 1 : 0;
   int n = rgb.getSize();
   for (int i = 1; i <= n; i++) {
     IJ.showStatus(i + "/" + n);
     r = new byte[w * h];
     g = new byte[w * h];
     b = new byte[w * h];
     cp = (ColorProcessor) rgb.getProcessor(slice);
     slice += inc;
     cp.getRGB(r, g, b);
     if (!keepSource) rgb.deleteSlice(1);
     channels[0].addSlice(null, r);
     channels[1].addSlice(null, g);
     channels[2].addSlice(null, b);
     IJ.showProgress((double) i / n);
   }
   return channels;
 }
  /**
   * Build an ImagePlus from double or float array
   *
   * @param imi : double or float array
   * @param name : name of the image
   * @param width
   * @param height
   * @return ImagePlus object
   */
  public static ImagePlus buildImagePlusFromDoubleOrFloatArrays(
      Object imi, String name, int width, int height) {
    float[][] im;
    if (imi instanceof double[][]) im = convertDoublesToFloats((double[][]) imi);
    else im = (float[][]) imi;
    if (im != null) {
      if (im.length == 1) {
        float[] tmp = new float[im[0].length];
        System.arraycopy(im[0], 0, tmp, 0, im[0].length);

        ImagePlus img = new ImagePlus(name, new FloatProcessor(width, height, tmp, null));
        return img;
      } else { // Stack
        ImageStack is = new ImageStack(width, height);
        for (int i = 0; i < im.length; i++) {
          if (im[i] != null) {
            float[] tmp = new float[im[i].length];
            System.arraycopy(im[i], 0, tmp, 0, im[i].length);
            is.addSlice("lvl " + (i + 1), new FloatProcessor(width, height, tmp, null));
          }
        }
        ImagePlus img = new ImagePlus(name, is);
        return img;
      }
    }
    return null;
  }
  /**
   * Read VoxelMatrix file stored in the old format
   *
   * @param path image file name with complete path
   * @param dis input data stream to read from
   * @param size1 dimension 1
   * @param size2 dimension 2
   * @param size3 dimension 3
   * @return read image or null if error
   * @throws IOException
   */
  static ImagePlus readOldFormat(String path, DataInputStream dis, int size1, int size2, int size3)
      throws IOException {
    // prepare variables
    ImageStack stack = new ImageStack(size1, size2);
    final int numPixels = size1 * size2;
    final int bufferSize = 4 * numPixels;
    final byte[] buffer = new byte[bufferSize];

    // write pixels
    for (int z = 0; z < size3; ++z) {
      final float[][] pixels = new float[size1][size2];
      int n = dis.read(buffer, 0, bufferSize);
      if (n < 0) return null;

      for (int j = 0; j < bufferSize; j += 4) {
        int tmp =
            (int)
                ((buffer[j]) << 24
                    | (buffer[j + 1]) << 16
                    | (buffer[j + 2]) << 8
                    | (buffer[j + 3]));
        int currentPos = j / 4;
        int y = currentPos / size2;
        int x = currentPos % size2;
        pixels[y][x] = (float) (tmp);
      }
      final FloatProcessor fp = new FloatProcessor(pixels);
      stack.addSlice(fp);
    }

    return new ImagePlus(path, stack);
  }
 /**
  * Convert VectorProcessor to ImagePlus with FloatProcessor stack.
  *
  * @return ImagePlus representation of this object.
  * @see #toFloatProcessors()
  */
 public ImagePlus toFloatStack() {
   final ImageStack stack = new ImageStack(width, height);
   final FloatProcessor[] fps = toFloatProcessors();
   for (int i = 0; i < fps.length; i++) {
     stack.addSlice("band " + i, fps[i]);
   }
   return new ImagePlus("From VectorProcessor", stack);
 }
Exemple #7
0
 void addScrollbars(ImagePlus imp) {
   ImageStack s = imp.getStack();
   int stackSize = s.getSize();
   nSlices = stackSize;
   hyperStack = imp.getOpenAsHyperStack();
   // imp.setOpenAsHyperStack(false);
   int[] dim = imp.getDimensions();
   int nDimensions = 2 + (dim[2] > 1 ? 1 : 0) + (dim[3] > 1 ? 1 : 0) + (dim[4] > 1 ? 1 : 0);
   if (nDimensions <= 3 && dim[2] != nSlices) hyperStack = false;
   if (hyperStack) {
     nChannels = dim[2];
     nSlices = dim[3];
     nFrames = dim[4];
   }
   // IJ.log("StackWindow: "+hyperStack+" "+nChannels+" "+nSlices+" "+nFrames);
   if (nSlices == stackSize) hyperStack = false;
   if (nChannels * nSlices * nFrames != stackSize) hyperStack = false;
   if (cSelector != null || zSelector != null || tSelector != null) removeScrollbars();
   ImageJ ij = IJ.getInstance();
   if (nChannels > 1) {
     cSelector = new ScrollbarWithLabel(this, 1, 1, 1, nChannels + 1, 'c');
     add(cSelector);
     if (ij != null) cSelector.addKeyListener(ij);
     cSelector.addAdjustmentListener(this);
     cSelector.setFocusable(false); // prevents scroll bar from blinking on Windows
     cSelector.setUnitIncrement(1);
     cSelector.setBlockIncrement(1);
   }
   if (nSlices > 1) {
     char label = nChannels > 1 || nFrames > 1 ? 'z' : 't';
     if (stackSize == dim[2] && imp.isComposite()) label = 'c';
     zSelector = new ScrollbarWithLabel(this, 1, 1, 1, nSlices + 1, label);
     if (label == 't') animationSelector = zSelector;
     add(zSelector);
     if (ij != null) zSelector.addKeyListener(ij);
     zSelector.addAdjustmentListener(this);
     zSelector.setFocusable(false);
     int blockIncrement = nSlices / 10;
     if (blockIncrement < 1) blockIncrement = 1;
     zSelector.setUnitIncrement(1);
     zSelector.setBlockIncrement(blockIncrement);
     sliceSelector = zSelector.bar;
   }
   if (nFrames > 1) {
     animationSelector = tSelector = new ScrollbarWithLabel(this, 1, 1, 1, nFrames + 1, 't');
     add(tSelector);
     if (ij != null) tSelector.addKeyListener(ij);
     tSelector.addAdjustmentListener(this);
     tSelector.setFocusable(false);
     int blockIncrement = nFrames / 10;
     if (blockIncrement < 1) blockIncrement = 1;
     tSelector.setUnitIncrement(1);
     tSelector.setBlockIncrement(blockIncrement);
   }
 }
  @Test
  public final void testRegionalMaxima_CubicMeshC26() {
    // load the reference image, and get its size
    ImageStack image = createCubicMeshImage();
    int sizeX = image.getWidth();
    int sizeY = image.getHeight();
    int sizeZ = image.getSize();

    // create test image: add a band with value 127 in the middle
    int zMid = sizeZ / 2;
    for (int y = 0; y < sizeY; y++) {
      for (int x = 0; x < sizeX; x++) {
        double val = image.getVoxel(x, y, zMid);
        if (val == 255) image.setVoxel(x, y, zMid, 127);
      }
    }

    ImageStack maxima = MinimaAndMaxima3D.regionalMaxima(image, 26);

    for (int z = 0; z < sizeZ; z++) {
      for (int y = 0; y < sizeY; y++) {
        for (int x = 0; x < sizeX; x++) {
          int v0 = (int) image.getVoxel(x, y, z);
          int v = (int) maxima.getVoxel(x, y, z);
          if (v0 == 255) assertEquals(255, v);
          else assertEquals(0, v);
        }
      }
    }
  }
Exemple #9
0
  public void run(String arg) {
    int[] wList = WindowManager.getIDList();
    if (wList == null) {
      IJ.error("No images are open.");
      return;
    }

    double thalf = 0.5;
    boolean keep;

    GenericDialog gd = new GenericDialog("Bleach correction");

    gd.addNumericField("t½:", thalf, 1);
    gd.addCheckbox("Keep source stack:", true);
    gd.showDialog();
    if (gd.wasCanceled()) return;

    long start = System.currentTimeMillis();
    thalf = gd.getNextNumber();
    keep = gd.getNextBoolean();
    if (keep) IJ.run("Duplicate...", "title='Bleach corrected' duplicate");
    ImagePlus imp1 = WindowManager.getCurrentImage();
    int d1 = imp1.getStackSize();
    double v1, v2;
    int width = imp1.getWidth();
    int height = imp1.getHeight();
    ImageProcessor ip1, ip2, ip3;

    int slices = imp1.getStackSize();
    ImageStack stack1 = imp1.getStack();
    ImageStack stack2 = imp1.getStack();
    int currentSlice = imp1.getCurrentSlice();

    for (int n = 1; n <= slices; n++) {
      ip1 = stack1.getProcessor(n);
      ip3 = stack1.getProcessor(1);
      ip2 = stack2.getProcessor(n);
      for (int x = 0; x < width; x++) {
        for (int y = 0; y < height; y++) {
          v1 = ip1.getPixelValue(x, y);
          v2 = ip3.getPixelValue(x, y);

          // =B8/(EXP(-C$7*A8))
          v1 = (v1 / Math.exp(-n * thalf));
          ip2.putPixelValue(x, y, v1);
        }
      }
      IJ.showProgress((double) n / slices);
      IJ.showStatus(n + "/" + slices);
    }

    // stack2.show();
    imp1.updateAndDraw();
  }
Exemple #10
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.");
  }
 public static ImageStack getChannel(ImagePlus imp, int c) {
   ImageStack stack1 = imp.getStack();
   ImageStack stack2 = new ImageStack(imp.getWidth(), imp.getHeight());
   for (int t = 1; t <= imp.getNFrames(); t++) {
     for (int z = 1; z <= imp.getNSlices(); z++) {
       int n = imp.getStackIndex(c, z, t);
       stack2.addSlice(stack1.getProcessor(n));
     }
   }
   return stack2;
 }
  /**
   * Resizes the current array to the given dimensions.
   *
   * @param input the array to resize
   * @param lenX the desired length in x-direction (with, horizontal length)
   * @param lenY the desired length in y-direction (height, vertical length)
   * @return a new 2d float array with the specified dimensions, and values interpolated from the
   *     input array.
   */
  public static float[][] resize(float[][] input, int lenX, int lenY) {
    float[] linearized = linearize2DArray(input);

    ImageStack is = ImageStack.create(input.length, input[0].length, 1, 32);
    is.setPixels(linearized, 1);

    ImageProcessor ip = new ImagePlus("", is).getProcessor();
    ip.setInterpolationMethod(ImageProcessor.BICUBIC);
    float[] resized = (float[]) ip.resize(lenX, lenY).getPixels();

    return linearizedArrayTo2D(resized, lenX, lenY);
  }
  private ImageStack createLeveledCubeGraphImage() {
    ImageStack stack = createCubeGraphImage();
    stack.setVoxel(5, 1, 1, 224);
    stack.setVoxel(9, 5, 1, 192);
    stack.setVoxel(9, 9, 5, 160);
    stack.setVoxel(9, 5, 9, 128);
    stack.setVoxel(5, 1, 9, 96);
    stack.setVoxel(1, 5, 9, 64);
    stack.setVoxel(1, 9, 5, 32);

    return stack;
  }
  private final void invertGray8Stack(ImageStack image) {
    int sizeX = image.getWidth();
    int sizeY = image.getHeight();
    int sizeZ = image.getSize();

    for (int z = 0; z < sizeZ; z++) {
      for (int y = 0; y < sizeY; y++) {
        for (int x = 0; x < sizeX; x++) {
          image.setVoxel(x, y, z, 255 - image.getVoxel(x, y, z));
        }
      }
    }
  }
Exemple #15
0
  public static ImagePlus scale(ImagePlus imp, double factor) {

    int newWidth = (int) Math.round(imp.getWidth() * factor);
    int newHeight = (int) Math.round(imp.getHeight() * factor);
    ImageStack is = new ImageStack(newWidth, newHeight);
    for (int i = 0; i < imp.getNSlices(); i++) {
      imp.setSlice(i);
      ImageProcessor ic = imp.getProcessor().resize(newWidth);
      is.addSlice(ic);
    }
    ImagePlus resImp = new ImagePlus(imp.getTitle() + "_scaled", is);
    return (resImp);
  }
 public void makeTransition(ImagePlus imp, int from, int num) {
   ImageStack stack = imp.getStack();
   int w = imp.getWidth(), h = imp.getHeight();
   ImageProcessor fr = stack.getProcessor(from).duplicate();
   ImageProcessor to = stack.getProcessor(from + 1);
   int[] col = new int[h];
   for (int n = 0; n < num; n++) {
     for (int x = n; x < w; x += num) {
       to.getColumn(x, 0, col, h);
       fr.putColumn(x, 0, col, h);
     }
     stack.addSlice("", fr, from + n);
   }
 }
  @Test
  public final void testDilationThinCubicMeshC26() {
    ImageStack mask = createThinCubicMeshImage();

    ImageStack marker = ImageStack.create(5, 5, 5, 8);
    marker.setVoxel(0, 0, 0, 255);

    GeodesicReconstruction3DHybrid0Gray8 algo = new GeodesicReconstruction3DHybrid0Gray8();
    algo.setConnectivity(26);

    ImageStack result = algo.applyTo(marker, mask);

    assertEquals(255, result.getVoxel(0, 4, 0), .01);
  }
 public void makeTransition(ImagePlus imp, int from, int num) {
   ImageStack stack = imp.getStack();
   int w = imp.getWidth(), h = imp.getHeight();
   ImageProcessor fr = stack.getProcessor(from).duplicate();
   ImageProcessor to = stack.getProcessor(from + 1);
   int[] row = new int[w];
   for (int n = 0; n < num; n++) {
     for (int y = n; y < h; y += num) {
       to.getRow(0, y, row, w);
       fr.putRow(0, y, row, w);
     }
     stack.addSlice("", fr, from + n);
   }
 }
  private ImageStack loadImages() {

    String fileNameNumberRef = new String();
    String fileNameNumberCorr = new String();

    if (numOfIteration < 9) {
      fileNameNumberRef = "000" + numOfIteration;
      fileNameNumberCorr = "000" + (numOfIteration + 1);
    }

    if (numOfIteration == 9) {
      fileNameNumberRef = "0009";
      fileNameNumberCorr = "0010";
    }

    if (numOfIteration >= 10 && numOfIteration < 99) {
      fileNameNumberRef = "00" + numOfIteration;
      fileNameNumberCorr = "00" + (numOfIteration + 1);
    }

    if (numOfIteration == 99) {
      fileNameNumberRef = "0099";
      fileNameNumberCorr = "0100";
    }

    if (numOfIteration >= 100 && numOfIteration < 999) {
      fileNameNumberRef = "0" + numOfIteration;
      fileNameNumberCorr = "0" + (numOfIteration + 1);
    }

    if (numOfIteration == 999) {
      fileNameNumberRef = "0999";
      fileNameNumberCorr = "1000";
    }

    if (numOfIteration >= 1000) {
      fileNameNumberRef = String.valueOf(numOfIteration);
      fileNameNumberCorr = String.valueOf(numOfIteration + 1);
    }

    ImagePlus ref = new ImagePlus(dir + name + fileNameNumberRef + format);
    ImagePlus corr = new ImagePlus(dir + name + (fileNameNumberCorr) + format);
    ImageStack tmp = ImageStack.create(ref.getWidth(), ref.getHeight(), 2, ref.getBitDepth());

    tmp.setPixels(ref.getProcessor().getPixels(), 1);
    tmp.setPixels(corr.getProcessor().getPixels(), 2);

    return tmp;
  }
Exemple #20
0
 ImagePlus openDicomStack(ZipInputStream zis, ZipEntry entry) throws IOException {
   ImagePlus imp = null;
   int count = 0;
   ImageStack stack = null;
   while (true) {
     if (count > 0) entry = zis.getNextEntry();
     if (entry == null) break;
     String name = entry.getName();
     ImagePlus imp2 = null;
     if (name.endsWith(".dcm")) {
       ByteArrayOutputStream out = new ByteArrayOutputStream();
       byte[] buf = new byte[4096];
       int len, byteCount = 0, progress = 0;
       while (true) {
         len = zis.read(buf);
         if (len < 0) break;
         out.write(buf, 0, len);
         byteCount += len;
         // IJ.showProgress((double)(byteCount%fileSize)/fileSize);
       }
       byte[] bytes = out.toByteArray();
       out.close();
       InputStream is = new ByteArrayInputStream(bytes);
       DICOM dcm = new DICOM(is);
       dcm.run(name);
       imp2 = dcm;
       is.close();
     }
     zis.closeEntry();
     if (imp2 == null) continue;
     count++;
     String label = imp2.getTitle();
     String info = (String) imp2.getProperty("Info");
     if (info != null) label += "\n" + info;
     if (count == 1) {
       imp = imp2;
       imp.getStack().setSliceLabel(label, 1);
     } else {
       stack = imp.getStack();
       stack.addSlice(label, imp2.getProcessor());
       imp.setStack(stack);
     }
   }
   zis.close();
   IJ.showProgress(1.0);
   if (count == 0)
     throw new IOException("This ZIP archive does not appear to contain any .dcm files");
   return imp;
 }
 /*
  * This test is illustrative of an issue in IJ1 where the internal stack gets
  * deleted in certain cases. If you setProcessor() on an ImagePlus with a
  * stack of 1 slice the stack gets deleted. A subsequent call to getStack()
  * will hatch a new one using the pixels of the current ImageProcessor.
  * Basically to avoid problems the legacy layer should never call
  * setProcessor() or if it does it should do a getStack() rather than cache
  * calls to previous getStack() calls.
  */
 @Test
 public void testStackKiller() {
   final ImageStack stack = new ImageStack(2, 2);
   final byte[] slice = new byte[] {1, 2, 3, 4};
   stack.addSlice("one slice", slice);
   final ImagePlus imp = new ImagePlus("fred", stack);
   assertEquals(stack, imp.getStack());
   final byte[] slice2 = new byte[] {5, 6, 7, 8};
   final ByteProcessor proc = new ByteProcessor(2, 2, slice2, null);
   imp.setProcessor(proc);
   final ImageStack secondStack = imp.getStack();
   assertNotSame(stack, secondStack);
   assertEquals(slice, stack.getPixels(1));
   assertEquals(slice2, secondStack.getPixels(1));
 }
Exemple #22
0
 void setStackDisplayRange(ImagePlus imp) {
   ImageStack stack = imp.getStack();
   double min = Double.MAX_VALUE;
   double max = -Double.MAX_VALUE;
   int n = stack.getSize();
   for (int i = 1; i <= n; i++) {
     if (!silentMode) IJ.showStatus("Calculating stack min and max: " + i + "/" + n);
     ImageProcessor ip = stack.getProcessor(i);
     ip.resetMinAndMax();
     if (ip.getMin() < min) min = ip.getMin();
     if (ip.getMax() > max) max = ip.getMax();
   }
   imp.getProcessor().setMinAndMax(min, max);
   imp.updateAndDraw();
 }
Exemple #23
0
 public void resetDisplayRanges() {
   int channels = getNChannels();
   if (lut == null) setupLuts(channels);
   ImageStack stack2 = getImageStack();
   if (lut == null
       || channels != lut.length
       || channels > stack2.getSize()
       || channels > MAX_CHANNELS) return;
   for (int i = 0; i < channels; ++i) {
     ImageProcessor ip2 = stack2.getProcessor(i + 1);
     ip2.resetMinAndMax();
     lut[i].min = ip2.getMin();
     lut[i].max = ip2.getMax();
   }
 }
Exemple #24
0
 public String getImageInfo(ImagePlus imp, ImageProcessor ip) {
   String infoProperty = null;
   if (imp.getStackSize() > 1) {
     ImageStack stack = imp.getStack();
     String label = stack.getSliceLabel(imp.getCurrentSlice());
     if (label != null && label.indexOf('\n') > 0) infoProperty = label;
   }
   if (infoProperty == null) {
     infoProperty = (String) imp.getProperty("Info");
     if (infoProperty == null) infoProperty = getExifData(imp);
   }
   String info = getInfo(imp, ip);
   if (infoProperty != null) return infoProperty + "\n------------------------\n" + info;
   else return info;
 }
  void borderstack(final ImageStack stack1, int nb, byte[] b) {
    final AtomicInteger ai = new AtomicInteger(0);
    final int dimX = stack1.getWidth();
    final int dimY = stack1.getHeight();
    final int dimZ = stack1.getSize();
    final byte[][] bord = new byte[dimZ][nb];

    for (int ithread = 0; ithread < threads.length; ithread++) {

      // Concurrently run in as many threads as CPUs

      threads[ithread] =
          new Thread() {

            public void run() {
              for (int superi = ai.getAndIncrement();
                  superi < dimZ;
                  superi = ai.getAndIncrement()) {
                int k = superi;
                ImageProcessor ip = stack1.getProcessor(k + 1);
                for (int j = 0; j < dimY; j++) {
                  for (int i = 0; i < dimX; i++) {
                    int val = (int) (ip.getPixelValue(i, j));
                    if ((val != 0)
                        && (i == 0
                            || j == 0
                            || k == 0
                            || i == dimX - 1
                            || j == dimY - 1
                            || k == dimZ - 1))
                      // bord[k][val-2]=1;
                      bord[k][val - 1] = 1;
                  }
                }
              }
            }
          };
    }
    startAndJoin(threads);
    for (int i = 0; i < dimZ; i++) {
      for (int j = 0; j < nb; j++) {
        b[j] *= (1 - bord[i][j]);
      }
    }
    for (int j = 0; j < nb; j++) {
      b[j] = (byte) (1 - b[j]);
    }
  }
Exemple #26
0
 public void run(String arg) {
   imp = WindowManager.getCurrentImage();
   if (imp == null) {
     IJ.noImage();
     return;
   }
   ImageStack stack = imp.getStack();
   int size = stack.getSize();
   if (size == 1 || (imp.getNChannels() == size && imp.isComposite())) {
     IJ.error("Stack or hyperstack required");
     return;
   }
   if (!showDialog(stack)) return;
   if (hyperstack) reduceHyperstack(imp, factor, reduceSlices);
   else reduceStack(imp, factor);
 }
Exemple #27
0
 ImageStack getRGBStack(ImagePlus imp) {
   ImageProcessor ip = imp.getProcessor();
   int w = ip.getWidth();
   int h = ip.getHeight();
   int size = w * h;
   byte[] r = new byte[size];
   byte[] g = new byte[size];
   byte[] b = new byte[size];
   ((ColorProcessor) ip).getRGB(r, g, b);
   ImageStack stack = new ImageStack(w, h);
   stack.addSlice("Red", r);
   stack.addSlice("Green", g);
   stack.addSlice("Blue", b);
   stack.setColorModel(ip.getDefaultColorModel());
   return stack;
 }
  /**
   * Combination of {@link ImageJInterpolation#crop(float[][], float, float, float, float)} and
   * {@link ImageJInterpolation#resize(float[][], int, int)}. Crops and resizes the input array into
   * a new output array.
   *
   * @param input the array to work on
   * @param lenX the desired length in x-direction (with, horizontal length)
   * @param lenY the desired length in y-direction (height, vertical length)
   * @param minX the lower index in x-direction
   * @param minY the lower index in y-direction
   * @param maxX the higher index in x-direction
   * @param maxY the higher index in y-direction
   * @return a new cropped and resized version of the input array
   */
  public static float[][] cropAndResize(
      float[][] input, int lenX, int lenY, float minX, float minY, float maxX, float maxY) {

    float[] linearized = linearize2DArray(input);

    ImageStack is = ImageStack.create(input[0].length, input.length, 1, 32);
    is.setPixels(linearized, 1);

    ImageProcessor ip = new ImagePlus("", is).getProcessor();
    ip.setInterpolationMethod(ImageProcessor.BICUBIC);
    ip.setRoi((int) minX, (int) minY, (int) Math.ceil(maxX - minX), (int) Math.ceil(maxY - minY));

    float[] resized = (float[]) ip.crop().resize(lenX, lenY).getPixels();

    return linearizedArrayTo2D(resized, lenX, lenY);
  }
 void showResults() {
   int count = rt.getCounter();
   // if (count==0) return;
   boolean lastSlice = !processStack || slice == imp.getStackSize();
   if ((showChoice == OVERLAY_OUTLINES || showChoice == OVERLAY_MASKS) && slice == 1 && count > 0)
     imp.setOverlay(overlay);
   else if (outlines != null && lastSlice) {
     String title = imp != null ? imp.getTitle() : "Outlines";
     String prefix;
     if (showChoice == MASKS) prefix = "Mask of ";
     else if (showChoice == ROI_MASKS) prefix = "Count Masks of ";
     else prefix = "Drawing of ";
     outlines.update(drawIP);
     outputImage = new ImagePlus(prefix + title, outlines);
     if (inSituShow) {
       if (imp.getStackSize() == 1) Undo.setup(Undo.TRANSFORM, imp);
       imp.setStack(null, outputImage.getStack());
     } else if (!hideOutputImage) outputImage.show();
   }
   if (showResults && !processStack) {
     TextPanel tp = IJ.getTextPanel();
     if (beginningCount > 0 && tp != null && tp.getLineCount() != count) rt.show("Results");
     Analyzer.firstParticle = beginningCount;
     Analyzer.lastParticle = Analyzer.getCounter() - 1;
   } else Analyzer.firstParticle = Analyzer.lastParticle = 0;
 }
  /**
   * Rearranges an ImageJ XYCTZ Hyperstack into XYCZT without wasting memory for processing 3d
   * images as a chunk, if it is already XYCTZ it will shuffle it back to XYCZT
   *
   * @param imp - the input {@link ImagePlus}
   * @return - an {@link ImagePlus} which can be the same instance if the image is XYC, XYZ, XYT or
   *     XY - otherwise a new instance containing the same processors but in the new order XYZCT
   */
  public static ImagePlus switchZTinXYCZT(final ImagePlus imp) {
    final int numChannels = imp.getNChannels();
    final int numTimepoints = imp.getNFrames();
    final int numZStacks = imp.getNSlices();

    String newTitle;
    if (imp.getTitle().startsWith("[XYCTZ]"))
      newTitle = imp.getTitle().substring(8, imp.getTitle().length());
    else newTitle = "[XYCTZ] " + imp.getTitle();

    // there is only one timepoint and one z-stack, i.e. XY(C)
    if (numTimepoints == 1 && numZStacks == 1) {
      return imp;
    }
    // there is only one channel XYZ(T) or one z-stack XYC(T)
    else if (numTimepoints == 1 || numZStacks == 1) {
      // numchannels, z-slices, timepoints
      // but of course now reversed...
      imp.setDimensions(numChannels, numTimepoints, numZStacks);
      imp.setTitle(newTitle);
      return imp;
    }

    // now we have to rearrange
    final ImageStack stack = new ImageStack(imp.getWidth(), imp.getHeight());

    for (int z = 1; z <= numZStacks; ++z) {
      for (int t = 1; t <= numTimepoints; ++t) {
        for (int c = 1; c <= numChannels; ++c) {
          final int index = imp.getStackIndex(c, z, t);
          final ImageProcessor ip = imp.getStack().getProcessor(index);
          stack.addSlice(imp.getStack().getSliceLabel(index), ip);
        }
      }
    }

    final ImagePlus result = new ImagePlus(newTitle, stack);
    // numchannels, z-slices, timepoints
    // but of course now reversed...
    result.setDimensions(numChannels, numTimepoints, numZStacks);
    result.getCalibration().pixelWidth = imp.getCalibration().pixelWidth;
    result.getCalibration().pixelHeight = imp.getCalibration().pixelHeight;
    result.getCalibration().pixelDepth = imp.getCalibration().pixelDepth;
    final CompositeImage composite = new CompositeImage(result);

    return composite;
  }