/** Converts the specified RGB image to 8-bits if the 3 channels are identical. */ public static void convertGrayJpegTo8Bits(ImagePlus imp) { ImageProcessor ip = imp.getProcessor(); if (ip.getBitDepth() == 24 && ip.isGrayscale()) { IJ.showStatus("Converting to 8-bit grayscale"); new ImageConverter(imp).convertToGray8(); } }
/** * 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
/** 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 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)); }
// set values in floatEdm to zero if pixel in mask equals 'resetOnThis' private void resetMasked(FloatProcessor floatEdm, ImageProcessor mask, int resetOnThis) { int width = mask.getWidth(); int height = mask.getHeight(); byte[] mPixels = (byte[]) mask.getPixels(); float[] fPixels = (float[]) floatEdm.getPixels(); for (int i = 0; i < width * height; i++) if (mPixels[i] == resetOnThis) fPixels[i] = 0; }
public IJLineIteratorIP(ImageProcessor ip, int xdir) { this.ip = ip; if (xdir > 1) throw new IllegalArgumentException("illegal direction " + xdir); dir = xdir; final int width = ip.getWidth(); final int height = ip.getHeight(); switch (dir) { case Ox: { size = height; blength = width; break; } case Oy: { size = width; blength = height; break; } } // end Object pix = ip.getPixels(); setType(pix); btype = ip.getBitDepth(); initbuffer(dir); // System.out.println("\nbitedpth "+ btype); }
/** * 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; }
protected synchronized void initialize(ImageProcessor projection) throws Exception { if (!init) { super.init(); // Precompute offsets lineOffset = 0; if (getGeometry().getDetectorWidth() != -1) { System.out.println( "row size projection: " + projection.getWidth() + "\nrow size detector: " + getGeometry().getDetectorWidth()); lineOffset = (projection.getWidth() - getGeometry().getDetectorWidth()) / 2; } maxI = getGeometry().getReconDimensionX(); maxJ = getGeometry().getReconDimensionY(); maxK = getGeometry().getReconDimensionZ(); maxU = getGeometry().getDetectorWidth(); // or it should be projection.getWidth(); maxV = getGeometry().getDetectorHeight(); dx = getGeometry().getVoxelSpacingX(); dy = getGeometry().getVoxelSpacingY(); dz = getGeometry().getVoxelSpacingZ(); time = System.currentTimeMillis(); // projectionViews = InitializeProjectionViews(); // volumeImage = InitializeVolumeImage(); } }
void lineToArea(ImagePlus imp) { Roi roi = imp.getRoi(); if (roi == null || !roi.isLine()) { IJ.error("Line to Area", "Line selection required"); return; } Undo.setup(Undo.ROI, imp); Roi roi2 = null; if (roi.getType() == Roi.LINE) { double width = roi.getStrokeWidth(); if (width <= 1.0) roi.setStrokeWidth(1.0000001); FloatPolygon p = roi.getFloatPolygon(); roi.setStrokeWidth(width); roi2 = new PolygonRoi(p, Roi.POLYGON); roi2.setDrawOffset(roi.getDrawOffset()); } else { ImageProcessor ip2 = new ByteProcessor(imp.getWidth(), imp.getHeight()); ip2.setColor(255); roi.drawPixels(ip2); // new ImagePlus("ip2", ip2.duplicate()).show(); ip2.setThreshold(255, 255, ImageProcessor.NO_LUT_UPDATE); ThresholdToSelection tts = new ThresholdToSelection(); roi2 = tts.convert(ip2); } transferProperties(roi, roi2); roi2.setStrokeWidth(0); Color c = roi2.getStrokeColor(); if (c != null) // remove any transparency roi2.setStrokeColor(new Color(c.getRed(), c.getGreen(), c.getBlue())); imp.setRoi(roi2); Roi.previousRoi = (Roi) roi.clone(); }
public void run(ImageProcessor ip) { IndexColorModel icm = (IndexColorModel) ip.getColorModel(); // IJ.write("Color Model=" + ip.getColorModel() + " " + ip.isColorLut()); int pixBits = icm.getPixelSize(); int mapSize = icm.getMapSize(); // retrieve the current lookup tables (maps) for R,G,B byte[] Rmap = new byte[mapSize]; icm.getReds(Rmap); byte[] Gmap = new byte[mapSize]; icm.getGreens(Gmap); byte[] Bmap = new byte[mapSize]; icm.getBlues(Bmap); // modify the lookup tables for (int idx = 0; idx < mapSize; idx++) { int r = 0xff & Rmap[idx]; // mask to treat as unsigned byte int g = 0xff & Gmap[idx]; int b = 0xff & Bmap[idx]; Rmap[idx] = (byte) Math.min(r + 10, 255); Gmap[idx] = (byte) Math.min(g + 10, 255); Bmap[idx] = (byte) Math.min(b + 10, 255); } // create a new color model and apply to the image IndexColorModel icm2 = new IndexColorModel(pixBits, mapSize, Rmap, Gmap, Bmap); ip.setColorModel(icm2); WindowManager.getCurrentImage().updateAndDraw(); }
void lineToArea(ImagePlus imp) { Roi roi = imp.getRoi(); if (roi == null || !roi.isLine()) { IJ.error("Line to Area", "Line selection required"); return; } if (roi.getType() == Roi.LINE && roi.getStrokeWidth() == 1) { IJ.error("Line to Area", "Straight line width must be > 1"); return; } ImageProcessor ip2 = new ByteProcessor(imp.getWidth(), imp.getHeight()); ip2.setColor(255); if (roi.getType() == Roi.LINE) ip2.fillPolygon(roi.getPolygon()); else { roi.drawPixels(ip2); // BufferedImage bi = new BufferedImage(imp.getWidth(), imp.getHeight(), // BufferedImage.TYPE_BYTE_GRAY); // Graphics g = bi.getGraphics(); // Roi roi2 = (Roi)roi.clone(); // roi2.setStrokeColor(Color.white); // roi2.drawOverlay(g); // ip2 = new ByteProcessor(bi); } // new ImagePlus("ip2", ip2.duplicate()).show(); ip2.setThreshold(255, 255, ImageProcessor.NO_LUT_UPDATE); ThresholdToSelection tts = new ThresholdToSelection(); Roi roi2 = tts.convert(ip2); imp.setRoi(roi2); Roi.previousRoi = (Roi) roi.clone(); }
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 skeletonize(ImageProcessor ip) { if (Prefs.blackBackground) ip.invert(); boolean edgePixels = hasEdgePixels(ip); ImageProcessor ip2 = expand(ip, edgePixels); ((ByteProcessor) ip2).skeletonize(); ip = shrink(ip, ip2, edgePixels); if (Prefs.blackBackground) ip.invert(); }
// constructor method public ContourTracer(ImageProcessor ip) { this.ip = ip; this.width = ip.getWidth(); this.height = ip.getHeight(); makeAuxArrays(); findAllContours(); collectRegions(); }
public boolean resizeImage(Image i, File outputFile, int width, int height) { ImagePlus ip = ((ImageJImage) i).getImagePlus(); ImageProcessor processor = ip.getProcessor(); processor = processor.resize(width, height); ip.setProcessor(null, processor); return ImageProcess.save(((ImageJImage) i).getImageType(), ip, outputFile); }
/** * Updates this stack so its attributes, such as min, max, calibration table and color model, are * the same as 'ip'. */ public void update(ImageProcessor ip) { if (ip != null) { min = ip.getMin(); max = ip.getMax(); cTable = ip.getCalibrationTable(); cm = ip.getColorModel(); } }
/** Constructs a Wand object from an ImageProcessor. */ public Wand(ImageProcessor ip) { this.ip = ip; if (ip instanceof ByteProcessor) bpixels = (byte[]) ip.getPixels(); else if (ip instanceof ColorProcessor) cpixels = (int[]) ip.getPixels(); else if (ip instanceof ShortProcessor) spixels = (short[]) ip.getPixels(); else if (ip instanceof FloatProcessor) fpixels = (float[]) ip.getPixels(); width = ip.getWidth(); height = ip.getHeight(); }
@Override public void run(ImageProcessor ip) { if (!ColorProcessor.class.isAssignableFrom(ip.getClass())) { ip = ip.convertToRGB(); } firePropertyChange(Progress.START); process((ColorProcessor) ip); firePropertyChange(Progress.END); }
ImageProcessor shrink(ImageProcessor ip, ImageProcessor ip2, boolean hasEdgePixels) { if (hasEdgePixels) { int width = ip.getWidth(); int height = ip.getHeight(); for (int y = 0; y < height; y++) for (int x = 0; x < width; x++) ip.putPixel(x, y, ip2.getPixel(x + 1, y + 1)); } return ip; }
// overwrite ip with floatEdm converted to bytes private void byteFromFloat(ImageProcessor ip, FloatProcessor floatEdm) { int width = ip.getWidth(); int height = ip.getHeight(); byte[] bPixels = (byte[]) ip.getPixels(); float[] fPixels = (float[]) floatEdm.getPixels(); for (int i = 0; i < width * height; i++) { float v = fPixels[i]; bPixels[i] = v < 255f ? (byte) (v + 0.5) : (byte) 255; } }
/** * Creates a new FloatProcessor using the specified ImageProcessor. Set 'cm' to null to use the * default grayscale LUT. */ public SerializableFloatProcessor(ImageProcessor imageProcessor) { imageProcessor = imageProcessor.duplicate(); FloatProcessor floatProcessor = imageProcessor.toFloat(0, null); this.width = floatProcessor.getWidth(); this.height = floatProcessor.getHeight(); this.pixels = (float[]) floatProcessor.getPixels(); this.cm = floatProcessor.getColorModel(); resetRoi(); if (pixels != null) findMinAndMax(); }
/** Draws an outline of this OvalRoi on the image. */ public void drawPixels(ImageProcessor ip) { Polygon p = getPolygon(); if (p.npoints > 0) { int saveWidth = ip.getLineWidth(); if (getStrokeWidth() > 1f) ip.setLineWidth((int) Math.round(getStrokeWidth())); ip.drawPolygon(p); ip.setLineWidth(saveWidth); } if (Line.getWidth() > 1 || getStrokeWidth() > 1) updateFullWindow = true; }
public static void run() { ImagePlus imgPlus = new ImagePlus("data/in/sample1.tiff"); ImageProcessor imgProcessor = imgPlus.getProcessor(); imgProcessor.invert(); FileSaver fs = new FileSaver(imgPlus); fs.saveAsJpeg("data/out/sample1-inverted.jpg"); }
/** * Starts the haralick detection. * * @param ip ImageProcessor of the source image */ @Override public void run(ImageProcessor ip) { if (!ByteProcessor.class.isAssignableFrom(ip.getClass())) { ip = ip.convertToByte(true); } firePropertyChange(Progress.START); process((ByteProcessor) ip); addData(features); firePropertyChange(Progress.END); }
public ImageProcessor crop() { ImageProcessor ip2 = createProcessor(roiWidth, roiHeight); float[] pixels2 = (float[]) ip2.getPixels(); for (int ys = roiY; ys < roiY + roiHeight; ys++) { int offset1 = (ys - roiY) * roiWidth; int offset2 = ys * width + roiX; for (int xs = 0; xs < roiWidth; xs++) pixels2[offset1++] = pixels[offset2++]; } return ip2; }
private static double getMeanOfSlice(ImageProcessor ip_slice) { int[][] var1 = ip_slice.getIntArray(); int mean = 0; for (int i = 0; i < var1.length; i++) { for (int j = 0; j < var1[i].length; j++) { mean += var1[i][j]; } } return (double) mean / ip_slice.getPixelCount(); }
public boolean cropImage(Image i, File outputFile, int top, int left, int width, int height) { ImagePlus ip = ((ImageJImage) i).getImagePlus(); ImageProcessor processor = ip.getProcessor(); processor.setRoi(left, top, width, height); processor = processor.crop(); ip.setProcessor(null, processor); return ImageProcess.save(((ImageJImage) i).getImageType(), ip, outputFile); }
private final void renderCubeFaces(final double hfov, final double vfov) { /* fragile, but that's not public API and we know what we're doing... */ final double cubeSize = frontSource.getWidth() - 1; /* prepare extended image */ ipSource = ip.createProcessor( hfov == 2.0 * Math.PI ? imp.getWidth() + 1 : imp.getWidth(), vfov == Math.PI ? imp.getHeight() + 1 : imp.getHeight()); prepareExtendedImage(imp.getProcessor(), ipSource); /* render cube faces */ final EquirectangularProjection q = p.clone(); q.resetOrientation(); q.setTargetWidth(cubeSize); q.setTargetHeight(cubeSize); q.setF(0.5f); final InverseTransformMapping<EquirectangularProjection> qMapping = new InverseTransformMapping<EquirectangularProjection>(q); IJ.showStatus("Rendering cube faces..."); IJ.showProgress(0, 6); qMapping.mapInterpolated(ipSource, frontSource); IJ.showProgress(1, 6); q.pan(Math.PI); qMapping.mapInterpolated(ipSource, backSource); IJ.showProgress(2, 6); q.resetOrientation(); q.pan(Math.PI / 2); qMapping.mapInterpolated(ipSource, leftSource); IJ.showProgress(3, 6); q.resetOrientation(); q.pan(-Math.PI / 2); qMapping.mapInterpolated(ipSource, rightSource); IJ.showProgress(4, 6); q.resetOrientation(); q.tilt(-Math.PI / 2); qMapping.mapInterpolated(ipSource, topSource); IJ.showProgress(5, 6); q.resetOrientation(); q.tilt(Math.PI / 2); qMapping.mapInterpolated(ipSource, bottomSource); IJ.showProgress(6, 6); if (showCubefaces) { new ImagePlus("front", frontSource).show(); new ImagePlus("back", backSource).show(); new ImagePlus("left", leftSource).show(); new ImagePlus("right", rightSource).show(); new ImagePlus("top", topSource).show(); new ImagePlus("bottom", bottomSource).show(); } }
public boolean rotateImage(Image i, File outputFile, boolean clockwise) { ImagePlus ip = ((ImageJImage) i).getImagePlus(); ImageProcessor processor = ip.getProcessor(); if (clockwise) { processor = processor.rotateRight(); } else { processor = processor.rotateLeft(); } ip.setProcessor(null, processor); return ImageProcess.save(((ImageJImage) i).getImageType(), ip, outputFile); }
private void backwardIteration() { // variables declaration float ortho; float diago; float newVal; // Process last line: consider only the pixel just after (on the right) for (int i = width - 2; i >= 0; i--) { if (maskProc.getPixel(i, height - 1) != maskLabel) continue; ortho = array[i + 1][height - 1]; updateIfNeeded(i, height - 1, ortho + weights[0]); } // Process regular lines for (int j = height - 2; j >= 0; j--) { // process last pixel of the current line: consider pixels // down and down-left if (maskProc.getPixel(width - 1, j) == maskLabel) { ortho = array[width - 1][j + 1]; diago = array[width - 2][j + 1]; newVal = min(ortho + weights[0], diago + weights[1]); updateIfNeeded(width - 1, j, newVal); } // Process pixels in the middle of the current line for (int i = width - 2; i > 0; i--) { // process only pixels inside structure if (maskProc.getPixel(i, j) != maskLabel) continue; // minimum distance of neighbor pixels ortho = min(array[i + 1][j], array[i][j + 1]); diago = min(array[i - 1][j + 1], array[i + 1][j + 1]); // compute new distance of current pixel newVal = min(ortho + weights[0], diago + weights[1]); // modify current pixel if needed updateIfNeeded(i, j, newVal); } // process first pixel of current line: consider pixels right, // down-right and down if (maskProc.getPixel(0, j) == maskLabel) { // curVal = array[0][j]; ortho = min(array[1][j], array[0][j + 1]); diago = array[1][j + 1]; newVal = min(ortho + weights[0], diago + weights[1]); updateIfNeeded(0, j, newVal); } } // end of backward iteration }