import ij.ImagePlus; import ij.io.FileSaver; import ij.process.ImageProcessor; ImagePlus imgPlus = new ImagePlus("path-to-sample.jpg"); ImageProcessor imgProcessor = imgPlus.getProcessor(); imgProcessor.invert(); FileSaver fs = new FileSaver(imgPlus); fs.saveAsJpeg("path-to-inverted.jpg");
import io.scif.gui.AWTImageTools; ... final ImagePlus imp = IJ.openImage("http://imagej.net/images/ct.dcm.zip"); // get pixels array reference final short[] pix = (short[]) imp.getProcessor().getPixels(); final int w = imp.getWidth(); final int h = imp.getHeight(); final boolean signed = imp.getCalibration().isSigned16Bit(); if (signed) { // adjust raw pixel values for (int i=0; i<pix.length; i++) { pix[i] -= 32768; } } // convert to BufferedImage final BufferedImage image = AWTImageTools.makeImage(pix, w, h, signed);
protected void setSurfacePlotMaskImage(ImagePlus imp){ widthMaskImage = imp.getWidth(); heightMaskImage = imp.getHeight(); pixelsMaskImage = (byte []) imp.getProcessor().getPixels(); }
private void writeFourChannelsWithAlpha(ImagePlus imp, String path) throws Exception { ImageStack stack = imp.getStack(); int w=imp.getWidth(), h=imp.getHeight(); ImagePlus imp2 = new ImagePlus("", new ColorProcessor(w,h)); ColorProcessor cp = (ColorProcessor)imp2.getProcessor(); for (int channel=1; channel<=4; channel++) cp.setChannel(channel, (ByteProcessor)stack.getProcessor(channel)); BufferedImage bi = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB); WritableRaster raster = bi.getRaster(); raster.setDataElements(0, 0, w, h, cp.getPixels()); ImageIO.write(bi, "png", new File(path)); }
private static final int getAutoThreshold(final ImagePlus imp) { final int[] histo = new int[256]; final int d = imp.getStackSize(); for (int z = 0; z < d; z++) { final byte[] p = (byte[]) imp.getStack().getPixels(z + 1); for (int i = 0; i < p.length; i++) { histo[(p[i] & 0xff)]++; } } return imp.getProcessor().getAutoThreshold(histo); }
public static final ImagePlus combineImages(ImagePlus image1, ImagePlus image2, Operation op) { String newName = "result of " + image1.getShortTitle(); if (image1.getStackSize() == 1) { ImageProcessor result = combineImages(image1.getProcessor(), image2.getProcessor(), op); return new ImagePlus(newName, result); } else { ImageStack result = combineImages(image1.getStack(), image2.getStack(), op); return new ImagePlus(newName, result); } }
/** Converts this ImagePlus to RGB. */ public void convertToRGB() { if (imp.getBitDepth()==24) return; if (imp.getStackSize()>1) { new StackConverter(imp).convertToRGB(); return; } ImageProcessor ip = imp.getProcessor(); imp.setProcessor(null, ip.convertToRGB()); imp.setCalibration(imp.getCalibration()); //update calibration }
public void assignImage(final ImagePlus imp) { if (imp == null) return; setSpatialCalibration(imp.getCalibration()); setIdentifier(imp.getTitle()); setNDimensions(imp.getNDimensions()); if (imp.getProcessor().isBinary()) properties.setProperty(KEY_SOURCE, SRC_IMG_BINARY); else if (imp.getProcessor().getMinThreshold() != ImageProcessor.NO_THRESHOLD) properties.setProperty(KEY_SOURCE, SRC_IMG_THRESH); else properties.setProperty(KEY_SOURCE, SRC_IMG); }
public static void crop(ImagePlus image, Rectangle rect) { if (image.getWidth() == rect.width && image.getHeight() == rect.height) return; image.changes = true; if (image.getStackSize() == 1) { image.setProcessor(image.getTitle(), crop(image.getProcessor(), rect)); return; } ImageStack stack = new ImageStack(rect.width, rect.height); ImageStack orig = image.getStack(); for (int i = 1; i < orig.getSize(); i++) stack.addSlice("", crop(orig.getProcessor(i), rect)); image.setStack(image.getTitle(), stack); } }
public void run(String arg) { OpenDialog od = new OpenDialog("Open Analyze...", arg); String directory = od.getDirectory(); String name = od.getFileName(); if (name==null) return; IJ.showStatus("Opening: " + directory + name); FileInfo fi = load(directory, name); FileOpener fo = new FileOpener(fi); ImagePlus imp = fo.open(false); if (imp==null) return; ImageStack stack = imp.getStack(); for (int i=1; i<=stack.getSize(); i++) { ImageProcessor ip = stack.getProcessor(i); ip.flipVertical(); } if (imp.getStackSize()>1) setStack(name, stack); else setProcessor(name, imp.getProcessor()); setCalibration(imp.getCalibration()); setFileInfo(fi); // needed for revert if (arg.equals("")) show(); }
private ImagePlus openFFT(ImagePlus imp) { ImageProcessor ip = imp.getProcessor(); FHT fht = new FHT(ip, true); ImageProcessor ps = fht.getPowerSpectrum(); ImagePlus imp2 = new ImagePlus(imp.getTitle(), ps); imp2.setProperty("FHT", fht); imp2.setProperty("Info", imp.getInfoProperty()); fht.originalWidth = (int)imp2.getNumericProperty("width"); fht.originalHeight = (int)imp2.getNumericProperty("height"); fht.originalBitDepth = (int)imp2.getNumericProperty("bitdepth"); fht.originalColorModel = ip.getColorModel(); imp2.setCalibration(imp.getCalibration()); return imp2; }
public static final ImagePlus geodesicDistanceMap(ImagePlus markerPlus, ImagePlus maskPlus) { ImagePlus result = null; String newName = markerPlus.getShortTitle() + "-distMap"; if (markerPlus.getStackSize() == 1 && maskPlus.getStackSize() == 1) { ImageProcessor distMap = geodesicDistanceMap(markerPlus.getProcessor(), maskPlus.getProcessor()); result = new ImagePlus(newName, distMap); } result.copyScale(markerPlus); return result; }
static void transfer(final ImagePlus src, final ImagePlus dst) { // Calibration: dst.setCalibration(src.getCalibration()); // Color model: final ColorModel cm = src.getProcessor().getColorModel(); dst.getProcessor().setColorModel(cm); dst.getStack().setColorModel(cm); // Min and max displaying: dst.setDisplayRange(src.getDisplayRangeMin(),src.getDisplayRangeMax()); }
/** * Computes the maximum value in the input image or stack, in order to * initialize colormap with the appropriate number of colors. */ private final static int computeMaxLabel(ImagePlus imagePlus) { if (imagePlus.getImageStackSize() == 1) { return computeMaxLabel(imagePlus.getProcessor()); } else { return computeMaxLabel(imagePlus.getStack()); } }
public void run(String arg) { convertToMask = arg.equals("mask"); if (arg.equals("skip") || convertToMask) showLegacyDialog = false; ImagePlus imp = IJ.getImage(); if (imp.getStackSize()==1) { if (!convertToMask && imp.getProcessor().isBinary()) { setThreshold(imp); return; } Undo.setup(Undo.TRANSFORM, imp); applyThreshold(imp, false); } else convertStack(imp); }
/** * Read mask from file. * An error is shown if the file read is not of the same size as the * previous mask. * * @param filename name of the mask file */ public void readFile(String filename) { ImagePlus aux = new ImagePlus(filename); if (aux.getWidth()!=width || aux.getHeight()!=height) IJ.error("Mask in file is not of the expected size"); ImageProcessor ip = aux.getProcessor(); int k=0; for (int y = 0; (y < height); y++) for (int x = 0; (x < width); x++, k++) if (ip.getPixelValue(x,y)!=0) mask[k]=true; else mask[k]=false; }
void packrgb(ImagePlus image){ int len = image.getWidth()*image.getHeight(); ImageProcessor imp = image.getProcessor(); int[] pix = (int[]) imp.getPixels(); pixels = new byte[len*3]; //pack pixels for(int i=0; i<len; i++){ int k=i*3; pixels[k+2] = (byte)((pix[i] & 0xff0000)>>16); //red pixels[k+1] = (byte)((pix[i] & 0x00ff00)>>8); //green pixels[k] = (byte)(pix[i] & 0x0000ff); //blue } }
/** * Checks if input image is color. * * @param imagePlus * input instance of ImagePlus * @return true if input image contains color processor(s) */ private boolean isColorImage(ImagePlus imagePlus) { if (imagePlus.getStackSize() == 1) { return imagePlus.getProcessor() instanceof ColorProcessor; } else { return imagePlus.getStack().getProcessor(1) instanceof ColorProcessor; } }
File inputFile = new File("someImage.png"); Image someImage = null; try { someImage = ImageIO.read(inputFile); } catch (IOException e) { e.printStackTrace(); } ImagePlus imagePlus = new ImagePlus(); imagePlus.setImage(someImage); ColorProcessor processor = (ColorProcessor) imagePlus.getProcessor();
ImageStatistics getRedirectStats(int measurements, Roi roi) { ImagePlus redirectImp = getRedirectImageOrStack(imp); if (redirectImp==null) return null; ImageProcessor ip = redirectImp.getProcessor(); if (imp.getTitle().equals("mask") && imp.getBitDepth()==8) { ip.setMask(imp.getProcessor()); ip.setRoi(0, 0, imp.getWidth(), imp.getHeight()); } else ip.setRoi(roi); return ImageStatistics.getStatistics(ip, measurements, redirectImp.getCalibration()); }