/**
   * Return an ImageRecord containing the images pixel dimensions.
   *
   * @param file absolute file path to image
   * @return ImageRecord containing the images pixel dimensions
   */
  public static ImageRecord getImageDimensions(final String file) {
    if (LOGGER.isDebugEnabled()) {
      LOGGER.debug("Getting image dimensions from: {}", file);
    }

    final ImageRecord dim = new ImageRecord(file);
    final Opener o = new Opener();
    final ImagePlus imp = o.openImage(file);
    if (imp == null) {
      return null;
    }
    ImageProcessor ip = imp.getProcessor();
    final int width = ip.getWidth();
    final int height = ip.getHeight();

    if (LOGGER.isDebugEnabled()) {
      LOGGER.debug(
          "{} (width: {} | height: {})", file, Integer.toString(width), Integer.toString(height));
    }

    dim.setWidth(width);
    dim.setHeight(height);
    ip = null;
    return dim;
  }
示例#2
0
  public static void main(String[] args) throws IOException, WriteException {
    // TODO code application logic here
    int cellNum = 0;
    double[] cTCF = new double[30];
    ImagePlus img = null;
    Opener op = new Opener();

    for (int i = 0; i < 30; i++) {
      String num = Integer.toString(i);
      img = op.openImage("E:/Photobleaching/Target/1/" + num + ".tif");

      ImageProcessor im = img.getProcessor().duplicate();
      // set region of interest for target cell
      im.setRoi(342, 668, 36, 36);
      double cellMean = im.getStatistics().mean;
      double cellArea = im.getStatistics().area;
      double cellIntensity = cellMean * cellArea;
      // --------------------------------------------------------------------------
      // calculate Integrated Density of background for target cell area
      // --------------------------------------------------------------------------
      ImageProcessor background1 = img.getProcessor().duplicate();
      // set region of interest for background
      background1.setRoi(341, 591, 21, 20);
      double backgroundMean1 = background1.getStatistics().mean;
      double backgroundIntensity1 = backgroundMean1 * cellArea;

      ImageProcessor background2 = img.getProcessor().duplicate();
      // set region of interest for background
      background2.setRoi(383, 641, 20, 20);
      double backgroundMean2 = background2.getStatistics().mean;
      double backgroundIntensity2 = backgroundMean2 * cellArea;

      double backgroundIntensity = (backgroundIntensity1 + backgroundIntensity2) / 2;
      // calculate corrected total cell fluorescence (CTCF)
      cTCF[cellNum] = cellIntensity - backgroundIntensity;
      cellNum = cellNum + 1;
    }

    WriteToExcel write = new WriteToExcel();
    write.write(1, cellNum, cTCF);
  }
 public Image getImage(JCRNodeWrapper node) throws IOException, RepositoryException {
   File tmp = null;
   OutputStream os = null;
   try {
     tmp = File.createTempFile("image", null);
     Node contentNode = node.getNode(Constants.JCR_CONTENT);
     os = new BufferedOutputStream(new FileOutputStream(tmp));
     InputStream is = contentNode.getProperty(Constants.JCR_DATA).getBinary().getStream();
     try {
       IOUtils.copy(is, os);
     } finally {
       IOUtils.closeQuietly(os);
       IOUtils.closeQuietly(is);
     }
     Opener op = new Opener();
     ImagePlus ip = op.openImage(tmp.getPath());
     return new ImageJImage(node.getPath(), ip, op.getFileType(tmp.getPath()));
   } finally {
     IOUtils.closeQuietly(os);
     FileUtils.deleteQuietly(tmp);
   }
 }
示例#4
0
  /** Restores original disk or network version of image. */
  public void revertToSaved(ImagePlus imp) {
    Image img;
    ProgressBar pb = IJ.getInstance().getProgressBar();
    ImageProcessor ip;
    String path = fi.directory + fi.fileName;

    if (fi.fileFormat == fi.GIF_OR_JPG) {
      // restore gif or jpg
      img = Toolkit.getDefaultToolkit().createImage(path);
      imp.setImage(img);
      if (imp.getType() == ImagePlus.COLOR_RGB) Opener.convertGrayJpegTo8Bits(imp);
      return;
    }

    if (fi.fileFormat == fi.DICOM) {
      // restore DICOM
      ImagePlus imp2 = (ImagePlus) IJ.runPlugIn("ij.plugin.DICOM", path);
      if (imp2 != null) imp.setProcessor(null, imp2.getProcessor());
      return;
    }

    if (fi.fileFormat == fi.BMP) {
      // restore BMP
      ImagePlus imp2 = (ImagePlus) IJ.runPlugIn("ij.plugin.BMP_Reader", path);
      if (imp2 != null) imp.setProcessor(null, imp2.getProcessor());
      return;
    }

    if (fi.fileFormat == fi.PGM) {
      // restore PGM
      ImagePlus imp2 = (ImagePlus) IJ.runPlugIn("ij.plugin.PGM_Reader", path);
      if (imp2 != null) imp.setProcessor(null, imp2.getProcessor());
      return;
    }

    if (fi.fileFormat == fi.ZIP_ARCHIVE) {
      // restore ".zip" file
      ImagePlus imp2 = (new Opener()).openZip(path);
      if (imp2 != null) imp.setProcessor(null, imp2.getProcessor());
      return;
    }

    // restore PNG or another image opened using ImageIO
    if (fi.fileFormat == fi.IMAGEIO) {
      ImagePlus imp2 = (new Opener()).openUsingImageIO(path);
      if (imp2 != null) imp.setProcessor(null, imp2.getProcessor());
      return;
    }

    if (fi.nImages > 1) return;

    ColorModel cm;
    if (fi.url == null || fi.url.equals("")) IJ.showStatus("Loading: " + path);
    else IJ.showStatus("Loading: " + fi.url + fi.fileName);
    Object pixels = readPixels(fi);
    if (pixels == null) return;
    cm = createColorModel(fi);
    switch (fi.fileType) {
      case FileInfo.GRAY8:
      case FileInfo.COLOR8:
      case FileInfo.BITMAP:
        ip = new ByteProcessor(width, height, (byte[]) pixels, cm);
        imp.setProcessor(null, ip);
        break;
      case FileInfo.GRAY16_SIGNED:
      case FileInfo.GRAY16_UNSIGNED:
      case FileInfo.GRAY12_UNSIGNED:
        ip = new ShortProcessor(width, height, (short[]) pixels, cm);
        imp.setProcessor(null, ip);
        break;
      case FileInfo.GRAY32_INT:
      case FileInfo.GRAY32_FLOAT:
        ip = new FloatProcessor(width, height, (float[]) pixels, cm);
        imp.setProcessor(null, ip);
        break;
      case FileInfo.RGB:
      case FileInfo.BGR:
      case FileInfo.ARGB:
      case FileInfo.ABGR:
      case FileInfo.RGB_PLANAR:
        img =
            Toolkit.getDefaultToolkit()
                .createImage(new MemoryImageSource(width, height, (int[]) pixels, 0, width));
        imp.setImage(img);
        break;
    }
  }
示例#5
0
  public static String createThumbImage(String fileAbsolutePath, String fileSaveAsNameSuffix) {
    try {
      Opener opener = new Opener();
      ImagePlus imp = opener.openImage(fileAbsolutePath);
      ImageProcessor ip = imp.getProcessor();
      StackProcessor sp = new StackProcessor(imp.getStack(), ip);

      int width = imp.getWidth();
      int height = imp.getHeight();

      int cropWidth = 0;
      int cropHeight = 0;
      if (width > height) {
        cropWidth = height;
        cropHeight = height;
      } else {
        cropWidth = width;
        cropHeight = width;
      }
      int x = -1;
      int y = -1;
      if (width == height) {
        x = 0;
        y = 0;
      } else if (width > height) {
        x = (width - height) / 2;
        y = 0;
      } else if (width < height) {
        x = 0;
        y = (height - width) / 2;
      }
      ImageStack croppedStack = sp.crop(x, y, cropWidth, cropHeight);

      imp.setStack(null, croppedStack);

      sp = new StackProcessor(imp.getStack(), imp.getProcessor());

      ImageStack resizedStack = sp.resize(100, 100, true);
      imp.setStack(null, resizedStack);

      File generateSourcePath = new File(fileAbsolutePath);

      StringBuffer filePath =
          new StringBuffer(generateSourcePath.getParentFile().getParentFile().getAbsolutePath());

      File generatePath = new File(filePath.toString() + File.separator + "generate");
      if (!generatePath.exists()) {
        generatePath.mkdirs();
      }
      filePath = new StringBuffer(generateSourcePath.getName());
      filePath.replace(filePath.lastIndexOf("."), filePath.lastIndexOf("."), fileSaveAsNameSuffix);

      String saveAsFilePath = generatePath.getAbsolutePath() + File.separator + filePath.toString();

      IJ.save(imp, saveAsFilePath);
      return saveAsFilePath;
    } catch (Exception e) {
      e.printStackTrace();
    }
    return null;
  }
 public boolean install(String path) {
   boolean isURL = path.contains("://");
   String lcPath = path.toLowerCase();
   if (isURL) path = Opener.updateUrl(path);
   boolean isTool =
       lcPath.endsWith("tool.ijm")
           || lcPath.endsWith("tool.txt")
           || lcPath.endsWith("tool.class")
           || lcPath.endsWith("tool.jar");
   boolean isMacro = lcPath.endsWith(".txt") || lcPath.endsWith(".ijm");
   byte[] data = null;
   String name = path;
   if (isURL) {
     int index = path.lastIndexOf("/");
     if (index != -1 && index <= path.length() - 1) name = path.substring(index + 1);
     data = download(path, name);
   } else {
     File f = new File(path);
     name = f.getName();
     data = download(f);
   }
   if (data == null) return false;
   if (name.endsWith(".txt") && !name.contains("_"))
     name = name.substring(0, name.length() - 4) + ".ijm";
   if (name.endsWith(".zip")) {
     if (!name.contains("_")) {
       IJ.error("Plugin Installer", "No underscore in file name:\n \n  " + name);
       return false;
     }
     name = name.substring(0, name.length() - 4) + ".jar";
   }
   String dir = null;
   boolean isLibrary = name.endsWith(".jar") && !name.contains("_");
   if (isLibrary) {
     dir = Menus.getPlugInsPath() + "jars";
     File f = new File(dir);
     if (!f.exists()) {
       boolean ok = f.mkdir();
       if (!ok) dir = Menus.getPlugInsPath();
     }
   }
   if (isTool) {
     dir = Menus.getPlugInsPath() + "Tools" + File.separator;
     File f = new File(dir);
     if (!f.exists()) {
       boolean ok = f.mkdir();
       if (!ok) dir = null;
     }
     if (dir != null && isMacro) {
       String name2 = getToolName(data);
       if (name2 != null) name = name2;
     }
   }
   if (dir == null) {
     SaveDialog sd =
         new SaveDialog("Save Plugin, Macro or Script...", Menus.getPlugInsPath(), name, null);
     String name2 = sd.getFileName();
     if (name2 == null) return false;
     dir = sd.getDirectory();
   }
   // IJ.log(dir+"   "+Menus.getPlugInsPath());
   if (!savePlugin(new File(dir, name), data)) return false;
   if (name.endsWith(".java")) IJ.runPlugIn("ij.plugin.Compiler", dir + name);
   Menus.updateImageJMenus();
   if (isTool) {
     if (isMacro) IJ.runPlugIn("ij.plugin.Macro_Runner", "Tools/" + name);
     else if (name.endsWith(".class")) {
       name = name.replaceAll("_", " ");
       name = name.substring(0, name.length() - 6);
       IJ.run(name);
     }
   }
   return true;
 }