Example #1
0
 public void reduceHyperstack(ImagePlus imp, int factor, boolean reduceSlices) {
   int channels = imp.getNChannels();
   int slices = imp.getNSlices();
   int frames = imp.getNFrames();
   int zfactor = reduceSlices ? factor : 1;
   int tfactor = reduceSlices ? 1 : factor;
   ImageStack stack = imp.getStack();
   ImageStack stack2 = new ImageStack(imp.getWidth(), imp.getHeight());
   boolean virtual = stack.isVirtual();
   int slices2 = slices / zfactor + ((slices % zfactor) != 0 ? 1 : 0);
   int frames2 = frames / tfactor + ((frames % tfactor) != 0 ? 1 : 0);
   int n = channels * slices2 * frames2;
   int count = 1;
   for (int t = 1; t <= frames; t += tfactor) {
     for (int z = 1; z <= slices; z += zfactor) {
       for (int c = 1; c <= channels; c++) {
         int i = imp.getStackIndex(c, z, t);
         IJ.showProgress(i, n);
         ImageProcessor ip = stack.getProcessor(imp.getStackIndex(c, z, t));
         // IJ.log(count++ +"  "+i+" "+c+" "+z+" "+t);
         stack2.addSlice(stack.getSliceLabel(i), ip);
       }
     }
   }
   imp.setStack(stack2, channels, slices2, frames2);
   Calibration cal = imp.getCalibration();
   if (cal.scaled()) cal.pixelDepth *= zfactor;
   if (virtual) imp.setTitle(imp.getTitle());
   IJ.showProgress(1.0);
 }
  /**
   * 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);
  }
Example #4
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;
 }
Example #5
0
  /**
   * Returns subset of given hyperstack
   *
   * @param imp
   * @param slices
   * @param frames
   * @param channels
   * @return
   */
  public static ImagePlus getSubHyperStack(
      ImagePlus imp, int[] slices, int[] frames, int[] channels) {

    int sizes[] = imp.getDimensions();

    // Zero index --> keep all indices
    if (slices.length == 1 && slices[0] == 0) {
      slices = Commons.getSequence(1, sizes[3]);
    }
    if (frames.length == 1 && frames[0] == 0) {
      frames = Commons.getSequence(1, sizes[4]);
    }
    if (channels.length == 1 && channels[0] == 0) {
      channels = Commons.getSequence(1, sizes[2]);
    }

    // Check input arguments
    if ((Commons.getMin(slices) < 1) || (Commons.getMax(slices) > sizes[3])) {
      System.out.println("Subscripts for slices out of bounds.");
      return imp;
    }
    if ((Commons.getMin(frames) < 1) || (Commons.getMax(frames) > sizes[4])) {
      System.out.println("Subscripts for frames out of bounds.");
      return imp;
    }
    if ((Commons.getMin(channels) < 1) || (Commons.getMax(channels) > sizes[2])) {
      System.out.println("Subscripts for channels out of bounds.");
      return imp;
    }

    // Convert indices into stack indices
    ImageStack ims = imp.getImageStack();
    boolean keep[] = new boolean[ims.getSize()];
    Arrays.fill(keep, false);

    int slicesToKeep = 0;
    for (int i = 0; i < slices.length; i++) {
      for (int j = 0; j < frames.length; j++) {
        for (int k = 0; k < channels.length; k++) {
          int test = imp.getStackIndex(channels[k], slices[i], frames[j]);
          keep[imp.getStackIndex(channels[k], slices[i], frames[j]) - 1] = true;
          slicesToKeep++;
        }
      }
    }

    ImageStack resIms = new ImageStack(imp.getWidth(), imp.getHeight());

    for (int i = 0; i < ims.getSize(); i++) {
      if (keep[i]) {
        resIms.addSlice(ims.getProcessor(i + 1));
      }
    }
    ImagePlus resImp = new ImagePlus("SubHyperStack of " + imp.getTitle(), resIms);
    resImp.setOpenAsHyperStack(true);
    resImp.setDimensions(channels.length, slices.length, frames.length);

    return resImp;
  }
Example #6
0
 /**
  * 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);
 }
Example #7
0
 private void doHSRGBProjection(ImagePlus rgbImp) {
   ImageStack stack = rgbImp.getStack();
   ImageStack stack2 = new ImageStack(stack.getWidth(), stack.getHeight());
   for (int i = startSlice; i <= stopSlice; i++) stack2.addSlice(null, stack.getProcessor(i));
   startSlice = 1;
   stopSlice = stack2.getSize();
   doRGBProjection(stack2);
 }
Example #8
0
  @Override
  public void run(ImageProcessor ip) {
    w = image.getWidth();
    h = image.getHeight();
    float[] rgb = new float[3];
    float[] lab = new float[3];

    if (image.getStack().getSize() == 1) {
      int[] pixels = (int[]) image.getProcessor().getPixels();
      float[] l = new float[w * h];
      float[] a = new float[w * h];
      float[] b = new float[w * h];

      for (int i = 0; i < w * h; i++) {
        int v = pixels[i];
        CIELAB.int2sRGB(v, rgb);
        CIELAB.sRGB2CIELAB(rgb, lab);
        l[i] = lab[0];
        a[i] = lab[1];
        b[i] = lab[2];
      }
      ImageStack stack = new ImageStack(w, h);
      stack.addSlice("L", new FloatProcessor(w, h, l, null));
      stack.addSlice("a", new FloatProcessor(w, h, a, null));
      stack.addSlice("b", new FloatProcessor(w, h, b, null));
      new ImagePlus(image.getTitle() + " Lab", stack).show();
    } else {
      int[] rgbi = new int[3];
      ImageStack stack = image.getStack();
      float[] l = (float[]) stack.getProcessor(1).getPixels();
      float[] a = (float[]) stack.getProcessor(2).getPixels();
      float[] b = (float[]) stack.getProcessor(3).getPixels();
      int[] pixels = new int[w * h];

      for (int i = 0; i < w * h; i++) {
        lab[0] = l[i];
        lab[1] = a[i];
        lab[2] = b[i];
        CIELAB.CIELAB2sRGB(lab, rgb);
        pixels[i] = CIELAB.sRGB2int(rgb);
      }
      ip = new ColorProcessor(w, h, pixels);
      new ImagePlus(image.getTitle() + " RGB", ip).show();
    }
  }
 /** Adds the image in 'ip' to the end of the stack. */
 public void addSlice(String sliceLabel, ImageProcessor ip) {
   if (ip.getWidth() != width || ip.getHeight() != height)
     throw new IllegalArgumentException("Dimensions do not match");
   if (nSlices == 0) {
     cm = ip.getColorModel();
     min = ip.getMin();
     max = ip.getMax();
   }
   addSlice(sliceLabel, ip.getPixels());
 }
 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;
 }
Example #11
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);
  }
 /**
  * Adds the image in 'ip' to the stack following slice 'n'. Adds the slice to the beginning of the
  * stack if 'n' is zero.
  */
 public void addSlice(String sliceLabel, ImageProcessor ip, int n) {
   if (n < 0 || n > nSlices) throw new IllegalArgumentException(outOfRange + n);
   addSlice(sliceLabel, ip);
   Object tempSlice = stack[nSlices - 1];
   String tempLabel = label[nSlices - 1];
   int first = n > 0 ? n : 1;
   for (int i = nSlices - 1; i >= first; i--) {
     stack[i] = stack[i - 1];
     label[i] = label[i - 1];
   }
   stack[n] = tempSlice;
   label[n] = tempLabel;
 }
Example #13
0
 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);
   }
 }
Example #14
0
 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);
   }
 }
Example #15
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;
 }
Example #16
0
 /*
  * 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));
 }
Example #17
0
  public ImagePlus record() {
    pause();
    int s = univ.getStartTime();
    int e = univ.getEndTime();

    univ.showTimepoint(s);
    try {
      Thread.sleep(100);
    } catch (InterruptedException ex) {
    }
    ImagePlus imp = univ.takeSnapshot();
    ImageStack stack = new ImageStack(imp.getWidth(), imp.getHeight());
    stack.addSlice("", imp.getProcessor());

    for (int i = s + 1; i <= e; i++) {
      univ.showTimepoint(i);
      try {
        Thread.sleep(100);
      } catch (InterruptedException ex) {
      }
      stack.addSlice("", univ.takeSnapshot().getProcessor());
    }
    return new ImagePlus("Movie", stack);
  }
  /**
   * 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;
  }
Example #19
0
 /** Opens a stack of images. */
 ImagePlus openStack(ColorModel cm, boolean show) {
   ImageStack stack = new ImageStack(fi.width, fi.height, cm);
   long skip = fi.getOffset();
   Object pixels;
   try {
     ImageReader reader = new ImageReader(fi);
     InputStream is = createInputStream(fi);
     if (is == null) return null;
     IJ.resetEscape();
     for (int i = 1; i <= fi.nImages; i++) {
       if (!silentMode) IJ.showStatus("Reading: " + i + "/" + fi.nImages);
       if (IJ.escapePressed()) {
         IJ.beep();
         IJ.showProgress(1.0);
         silentMode = false;
         return null;
       }
       pixels = reader.readPixels(is, skip);
       if (pixels == null) break;
       stack.addSlice(null, pixels);
       skip = fi.gapBetweenImages;
       if (!silentMode) IJ.showProgress(i, fi.nImages);
     }
     is.close();
   } catch (Exception e) {
     IJ.log("" + e);
   } catch (OutOfMemoryError e) {
     IJ.outOfMemory(fi.fileName);
     stack.trim();
   }
   if (!silentMode) IJ.showProgress(1.0);
   if (stack.getSize() == 0) return null;
   if (fi.sliceLabels != null && fi.sliceLabels.length <= stack.getSize()) {
     for (int i = 0; i < fi.sliceLabels.length; i++) stack.setSliceLabel(fi.sliceLabels[i], i + 1);
   }
   ImagePlus imp = new ImagePlus(fi.fileName, stack);
   if (fi.info != null) imp.setProperty("Info", fi.info);
   if (show) imp.show();
   imp.setFileInfo(fi);
   setCalibration(imp);
   ImageProcessor ip = imp.getProcessor();
   if (ip.getMin() == ip.getMax()) // find stack min and max if first slice is blank
   setStackDisplayRange(imp);
   if (!silentMode) IJ.showProgress(1.0);
   silentMode = false;
   return imp;
 }
Example #20
0
 public void reduceStack(ImagePlus imp, int factor) {
   ImageStack stack = imp.getStack();
   boolean virtual = stack.isVirtual();
   int n = stack.getSize();
   ImageStack stack2 = new ImageStack(stack.getWidth(), stack.getHeight());
   for (int i = 1; i <= n; i += factor) {
     if (virtual) IJ.showProgress(i, n);
     stack2.addSlice(stack.getSliceLabel(i), stack.getProcessor(i));
   }
   imp.setStack(null, stack2);
   if (virtual) {
     IJ.showProgress(1.0);
     imp.setTitle(imp.getTitle());
   }
   Calibration cal = imp.getCalibration();
   if (cal.scaled()) cal.pixelDepth *= factor;
 }
Example #21
0
 void createNewStack(ImagePlus imp, ImageProcessor ip) {
   int nSlices = imp.getStackSize();
   int w = imp.getWidth(), h = imp.getHeight();
   ImagePlus imp2 = imp.createImagePlus();
   Rectangle r = ip.getRoi();
   boolean crop = r.width != imp.getWidth() || r.height != imp.getHeight();
   ImageStack stack1 = imp.getStack();
   ImageStack stack2 = new ImageStack(newWidth, newHeight);
   ImageProcessor ip1, ip2;
   int method = interpolationMethod;
   if (w == 1 || h == 1) method = ImageProcessor.NONE;
   for (int i = 1; i <= nSlices; i++) {
     IJ.showStatus("Scale: " + i + "/" + nSlices);
     ip1 = stack1.getProcessor(i);
     String label = stack1.getSliceLabel(i);
     if (crop) {
       ip1.setRoi(r);
       ip1 = ip1.crop();
     }
     ip1.setInterpolationMethod(method);
     ip2 = ip1.resize(newWidth, newHeight, averageWhenDownsizing);
     if (ip2 != null) stack2.addSlice(label, ip2);
     IJ.showProgress(i, nSlices);
   }
   imp2.setStack(title, stack2);
   Calibration cal = imp2.getCalibration();
   if (cal.scaled()) {
     cal.pixelWidth *= 1.0 / xscale;
     cal.pixelHeight *= 1.0 / yscale;
   }
   IJ.showProgress(1.0);
   int[] dim = imp.getDimensions();
   imp2.setDimensions(dim[2], dim[3], dim[4]);
   if (imp.isComposite()) {
     imp2 = new CompositeImage(imp2, ((CompositeImage) imp).getMode());
     ((CompositeImage) imp2).copyLuts(imp);
   }
   if (imp.isHyperStack()) imp2.setOpenAsHyperStack(true);
   if (newDepth > 0 && newDepth != oldDepth)
     imp2 = (new Resizer()).zScale(imp2, newDepth, interpolationMethod);
   if (imp2 != null) {
     imp2.show();
     imp2.changes = true;
   }
 }
Example #22
0
 public ImageStack makeStack(ImageProcessor ip, int w, int h, int b) {
   int stackSize = w * h;
   int width = ip.getWidth() / w;
   int height = ip.getHeight() / h;
   ImageStack stack = new ImageStack(width, height);
   for (int y = 0; y < h; y++)
     for (int x = 0; x < w; x++) {
       ip.setRoi(x * width, y * height, width, height);
       stack.addSlice(null, ip.crop());
     }
   if (b > 0) {
     int cropwidth = width - b - b / 2;
     int cropheight = height - b - b / 2;
     StackProcessor sp = new StackProcessor(stack, ip);
     stack = sp.crop(b, b, cropwidth, cropheight);
   }
   return stack;
 }
Example #23
0
 public void doHyperStackProjection(boolean allTimeFrames) {
   int start = startSlice;
   int stop = stopSlice;
   int firstFrame = 1;
   int lastFrame = imp.getNFrames();
   if (!allTimeFrames) firstFrame = lastFrame = imp.getFrame();
   ImageStack stack = new ImageStack(imp.getWidth(), imp.getHeight());
   int channels = imp.getNChannels();
   int slices = imp.getNSlices();
   if (slices == 1) {
     slices = imp.getNFrames();
     firstFrame = lastFrame = 1;
   }
   int frames = lastFrame - firstFrame + 1;
   increment = channels;
   boolean rgb = imp.getBitDepth() == 24;
   for (int frame = firstFrame; frame <= lastFrame; frame++) {
     for (int channel = 1; channel <= channels; channel++) {
       startSlice = (frame - 1) * channels * slices + (start - 1) * channels + channel;
       stopSlice = (frame - 1) * channels * slices + (stop - 1) * channels + channel;
       if (rgb) doHSRGBProjection(imp);
       else doProjection();
       stack.addSlice(null, projImage.getProcessor());
     }
   }
   projImage = new ImagePlus(makeTitle(), stack);
   projImage.setDimensions(channels, 1, frames);
   if (channels > 1) {
     projImage = new CompositeImage(projImage, 0);
     ((CompositeImage) projImage).copyLuts(imp);
     if (method == SUM_METHOD || method == SD_METHOD)
       ((CompositeImage) projImage).resetDisplayRanges();
   }
   if (frames > 1) projImage.setOpenAsHyperStack(true);
   Overlay overlay = imp.getOverlay();
   if (overlay != null) {
     startSlice = start;
     stopSlice = stop;
     if (imp.getType() == ImagePlus.COLOR_RGB)
       projImage.setOverlay(projectRGBHyperStackRois(overlay));
     else projImage.setOverlay(projectHyperStackRois(overlay));
   }
   IJ.showProgress(1, 1);
 }
Example #24
0
 // extract range of slices
 ImagePlus stackRange(ImagePlus imp, int first, int last, int inc, String title) throws Exception {
   ImageStack stack = imp.getStack();
   ImageStack stack2 = null;
   Roi roi = imp.getRoi();
   for (int i = first, j = 0; i <= last; i += inc) {
     // IJ.log(first+" "+last+" "+inc+" "+i);
     IJ.showProgress(i - first, last - first);
     int currSlice = i - j;
     ImageProcessor ip2 = stack.getProcessor(currSlice);
     // ip2.setRoi(roi);
     // ip2 = ip2.crop();
     if (stack2 == null) stack2 = new ImageStack(ip2.getWidth(), ip2.getHeight());
     stack2.addSlice(stack.getSliceLabel(currSlice), ip2);
   }
   ImagePlus substack = imp.createImagePlus();
   substack.setStack(title, stack2);
   substack.setCalibration(imp.getCalibration());
   return substack;
 }
  public ImageStack expandStack(ImageStack stackOld, int wNew, int hNew, int xOff, int yOff) {
    int nFrames = stackOld.getSize();
    ImageProcessor ipOld = stackOld.getProcessor(1);
    java.awt.Color colorBack = Toolbar.getBackgroundColor();

    ImageStack stackNew = new ImageStack(wNew, hNew, stackOld.getColorModel());
    ImageProcessor ipNew;

    for (int i = 1; i <= nFrames; i++) {
      IJ.showProgress((double) i / nFrames);
      ipNew = ipOld.createProcessor(wNew, hNew);
      if (zeroFill) ipNew.setValue(0.0);
      else ipNew.setColor(colorBack);
      ipNew.fill();
      ipNew.insert(stackOld.getProcessor(i), xOff, yOff);
      stackNew.addSlice(stackOld.getSliceLabel(i), ipNew);
    }
    return stackNew;
  }
Example #26
0
 ImagePlus duplicateStack(ImagePlus img1) {
   ImageStack stack1 = img1.getStack();
   int width = stack1.getWidth();
   int height = stack1.getHeight();
   int n = stack1.getSize();
   ImageStack stack2 = img1.createEmptyStack();
   try {
     for (int i = 1; i <= n; i++) {
       ImageProcessor ip1 = stack1.getProcessor(i);
       ip1.resetRoi();
       ImageProcessor ip2 = ip1.crop();
       stack2.addSlice(stack1.getSliceLabel(i), ip2);
     }
   } catch (OutOfMemoryError e) {
     stack2.trim();
     stack2 = null;
     return null;
   }
   return new ImagePlus("Duplicate", stack2);
 }
Example #27
0
  public static ImagePlus asImagePlus(
      String title, double[] data, int width, int height, int nSlices, int nFrames, int nChannels) {
    ImageStack imageStack = new ImageStack(width, height);
    FloatProcessor ip = null;
    for (int i = 0; i < nSlices * nFrames * nChannels; i++) {
      ip = new FloatProcessor(width, height);
      for (int iX = 0; iX < width; iX++) {
        for (int iY = 0; iY < height; iY++) {
          ip.putPixelValue(iX, iY, data[i * height * width + iY * width + iX]);
        }
      }
      imageStack.addSlice(ip);
    }

    ImagePlus resImp = new ImagePlus(title, imageStack);
    resImp.setOpenAsHyperStack(true);
    resImp.setDimensions(nChannels, nSlices, nFrames);

    return resImp;
  }
  public void runLipschitz(ImageProcessor ip) {
    if (IJ.escapePressed()) return;
    breaked = false;
    Date d1, d2;
    d1 = new Date();

    IJ.showStatus("Initializing...");
    m_stack_out = m_imp.createEmptyStack();
    ImagePlus imp2 = null;

    for (int i = 0; ((i < m_scount) && (!breaked)); i++) {
      if (m_scount > 1) {
        ip = m_stack.getProcessor(i + 1);
      }
      iptmp = ip.createProcessor(ImageWidth, ImageHeight);
      iptmp.copyBits(ip, 0, 0, Blitter.COPY);

      IJ.showStatus("Filtering " + (i + 1) + "/" + m_scount + " slice.");

      Lipschitz2D(iptmp);

      m_stack_out.addSlice(m_imp.getShortTitle() + " " + (i + 1) + "/" + m_scount, iptmp);

      if (breaked = IJ.escapePressed()) IJ.beep();
    }

    imp2 =
        new ImagePlus(
            m_imp.getShortTitle()
                + " Filtered (Lipschitz) Slope:"
                + m_Slope
                + " "
                + ((m_Down) ? " -Down" : " ")
                + " "
                + ((m_TopHat) ? " -TopHat" : " ")
                + ((breaked) ? " -INTERUPTED" : ""),
            m_stack_out);
    imp2.show();
    imp2.updateAndDraw();
    IJ.showProgress(1.0);
  } // end of 'runLipschitz' method
Example #29
0
  /* Replaces all sample index in imp with neighbor sample*/
  public static ImagePlus ReplaceSlices(ImagePlus imp, ArrayList<Integer> samples) {
    ImageStack ims = imp.getStack();
    Iterator itr = samples.iterator();
    while (itr.hasNext()) {
      Integer k = (Integer) itr.next();
      if (k == 0) {
        continue;
      }
      ImageProcessor ip_slice = ims.getProcessor(k);
      //            double value_1 = getMeanOfSlice(ims.getProcessor(k-1));
      //            double value_2 = getMeanOfSlice(ims.getProcessor(k));
      //            double value_3 = getMeanOfSlice(ims.getProcessor(k+1));
      //            double value_4 = getMeanOfSlice(ims.getProcessor(k+2));

      // System.out.print("Replacing "+ value_ori+"with " +value_re );
      ims.addSlice("", ip_slice, k);
      ims.deleteSlice(k + 2);
    }
    imp.setStack(ims);
    return imp;
  }
Example #30
0
 void createNewStack(ImagePlus imp, ImageProcessor ip) {
   Rectangle r = ip.getRoi();
   boolean crop = r.width != imp.getWidth() || r.height != imp.getHeight();
   int nSlices = imp.getStackSize();
   ImageStack stack1 = imp.getStack();
   ImageStack stack2 = new ImageStack(newWidth, newHeight);
   ImageProcessor ip1, ip2;
   boolean interp = interpolate;
   if (imp.getWidth() == 1 || imp.getHeight() == 1) interp = false;
   for (int i = 1; i <= nSlices; i++) {
     IJ.showStatus("Scale: " + i + "/" + nSlices);
     ip1 = stack1.getProcessor(i);
     String label = stack1.getSliceLabel(i);
     if (crop) {
       ip1.setRoi(r);
       ip1 = ip1.crop();
     }
     ip1.setInterpolate(interp);
     ip2 = ip1.resize(newWidth, newHeight);
     if (ip2 != null) stack2.addSlice(label, ip2);
     IJ.showProgress(i, nSlices);
   }
   ImagePlus imp2 = imp.createImagePlus();
   imp2.setStack(title, stack2);
   Calibration cal = imp2.getCalibration();
   if (cal.scaled()) {
     cal.pixelWidth *= 1.0 / xscale;
     cal.pixelHeight *= 1.0 / yscale;
   }
   int[] dim = imp.getDimensions();
   imp2.setDimensions(dim[2], dim[3], dim[4]);
   IJ.showProgress(1.0);
   if (imp.isComposite()) {
     imp2 = new CompositeImage(imp2, 0);
     ((CompositeImage) imp2).copyLuts(imp);
   }
   if (imp.isHyperStack()) imp2.setOpenAsHyperStack(true);
   imp2.show();
   imp2.changes = true;
 }