private ImagePlus createBinary(ImagePlus image, int value) { int w = image.getWidth(), h = image.getWidth(); int d = image.getStackSize(); ImageStack stack = new ImageStack(w, h); for(int z = 0; z < d; z++) { byte[] f = new byte[w*h]; byte[] p = (byte[])image.getStack() .getProcessor(z+1).getPixels(); for(int i = 0; i < w*h; i++) f[i] = (int)(p[i]&0xff) == value ? (byte)255 : 0; stack.addSlice("", new ByteProcessor(w, h, f, null)); } return new ImagePlus("Label_" + (int)(value&0xff), stack); }
private static ImageStack sortStack(ImageStack stack, String[] strings) { ImageProcessor ip = stack.getProcessor(1); ImageStack stack2 = new ImageStack(ip.getWidth(), ip.getHeight(), ip.getColorModel()); for (int i=0; i<stack.getSize(); i++) { int slice = (int)Tools.parseDouble(strings[i].substring(strings[i].length()-MAX_DIGITS), 0.0); if (slice==0) return null; stack2.addSlice(sliceLabels[slice-1], stack.getPixels(slice)); } stack2.update(stack.getProcessor(1)); return stack2; }
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); }
public ImageStack makeSubStack(ImageStack stack, int beginSlice, int endSlice) { ImageStack newStack = new ImageStack(stack.getWidth(), stack.getHeight(), stack.getColorModel()); for(int i = beginSlice; i < endSlice; ++i) { newStack.addSlice(stack.getProcessor(i+1)); } return newStack; }
/** Creates a float version of this stack. */ public ImageStack convertToFloat() { ImageStack stack2 = new ImageStack(width, height, getColorModel()); for (int i=1; i<=getSize(); i++) { ImageProcessor ip2 = this.getProcessor(i); ip2 = ip2.convertToFloat(); stack2.addSlice(this.getSliceLabel(i), ip2); } return stack2; }
/** * bag class for getting the result of the loaded classifier */ private static class LoadedClassifier { private AbstractClassifier newClassifier = null; private Instances newHeader = null; }
public void run(ImagePlus image, Roi[] rois) { w = image.getWidth(); h = image.getHeight(); ImageStack stack = new ImageStack(w, h); int firstIndex = -1, lastIndex = -1; for(int i = 1; i < rois.length; i++) { IJ.error("There must be at least one selection in order to interpolate."); return; ByteProcessor bp = new ByteProcessor(w, h); if(rois[i] != null) { bp.copyBits(rois[i].getMask(), rois[i].getBounds().x, rois[i].getBounds().y, ij.process.Blitter.ADD); stack.addSlice("", bp); ImagePlus roiImage = new ImagePlus("bla", stack); ImageProcessor bp = stack.getProcessor(1); stack.deleteSlice(1); int threshold = 255; bp.setThreshold(threshold, threshold, ImageProcessor.NO_LUT_UPDATE); ts.run(bp); rois[i] = roiImage.getRoi();
void sixteenBitsToRGB(ImagePlus imp) { Roi roi = imp.getRoi(); int width, height; Rectangle r; if (roi!=null) { r = roi.getBounds(); width = r.width; height = r.height; } else r = new Rectangle(0,0,imp.getWidth(),imp.getHeight()); ImageProcessor ip; ImageStack stack1 = imp.getStack(); ImageStack stack2 = new ImageStack(r.width, r.height); for (int i=1; i<=stack1.getSize(); i++) { ip = stack1.getProcessor(i); ip.setRoi(r); ImageProcessor ip2 = ip.crop(); ip2 = ip2.convertToByte(true); stack2.addSlice(null, ip2); } ImagePlus imp2 = imp.createImagePlus(); imp2.setStack(imp.getTitle()+" (RGB)", stack2); ImageConverter ic = new ImageConverter(imp2); ic.convertRGBStackToRGB(); imp2.show(); }
public static ImagePlus rebin(ImagePlus imp, float min, float max, int nbins) { float delta = (max - min) / nbins; int w = imp.getWidth(), h = imp.getHeight(); int d = imp.getStackSize(); ImageStack res = new ImageStack(w, h); for(int z = 0; z < d; z++) { float[] f = (float[])imp.getStack().getProcessor(z+1). getPixels(); byte[] b = new byte[w*h]; for(int i = 0; i < w*h; i++) { b[i] = (byte)((f[i] - min) / delta); } res.addSlice("", new ByteProcessor(w, h, b, null)); } ImagePlus result = new ImagePlus("Rebinned", res); result.setCalibration(imp.getCalibration()); return result; }
public void makeTransition(ImagePlus imp, int from, int num) { if(from > imp.getStackSize()) { IJ.error("Need a following slice to which to transit."); return; } num++; // so that really num slices are added ImageStack stack = imp.getStack(); int[] before = (int[])(stack.getProcessor(from). convertToRGB().getPixels()); int[] after = (int[])(stack.getProcessor(from+1). convertToRGB().getPixels()); for(int z = 1; z < num; z++) { ColorProcessor bp = new ColorProcessor( stack.getWidth(), stack.getHeight()); int[] pixels = (int[])bp.getPixels(); double dp = z; double dn = num - z; for(int i = 0; i < pixels.length; i++) { pixels[i] = interpolate( before[i], dp, after[i], dn); } new ImagePlus("slice + " + z, bp).show(); stack.addSlice("", bp, from + z - 1); } }
private void applyRGBStack(ImagePlus imp) { double min = imp.getDisplayRangeMin(); double max = imp.getDisplayRangeMax(); if (IJ.debugMode) IJ.log("applyRGBStack: "+min+"-"+max); int current = imp.getCurrentSlice(); int n = imp.getStackSize(); if (!IJ.showMessageWithCancel("Update Entire Stack?", "Apply brightness and contrast settings\n"+ "to all "+n+" slices in the stack?\n \n"+ return; ImageProcessor mask = imp.getMask(); Rectangle roi = imp.getRoi()!=null?imp.getRoi().getBounds():null; ImageStack stack = imp.getStack(); for (int i=1; i<=n; i++) { IJ.showProgress(i, n); IJ.showStatus(i+"/"+n); if (i!=current) { ImageProcessor ip = stack.getProcessor(i); ip.setRoi(roi); if (mask!=null) ip.snapshot(); if (channels!=7) ((ColorProcessor)ip).setMinAndMax(min, max, channels); else ip.setMinAndMax(min, max); if (mask!=null) ip.reset(mask);
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)); }
if (imp.getStackSize() > 1) ipp = imp.getStack().getProcessor(i+1); else ipp = imp.getProcessor(); float [] a = null; try if (imp.getRoi() != null) Rectangle roiRect = imp.getRoi().getBoundingRect(); width = roiRect.width; height = roiRect.height; x = roiRect.x; y = roiRect.y; ipp.setRoi(x, y, width, height); ipp = ipp.crop(); a = getFloatPixels(ipp.getPixels()); catch (Exception e) { CharArrayWriter c = new CharArrayWriter(); e.printStackTrace(new PrintWriter(c)); IJ.write(c.toString()); } return a;
public ImagePlus call(){ final ImagePlus hsb = originalImage.duplicate(); ImageConverter ic = new ImageConverter( hsb ); ic.convertToHSB(); ImageStack is = new ImageStack(originalImage.getWidth(), originalImage.getHeight()); for(int n=1; n<=hsb.getImageStackSize(); n++) is.addSlice(hsb.getImageStack().getSliceLabel(n), hsb.getImageStack().getProcessor(n).convertToRGB()); return new ImagePlus ("HSB", is); } };
/** Flattens Overlay 'overlay' on slice 'slice' corresponding to * coordinates 'z' and 't' in RGB-HyperStack 'stack' */ private void flattenImage(ImageStack stack, int slice, Overlay overlay, boolean showAll, int z, int t) { ImageProcessor ips = stack.getProcessor(slice); ImagePlus imp1 = new ImagePlus("temp", ips); int w = imp1.getWidth(); int h = imp1.getHeight(); for (int i=0; i<overlay.size(); i++) { Roi r = overlay.get(i); int cPos = r.getCPosition();// 0 or 1 (RGB-HyperStack) int zPos = r.getZPosition(); int tPos = r.getTPosition(); if (!((cPos==1 || cPos==0) && (zPos==z || zPos==0) && (tPos==t || tPos==0) || showAll)) r.setLocation(w, h); } imp1.setOverlay(overlay); ImagePlus imp2 = imp1.flatten(); stack.setPixels(imp2.getProcessor().getPixels(), slice); }
/** Converts a 3-slice (hue, saturation, brightness) 8-bit stack to RGB. */ public void convertHSBToRGB() { if (imp.getStackSize()!=3) throw new IllegalArgumentException("3-slice 8-bit stack required"); ImageStack stack = imp.getStack(); byte[] H = (byte[])stack.getPixels(1); byte[] S = (byte[])stack.getPixels(2); byte[] B = (byte[])stack.getPixels(3); int width = imp.getWidth(); int height = imp.getHeight(); imp.trimProcessor(); ColorProcessor cp = new ColorProcessor(width, height); cp.setHSB(H, S, B); imp.setImage(cp.createImage()); imp.killStack(); if (IJ.isLinux()) imp.setTitle(imp.getTitle()); }
/** Flattens Overlay 'overlay' on slice 'slice' of ImageStack 'stack'. * Copied from OverlayCommands by Marcel Boeglin 2014.01.08. */ private void flattenImage(ImageStack stack, int slice, Overlay overlay, boolean showAll) { ImageProcessor ips = stack.getProcessor(slice); ImagePlus imp1 = new ImagePlus("temp", ips); int w = imp1.getWidth(); int h = imp1.getHeight(); for (int i=0; i<overlay.size(); i++) { Roi r = overlay.get(i); int roiPosition = r.getPosition(); //IJ.log(slice+" "+i+" "+roiPosition+" "+showAll+" "+overlay.size()); if (!(roiPosition==0 || roiPosition==slice || showAll)) r.setLocation(w, h); } imp1.setOverlay(overlay); ImagePlus imp2 = imp1.flatten(); stack.setPixels(imp2.getProcessor().getPixels(), slice); }
ImageProcessor getSlice(ImagePlus imp, double x1, double y1, double x2, double y2, String status) { Roi roi = imp.getRoi(); int roiType = roi!=null?roi.getType():0; ImageStack stack = imp.getStack(); int stackSize = stack.getSize(); ImageProcessor ip,ip2=null; float[] line = null; for (int i=0; i<stackSize; i++) { ip = stack.getProcessor(i+1); line = getLine(ip, x1, y1, x2, y2, line); if (i==0) ip2 = ip.createProcessor(line.length, stackSize); putRow(ip2, 0, i, line, line.length); if (status!=null) IJ.showStatus("Slicing: "+status +i+"/"+stackSize); } Calibration cal = imp.getCalibration(); double zSpacing = inputZSpacing/cal.pixelWidth; if (zSpacing!=1.0) { ip2.setInterpolate(true); ip2 = ip2.resize(line.length, (int)(stackSize*zSpacing)); } return ip2; }
/** This method has been replaced by IJ.createHyperStack(). */ public ImagePlus createHyperStack(String title, int channels, int slices, int frames, int bitDepth) { int size = channels*slices*frames; ImageStack stack2 = new ImageStack(width, height, size); // create empty stack ImageProcessor ip2 = null; switch (bitDepth) { case 8: ip2 = new ByteProcessor(width, height); break; case 16: ip2 = new ShortProcessor(width, height); break; case 24: ip2 = new ColorProcessor(width, height); break; case 32: ip2 = new FloatProcessor(width, height); break; default: throw new IllegalArgumentException("Invalid bit depth"); } stack2.setPixels(ip2.getPixels(), 1); // can't create ImagePlus will null 1st image ImagePlus imp2 = new ImagePlus(title, stack2); stack2.setPixels(null, 1); imp2.setDimensions(channels, slices, frames); imp2.setCalibration(getCalibration()); imp2.setOpenAsHyperStack(true); return imp2; }
/** Converts the stack to 8-bits indexed color. 'nColors' must be greater than 1 and less than or equal to 256. */ public void convertToIndexedColor(int nColors) { if (type!=ImagePlus.COLOR_RGB) throw new IllegalArgumentException("RGB stack required"); ImageStack stack = imp.getStack(); int size = stack.getSize(); ImageProcessor montage = new ColorProcessor(width*size, height); for (int i=0; i<size; i++) montage.insert(stack.getProcessor(i+1), i*width, 0); MedianCut mc = new MedianCut((ColorProcessor)montage); montage = mc.convertToByte(nColors); ImageStack stack2 = new ImageStack(width, height); for (int i=0; i<size; i++) { montage.setRoi(i*width, 0, width, height); stack2.addSlice(null, montage.crop()); } imp.setStack(null, stack2); }