/** Generate output image whose type is same as input image. */ private ImagePlus makeOutputImage(ImagePlus imp, FloatProcessor fp, int ptype) { int width = imp.getWidth(); int height = imp.getHeight(); float[] pixels = (float[]) fp.getPixels(); ImageProcessor oip = null; // Create output image consistent w/ type of input image. int size = pixels.length; switch (ptype) { case BYTE_TYPE: oip = imp.getProcessor().createProcessor(width, height); byte[] pixels8 = (byte[]) oip.getPixels(); for (int i = 0; i < size; i++) pixels8[i] = (byte) pixels[i]; break; case SHORT_TYPE: oip = imp.getProcessor().createProcessor(width, height); short[] pixels16 = (short[]) oip.getPixels(); for (int i = 0; i < size; i++) pixels16[i] = (short) pixels[i]; break; case FLOAT_TYPE: oip = new FloatProcessor(width, height, pixels, null); break; } // Adjust for display. // Calling this on non-ByteProcessors ensures image // processor is set up to correctly display image. oip.resetMinAndMax(); // Create new image plus object. Don't use // ImagePlus.createImagePlus here because there may be // attributes of input image that are not appropriate for // projection. return new ImagePlus(makeTitle(), oip); }
void Bernsen(ImagePlus imp, int radius, double par1, double par2, boolean doIwhite) { // Bernsen recommends WIN_SIZE = 31 and CONTRAST_THRESHOLD = 15. // 1) Bernsen J. (1986) "Dynamic Thresholding of Grey-Level Images" // Proc. of the 8th Int. Conf. on Pattern Recognition, pp. 1251-1255 // 2) Sezgin M. and Sankur B. (2004) "Survey over Image Thresholding // Techniques and Quantitative Performance Evaluation" Journal of // Electronic Imaging, 13(1): 146-165 // http://citeseer.ist.psu.edu/sezgin04survey.html // Ported to ImageJ plugin from E Celebi's fourier_0.8 routines // This version uses a circular local window, instead of a rectagular one ImagePlus Maximp, Minimp; ImageProcessor ip = imp.getProcessor(), ipMax, ipMin; int contrast_threshold = 15; int local_contrast; int mid_gray; byte object; byte backg; int temp; if (par1 != 0) { IJ.log("Bernsen: changed contrast_threshold from :" + contrast_threshold + " to:" + par1); contrast_threshold = (int) par1; } if (doIwhite) { object = (byte) 0xff; backg = (byte) 0; } else { object = (byte) 0; backg = (byte) 0xff; } Maximp = duplicateImage(ip); ipMax = Maximp.getProcessor(); RankFilters rf = new RankFilters(); rf.rank(ipMax, radius, rf.MAX); // Maximum // Maximp.show(); Minimp = duplicateImage(ip); ipMin = Minimp.getProcessor(); rf.rank(ipMin, radius, rf.MIN); // Minimum // Minimp.show(); byte[] pixels = (byte[]) ip.getPixels(); byte[] max = (byte[]) ipMax.getPixels(); byte[] min = (byte[]) ipMin.getPixels(); for (int i = 0; i < pixels.length; i++) { local_contrast = (int) ((max[i] & 0xff) - (min[i] & 0xff)); mid_gray = (int) ((min[i] & 0xff) + (max[i] & 0xff)) / 2; temp = (int) (pixels[i] & 0x0000ff); if (local_contrast < contrast_threshold) pixels[i] = (mid_gray >= 128) ? object : backg; // Low contrast region else pixels[i] = (temp >= mid_gray) ? object : backg; } // imp.updateAndDraw(); return; }
void createMaskFromThreshold(ImagePlus imp) { ImageProcessor ip = imp.getProcessor(); if (ip.getMinThreshold() == ImageProcessor.NO_THRESHOLD) { IJ.error("Create Mask", "Area selection or thresholded image required"); return; } double t1 = ip.getMinThreshold(); double t2 = ip.getMaxThreshold(); IJ.run("Duplicate...", "title=mask"); ImagePlus imp2 = WindowManager.getCurrentImage(); ImageProcessor ip2 = imp2.getProcessor(); ip2.setThreshold(t1, t2, ImageProcessor.NO_LUT_UPDATE); IJ.run("Convert to Mask"); }
void createEllipse(ImagePlus imp) { IJ.showStatus("Fitting ellipse"); Roi roi = imp.getRoi(); if (roi == null) { noRoi("Fit Ellipse"); return; } if (roi.isLine()) { IJ.error("Fit Ellipse", "\"Fit Ellipse\" does not work with line selections"); return; } ImageProcessor ip = imp.getProcessor(); ip.setRoi(roi); int options = Measurements.CENTROID + Measurements.ELLIPSE; ImageStatistics stats = ImageStatistics.getStatistics(ip, options, null); double dx = stats.major * Math.cos(stats.angle / 180.0 * Math.PI) / 2.0; double dy = -stats.major * Math.sin(stats.angle / 180.0 * Math.PI) / 2.0; double x1 = stats.xCentroid - dx; double x2 = stats.xCentroid + dx; double y1 = stats.yCentroid - dy; double y2 = stats.yCentroid + dy; double aspectRatio = stats.minor / stats.major; imp.killRoi(); imp.setRoi(new EllipseRoi(x1, y1, x2, y2, aspectRatio)); }
/** * Prepare for processing; also called at the very end with argument 'final' to show any newly * created output image. */ public int setup(String arg, ImagePlus imp) { if (arg.equals("final")) { showOutput(); return DONE; } this.imp = imp; // 'arg' is processing type; default is 'EDM' (0) if (arg.equals("watershed")) { processType = WATERSHED; flags += KEEP_THRESHOLD; } else if (arg.equals("points")) processType = UEP; else if (arg.equals("voronoi")) processType = VORONOI; // output type if (processType != WATERSHED) // Watershed always has output BYTE_OVERWRITE=0 outImageType = outputType; // otherwise use the static variable from setOutputType if (outImageType != BYTE_OVERWRITE) flags |= NO_CHANGES; // check image and prepare if (imp != null) { ImageProcessor ip = imp.getProcessor(); if (!ip.isBinary()) { IJ.error("8-bit binary image (0 and 255) required."); return DONE; } ip.resetRoi(); // processing routines assume background=0; image may be otherwise boolean invertedLut = imp.isInvertedLut(); background255 = (invertedLut && Prefs.blackBackground) || (!invertedLut && !Prefs.blackBackground); } return flags; } // public int setup
public int showDialog(ImagePlus imp, String command, PlugInFilterRunner pfr) { if (doOptions) { this.imp = imp; this.pfr = pfr; GenericDialog gd = new GenericDialog("Binary Options"); gd.addNumericField("Iterations (1-" + MAX_ITERATIONS + "):", iterations, 0, 3, ""); gd.addNumericField("Count (1-8):", count, 0, 3, ""); gd.addCheckbox("Black background", Prefs.blackBackground); gd.addCheckbox("Pad edges when eroding", Prefs.padEdges); gd.addChoice("EDM output:", outputTypes, outputTypes[EDM.getOutputType()]); if (imp != null) { gd.addChoice("Do:", operations, operation); gd.addPreviewCheckbox(pfr); gd.addDialogListener(this); previewing = true; } gd.addHelp(IJ.URL + "/docs/menus/process.html#options"); gd.showDialog(); previewing = false; if (gd.wasCanceled()) return DONE; if (imp == null) { // options dialog only, no do/preview dialogItemChanged(gd, null); // read dialog result return DONE; } return operation.equals(NO_OPERATION) ? DONE : IJ.setupDialog(imp, flags); } else { // no dialog, 'arg' is operation type if (!((ByteProcessor) imp.getProcessor()).isBinary()) { IJ.error("8-bit binary (black and white only) image required."); return DONE; } return IJ.setupDialog(imp, flags); } }
public void run(ImageProcessor ip) { if (enlarge && gd.wasOKed()) synchronized (this) { if (!isEnlarged) { enlargeCanvas(); isEnlarged = true; } } if (isEnlarged) { // enlarging may have made the ImageProcessor invalid, also for the parallel // threads int slice = pfr.getSliceNumber(); if (imp.getStackSize() == 1) ip = imp.getProcessor(); else ip = imp.getStack().getProcessor(slice); } ip.setInterpolationMethod(interpolationMethod); if (fillWithBackground) { Color bgc = Toolbar.getBackgroundColor(); if (bitDepth == 8) ip.setBackgroundValue(ip.getBestIndex(bgc)); else if (bitDepth == 24) ip.setBackgroundValue(bgc.getRGB()); } else ip.setBackgroundValue(0); ip.rotate(angle); if (!gd.wasOKed()) drawGridLines(gridLines); if (isEnlarged && imp.getStackSize() == 1) { imp.changes = true; imp.updateAndDraw(); Undo.setup(Undo.COMPOUND_FILTER_DONE, imp); } }
void createMask(ImagePlus imp) { Roi roi = imp.getRoi(); boolean useInvertingLut = Prefs.useInvertingLut; Prefs.useInvertingLut = false; if (roi == null || !(roi.isArea() || roi.getType() == Roi.POINT)) { createMaskFromThreshold(imp); Prefs.useInvertingLut = useInvertingLut; return; } ImagePlus maskImp = null; Frame frame = WindowManager.getFrame("Mask"); if (frame != null && (frame instanceof ImageWindow)) maskImp = ((ImageWindow) frame).getImagePlus(); if (maskImp == null) { ImageProcessor ip = new ByteProcessor(imp.getWidth(), imp.getHeight()); if (!Prefs.blackBackground) ip.invertLut(); maskImp = new ImagePlus("Mask", ip); maskImp.show(); } ImageProcessor ip = maskImp.getProcessor(); ip.setRoi(roi); ip.setValue(255); ip.fill(ip.getMask()); maskImp.updateAndDraw(); Prefs.useInvertingLut = useInvertingLut; }
void Contrast(ImagePlus imp, int radius, double par1, double par2, boolean doIwhite) { // G. Landini, 2013 // Based on a simple contrast toggle. This procedure does not have user-provided paramters other // than the kernel radius // Sets the pixel value to either white or black depending on whether its current value is // closest to the local Max or Min respectively // The procedure is similar to Toggle Contrast Enhancement (see Soille, Morphological Image // Analysis (2004), p. 259 ImagePlus Maximp, Minimp; ImageProcessor ip = imp.getProcessor(), ipMax, ipMin; int c_value = 0; int mid_gray; byte object; byte backg; if (doIwhite) { object = (byte) 0xff; backg = (byte) 0; } else { object = (byte) 0; backg = (byte) 0xff; } Maximp = duplicateImage(ip); ipMax = Maximp.getProcessor(); RankFilters rf = new RankFilters(); rf.rank(ipMax, radius, rf.MAX); // Maximum // Maximp.show(); Minimp = duplicateImage(ip); ipMin = Minimp.getProcessor(); rf.rank(ipMin, radius, rf.MIN); // Minimum // Minimp.show(); byte[] pixels = (byte[]) ip.getPixels(); byte[] max = (byte[]) ipMax.getPixels(); byte[] min = (byte[]) ipMin.getPixels(); for (int i = 0; i < pixels.length; i++) { pixels[i] = ((Math.abs((int) (max[i] & 0xff - pixels[i] & 0xff)) <= Math.abs((int) (pixels[i] & 0xff - min[i] & 0xff)))) ? object : backg; } // imp.updateAndDraw(); return; }
private ImagePlus duplicateImage(ImageProcessor iProcessor) { int w = iProcessor.getWidth(); int h = iProcessor.getHeight(); ImagePlus iPlus = NewImage.createByteImage("Image", w, h, 1, NewImage.FILL_BLACK); ImageProcessor imageProcessor = iPlus.getProcessor(); imageProcessor.copyBits(iProcessor, 0, 0, Blitter.COPY); return iPlus; }
void MidGrey(ImagePlus imp, int radius, double par1, double par2, boolean doIwhite) { // See: Image Processing Learning Resourches HIPR2 // http://homepages.inf.ed.ac.uk/rbf/HIPR2/adpthrsh.htm ImagePlus Maximp, Minimp; ImageProcessor ip = imp.getProcessor(), ipMax, ipMin; int c_value = 0; int mid_gray; byte object; byte backg; if (par1 != 0) { IJ.log("MidGrey: changed c_value from :" + c_value + " to:" + par1); c_value = (int) par1; } if (doIwhite) { object = (byte) 0xff; backg = (byte) 0; } else { object = (byte) 0; backg = (byte) 0xff; } Maximp = duplicateImage(ip); ipMax = Maximp.getProcessor(); RankFilters rf = new RankFilters(); rf.rank(ipMax, radius, rf.MAX); // Maximum // Maximp.show(); Minimp = duplicateImage(ip); ipMin = Minimp.getProcessor(); rf.rank(ipMin, radius, rf.MIN); // Minimum // Minimp.show(); byte[] pixels = (byte[]) ip.getPixels(); byte[] max = (byte[]) ipMax.getPixels(); byte[] min = (byte[]) ipMin.getPixels(); for (int i = 0; i < pixels.length; i++) { pixels[i] = ((int) (pixels[i] & 0xff) > (int) (((max[i] & 0xff) + (min[i] & 0xff)) / 2) - c_value) ? object : backg; } // imp.updateAndDraw(); return; }
/** * Execute the plugin functionality: duplicate and scale the given image. * * @return an Object[] array with the name and the scaled ImagePlus. Does NOT show the new, image; * just returns it. */ public Object[] exec( ImagePlus imp, String myMethod, int radius, double par1, double par2, boolean doIwhite) { // 0 - Check validity of parameters if (null == imp) return null; ImageProcessor ip = imp.getProcessor(); int xe = ip.getWidth(); int ye = ip.getHeight(); // int [] data = (ip.getHistogram()); IJ.showStatus("Thresholding..."); long startTime = System.currentTimeMillis(); // 1 Do it if (imp.getStackSize() == 1) { ip.snapshot(); Undo.setup(Undo.FILTER, imp); } // Apply the selected algorithm if (myMethod.equals("Bernsen")) { Bernsen(imp, radius, par1, par2, doIwhite); } else if (myMethod.equals("Contrast")) { Contrast(imp, radius, par1, par2, doIwhite); } else if (myMethod.equals("Mean")) { Mean(imp, radius, par1, par2, doIwhite); } else if (myMethod.equals("Median")) { Median(imp, radius, par1, par2, doIwhite); } else if (myMethod.equals("MidGrey")) { MidGrey(imp, radius, par1, par2, doIwhite); } else if (myMethod.equals("Niblack")) { Niblack(imp, radius, par1, par2, doIwhite); } else if (myMethod.equals("Otsu")) { Otsu(imp, radius, par1, par2, doIwhite); } else if (myMethod.equals("Phansalkar")) { Phansalkar(imp, radius, par1, par2, doIwhite); } else if (myMethod.equals("Sauvola")) { Sauvola(imp, radius, par1, par2, doIwhite); } // IJ.showProgress((double)(255-i)/255); imp.updateAndDraw(); imp.getProcessor().setThreshold(255, 255, ImageProcessor.NO_LUT_UPDATE); // 2 - Return the threshold and the image IJ.showStatus("\nDone " + (System.currentTimeMillis() - startTime) / 1000.0); return new Object[] {imp}; }
private void doRGBProjection(ImageStack stack) { ImageStack[] channels = ChannelSplitter.splitRGB(stack, true); ImagePlus red = new ImagePlus("Red", channels[0]); ImagePlus green = new ImagePlus("Green", channels[1]); ImagePlus blue = new ImagePlus("Blue", channels[2]); imp.unlock(); ImagePlus saveImp = imp; imp = red; color = "(red)"; doProjection(); ImagePlus red2 = projImage; imp = green; color = "(green)"; doProjection(); ImagePlus green2 = projImage; imp = blue; color = "(blue)"; doProjection(); ImagePlus blue2 = projImage; int w = red2.getWidth(), h = red2.getHeight(), d = red2.getStackSize(); if (method == SD_METHOD) { ImageProcessor r = red2.getProcessor(); ImageProcessor g = green2.getProcessor(); ImageProcessor b = blue2.getProcessor(); double max = 0; double rmax = r.getStatistics().max; if (rmax > max) max = rmax; double gmax = g.getStatistics().max; if (gmax > max) max = gmax; double bmax = b.getStatistics().max; if (bmax > max) max = bmax; double scale = 255 / max; r.multiply(scale); g.multiply(scale); b.multiply(scale); red2.setProcessor(r.convertToByte(false)); green2.setProcessor(g.convertToByte(false)); blue2.setProcessor(b.convertToByte(false)); } RGBStackMerge merge = new RGBStackMerge(); ImageStack stack2 = merge.mergeStacks(w, h, d, red2.getStack(), green2.getStack(), blue2.getStack(), true); imp = saveImp; projImage = new ImagePlus(makeTitle(), stack2); }
public int setup(String arg, ImagePlus imp) { this.arg = arg; IJ.register(Binary.class); doOptions = arg.equals("options"); if (doOptions) { if (imp == null) return NO_IMAGE_REQUIRED; // options dialog does not need a (suitable) image ImageProcessor ip = imp.getProcessor(); if (!(ip instanceof ByteProcessor)) return NO_IMAGE_REQUIRED; if (!((ByteProcessor) ip).isBinary()) return NO_IMAGE_REQUIRED; } return flags; }
void lineWidth() { int width = (int) IJ.getNumber("Line Width:", Line.getWidth()); if (width == IJ.CANCELED) return; Line.setWidth(width); LineWidthAdjuster.update(); ImagePlus imp = WindowManager.getCurrentImage(); if (imp != null && imp.isProcessor()) { ImageProcessor ip = imp.getProcessor(); ip.setLineWidth(Line.getWidth()); Roi roi = imp.getRoi(); if (roi != null && roi.isLine()) imp.draw(); } }
public void showRois3D() { registerActiveImage(); if (currentImage == null) return; // verifier que l'image active a les memes dimentions Object[] os = this.list.getSelectedValues(); if (os.length == 1) { mcib3d.geom.Object3D o = ((Object3DGui) os[0]).getObject3D(); currentImage.setSlice((o.getZmax() + o.getZmin()) / 2 + 1); } int nSlices = currentImage.getNSlices(); currentROIs = new HashMap<Integer, Roi>(nSlices); // stores the roi mask to save memory.. if (roiMask == null || !roiMask.sameDimentions(currentImage)) { roiMask = new ImageByte("mask", currentImage.getWidth(), currentImage.getHeight(), nSlices); } else { roiMask.erase(); } ImageStack maskStack = roiMask.getImageStack(); Object3DGui obj; for (Object o : os) { obj = (Object3DGui) o; obj.getObject3D().draw(maskStack, 255); } // roiMask.show(); for (int i = 1; i <= nSlices; i++) { ImagePlus im = new ImagePlus("mask", maskStack.getProcessor(i)); im.getProcessor().setThreshold(1, 255, ImageProcessor.NO_LUT_UPDATE); ThresholdToSelection tts = new ThresholdToSelection(); tts.setup("", im); tts.run(im.getProcessor()); Roi r = im.getRoi(); if (r != null) currentROIs.put(i, r); } updateRoi(); }
void Mean(ImagePlus imp, int radius, double par1, double par2, boolean doIwhite) { // See: Image Processing Learning Resourches HIPR2 // http://homepages.inf.ed.ac.uk/rbf/HIPR2/adpthrsh.htm ImagePlus Meanimp; ImageProcessor ip = imp.getProcessor(), ipMean; int c_value = 0; byte object; byte backg; if (par1 != 0) { IJ.log("Mean: changed c_value from :" + c_value + " to:" + par1); c_value = (int) par1; } if (doIwhite) { object = (byte) 0xff; backg = (byte) 0; } else { object = (byte) 0; backg = (byte) 0xff; } Meanimp = duplicateImage(ip); ImageConverter ic = new ImageConverter(Meanimp); ic.convertToGray32(); ipMean = Meanimp.getProcessor(); RankFilters rf = new RankFilters(); rf.rank(ipMean, radius, rf.MEAN); // Mean // Meanimp.show(); byte[] pixels = (byte[]) ip.getPixels(); float[] mean = (float[]) ipMean.getPixels(); for (int i = 0; i < pixels.length; i++) pixels[i] = ((int) (pixels[i] & 0xff) > (int) (mean[i] - c_value)) ? object : backg; // imp.updateAndDraw(); return; }
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(); }
// at the very end - show output image (if the is a separate one) private void showOutput() { if (interrupted) return; if (outStack != null) { outImp = new ImagePlus(TITLE_PREFIX[processType] + imp.getShortTitle(), outStack); int[] d = imp.getDimensions(); outImp.setDimensions(d[2], d[3], d[4]); for (int i = 1; i <= imp.getStackSize(); i++) outStack.setSliceLabel(imp.getStack().getSliceLabel(i), i); } if (outImageType != BYTE_OVERWRITE) { ImageProcessor ip = outImp.getProcessor(); if (!Prefs.blackBackground) ip.invertLut(); ip.resetMinAndMax(); outImp.show(); } }
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; }
/** 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; }
void setHistogram(ImagePlus imp, int j) { ImageProcessor ip = imp.getProcessor(); ImageStatistics stats = ImageStatistics.getStatistics(ip, AREA + MODE, null); int maxCount2 = 0; histogram = stats.histogram; for (int i = 0; i < stats.nBins; i++) if ((histogram[i] > maxCount2) && (i != stats.mode)) maxCount2 = histogram[i]; hmax = stats.maxCount; if ((hmax > (maxCount2 * 1.5)) && (maxCount2 != 0)) { // GL 1.5 was 2 hmax = (int) (maxCount2 * 1.1); // GL 1.1 was 1.5 histogram[stats.mode] = hmax; } os = null; ColorModel cm = ip.getColorModel(); if (!(cm instanceof IndexColorModel)) return; IndexColorModel icm = (IndexColorModel) cm; int mapSize = icm.getMapSize(); if (mapSize != 256) return; byte[] r = new byte[256]; byte[] g = new byte[256]; byte[] b = new byte[256]; icm.getReds(r); icm.getGreens(g); icm.getBlues(b); hColors = new Color[256]; if (isRGB) { if (j == 0) { for (int i = 0; i < 256; i++) hColors[i] = new Color(i & 255, 0 & 255, 0 & 255); } else if (j == 1) { for (int i = 0; i < 256; i++) hColors[i] = new Color(0 & 255, i & 255, 0 & 255); } else if (j == 2) { for (int i = 0; i < 256; i++) hColors[i] = new Color(0 & 255, 0 & 255, i & 255); } } else { if (j == 0) { for (int i = 0; i < 256; i++) hColors[i] = new Color(r[i] & 255, g[i] & 255, b[i] & 255); } else if (j == 1) { for (int i = 0; i < 256; i++) // hColors[i] = new Color(127-i/2&255, 127+i/2&255, 127-i/2&255); hColors[i] = new Color(192 - i / 4 & 255, 192 + i / 4 & 255, 192 - i / 4 & 255); } else if (j == 2) { for (int i = 0; i < 256; i++) hColors[i] = new Color(i & 255, i & 255, 0 & 255); } } }
public void run(String arg) { imp = IJ.getImage(); Roi roi = imp.getRoi(); if (roi != null && !roi.isArea()) imp.killRoi(); // ignore any line selection ImageProcessor ip = imp.getProcessor(); if (!showDialog(ip)) return; if (ip.getWidth() > 1 && ip.getHeight() > 1) ip.setInterpolate(interpolate); else ip.setInterpolate(false); ip.setBackgroundValue(bgValue); imp.startTiming(); try { if (newWindow && imp.getStackSize() > 1 && processStack) createNewStack(imp, ip); else scale(ip); } catch (OutOfMemoryError o) { IJ.outOfMemory("Scale"); } IJ.showProgress(1.0); }
protected void registerActiveImage() { ImagePlus activeImage = WindowManager.getCurrentImage(); if (activeImage != null && activeImage.getProcessor() != null) { // && activeImage.getImageStackSize() > 1 if (currentImage != null && currentImage.getWindow() != null && currentImage != activeImage) { // System.out.println("remove listener:"+currentImage.getTitle()); ImageUtils.removeScrollListener(currentImage, this, this); currentImage.killRoi(); currentImage.updateAndDraw(); currentImage = null; } if (currentImage != activeImage) { // System.out.println("add listener:"+activeImage.getTitle()); ImageUtils.addScrollListener(activeImage, this, this); this.currentImage = activeImage; } } }
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); }
void createSelectionFromMask(ImagePlus imp) { ImageProcessor ip = imp.getProcessor(); if (ip.getMinThreshold() != ImageProcessor.NO_THRESHOLD) { IJ.runPlugIn("ij.plugin.filter.ThresholdToSelection", ""); return; } if (!ip.isBinary()) { IJ.error( "Create Selection", "This command creates a composite selection from\n" + "a mask (8-bit binary image with white background)\n" + "or from an image that has been thresholded using\n" + "the Image>Adjust>Threshold tool. The current\n" + "image is not a mask and has not been thresholded."); return; } int threshold = ip.isInvertedLut() ? 255 : 0; ip.setThreshold(threshold, threshold, ImageProcessor.NO_LUT_UPDATE); IJ.runPlugIn("ij.plugin.filter.ThresholdToSelection", ""); }
protected void setDrawingColor(int ox, int oy, boolean setBackground) { // IJ.log("setDrawingColor: "+setBackground+this); int type = imp.getType(); int[] v = imp.getPixel(ox, oy); switch (type) { case ImagePlus.GRAY8: { if (setBackground) setBackgroundColor(getColor(v[0])); else setForegroundColor(getColor(v[0])); break; } case ImagePlus.GRAY16: case ImagePlus.GRAY32: { double min = imp.getProcessor().getMin(); double max = imp.getProcessor().getMax(); double value = (type == ImagePlus.GRAY32) ? Float.intBitsToFloat(v[0]) : v[0]; int index = (int) (255.0 * ((value - min) / (max - min))); if (index < 0) index = 0; if (index > 255) index = 255; if (setBackground) setBackgroundColor(getColor(index)); else setForegroundColor(getColor(index)); break; } case ImagePlus.COLOR_RGB: case ImagePlus.COLOR_256: { Color c = new Color(v[0], v[1], v[2]); if (setBackground) setBackgroundColor(c); else setForegroundColor(c); break; } } Color c; if (setBackground) c = Toolbar.getBackgroundColor(); else { c = Toolbar.getForegroundColor(); imp.setColor(c); } IJ.showStatus("(" + c.getRed() + ", " + c.getGreen() + ", " + c.getBlue() + ")"); }
public void run(String arg) { ImagePlus imp = WindowManager.getCurrentImage(); if (imp == null) { IJ.noImage(); return; } if (imp.getStackSize() > 1) { IJ.error("This command requires a montage"); return; } GenericDialog gd = new GenericDialog("Stack Maker"); gd.addNumericField("Images_per_row: ", w, 0); gd.addNumericField("Images_per_column: ", h, 0); gd.addNumericField("Border width: ", b, 0); gd.showDialog(); if (gd.wasCanceled()) return; w = (int) gd.getNextNumber(); h = (int) gd.getNextNumber(); b = (int) gd.getNextNumber(); ImageStack stack = makeStack(imp.getProcessor(), w, h, b); new ImagePlus("Stack", stack).show(); }
void createMask(ImagePlus imp) { Roi roi = imp.getRoi(); boolean useInvertingLut = Prefs.useInvertingLut; Prefs.useInvertingLut = false; boolean selectAll = roi != null && roi.getType() == Roi.RECTANGLE && roi.getBounds().width == imp.getWidth() && roi.getBounds().height == imp.getHeight() && imp.isThreshold(); if (roi == null || !(roi.isArea() || roi.getType() == Roi.POINT) || selectAll) { createMaskFromThreshold(imp); Prefs.useInvertingLut = useInvertingLut; return; } ImagePlus maskImp = null; Frame frame = WindowManager.getFrame("Mask"); if (frame != null && (frame instanceof ImageWindow)) maskImp = ((ImageWindow) frame).getImagePlus(); if (maskImp == null) { ImageProcessor ip = new ByteProcessor(imp.getWidth(), imp.getHeight()); if (!Prefs.blackBackground) ip.invertLut(); maskImp = new ImagePlus("Mask", ip); maskImp.show(); } ImageProcessor ip = maskImp.getProcessor(); ip.setRoi(roi); ip.setValue(255); ip.fill(ip.getMask()); Calibration cal = imp.getCalibration(); if (cal.scaled()) { Calibration cal2 = maskImp.getCalibration(); cal2.pixelWidth = cal.pixelWidth; cal2.pixelHeight = cal.pixelHeight; cal2.setUnit(cal.getUnit()); } maskImp.updateAndRepaintWindow(); Prefs.useInvertingLut = useInvertingLut; }
/** Splits the specified image into separate channels. */ public static ImagePlus[] split(ImagePlus imp) { if (imp.getType() == ImagePlus.COLOR_RGB) { ImageStack[] stacks = splitRGB(imp.getStack(), true); ImagePlus[] images = new ImagePlus[3]; images[0] = new ImagePlus("red", stacks[0]); images[1] = new ImagePlus("green", stacks[1]); images[2] = new ImagePlus("blue", stacks[2]); return images; } int width = imp.getWidth(); int height = imp.getHeight(); int channels = imp.getNChannels(); int slices = imp.getNSlices(); int frames = imp.getNFrames(); int bitDepth = imp.getBitDepth(); int size = slices * frames; Vector images = new Vector(); HyperStackReducer reducer = new HyperStackReducer(imp); for (int c = 1; c <= channels; c++) { ImageStack stack2 = new ImageStack(width, height, size); // create empty stack stack2.setPixels( imp.getProcessor().getPixels(), 1); // can't create ImagePlus will null 1st image ImagePlus imp2 = new ImagePlus("C" + c + "-" + imp.getTitle(), stack2); stack2.setPixels(null, 1); imp.setPosition(c, 1, 1); imp2.setDimensions(1, slices, frames); imp2.setCalibration(imp.getCalibration()); reducer.reduce(imp2); if (imp.isComposite() && ((CompositeImage) imp).getMode() == IJ.GRAYSCALE) IJ.run(imp2, "Grays", ""); if (imp2.getNDimensions() > 3) imp2.setOpenAsHyperStack(true); images.add(imp2); } ImagePlus[] array = new ImagePlus[images.size()]; return (ImagePlus[]) images.toArray(array); }