/** * Calculate the Rand index between some 2D original labels and the corresponding proposed labels. * Both image are binarized. We follow the definition of Rand index as described by William M. * Rand \cite{Rand71}. * * <p>BibTeX: * * <pre> * @article{Rand71, * author = {William M. Rand}, * title = {Objective criteria for the evaluation of clustering methods}, * journal = {Journal of the American Statistical Association}, * year = {1971}, * volume = {66}, * number = {336}, * pages = {846--850}, * doi = {10.2307/2284239) * } * </pre> * * @param label 2D image with the original labels * @param proposal 2D image with the proposed labels * @param binaryThreshold threshold value to binarize the input images * @return rand index value and derived statistics */ public ClassificationStatistics randIndexStats( ImageProcessor label, ImageProcessor proposal, double binaryThreshold) { // Binarize inputs ByteProcessor binaryLabel = new ByteProcessor(label.getWidth(), label.getHeight()); ByteProcessor binaryProposal = new ByteProcessor(label.getWidth(), label.getHeight()); for (int x = 0; x < label.getWidth(); x++) for (int y = 0; y < label.getHeight(); y++) { binaryLabel.set(x, y, label.getPixelValue(x, y) > binaryThreshold ? 255 : 0); binaryProposal.set(x, y, proposal.getPixelValue(x, y) > binaryThreshold ? 255 : 0); } // Find components ShortProcessor components1 = (ShortProcessor) Utils.connectedComponents(new ImagePlus("binary labels", binaryLabel), 4) .allRegions .getProcessor(); ShortProcessor components2 = (ShortProcessor) Utils.connectedComponents(new ImagePlus("proposal labels", binaryProposal), 4) .allRegions .getProcessor(); return getRandIndexStats(components1, components2); }
private static final void prepareExtendedImage( final ImageProcessor source, final ImageProcessor target) { if (target.getWidth() > source.getWidth()) { target.copyBits(source, source.getWidth(), 0, Blitter.COPY); if (target.getHeight() > source.getHeight()) target.copyBits(source, source.getWidth(), 0, Blitter.COPY); } if (target.getHeight() > source.getHeight()) target.copyBits(source, 0, 1, Blitter.COPY); target.copyBits(source, 0, 0, Blitter.COPY); }
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); }
// 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; }
@Override public final void run() { while (!isInterrupted()) { final boolean b; synchronized (this) { b = pleaseRepaint; pleaseRepaint = keepPainting; } if (b) { final long t = System.currentTimeMillis(); lambda += dt * dLambda; phi += dt * dPhi; this.camera.setOrientation(lambda, phi, rho); mapper.map(temp); final Object targetPixels = target.getPixels(); target.setPixels(temp.getPixels()); temp.setPixels(targetPixels); impTarget.updateAndDraw(); if (visualize) visualize(impSource, temp.getWidth(), temp.getHeight(), p); dt = (System.currentTimeMillis() - t) / 1000f; } synchronized (this) { try { if (!pleaseRepaint) wait(); } catch (final InterruptedException e) { } } } }
/** * 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; }
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; }
// constructor method public ContourTracer(ImageProcessor ip) { this.ip = ip; this.width = ip.getWidth(); this.height = ip.getHeight(); makeAuxArrays(); findAllContours(); collectRegions(); }
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; }
/** 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 BooleanImage getBinaryImage(ImageProcessor ip) { if (th1 == null || !isSupported(ip)) return new BooleanImage(ip.getWidth(), ip.getHeight()); int width = ip.getWidth(); int height = ip.getHeight(); byte[] ch1, ch2, ch3; ch1 = new byte[ip.getWidth() * ip.getHeight()]; ch2 = new byte[ip.getWidth() * ip.getHeight()]; ch3 = new byte[ip.getWidth() * ip.getHeight()]; ColorProcessor cp = (ColorProcessor) ip; switch (colorspace) { case RGB: cp.getRGB(ch1, ch2, ch3); break; case HSB: cp.getHSB(ch1, ch2, ch3); break; case Lab: getLab(cp, ch1, ch2, ch3); break; case YUV: getYUV(cp, ch1, ch2, ch3); break; default: break; } boolean[] pix = new boolean[width * height]; for (int i = 0; i < ch1.length; i++) { boolean c1 = th1[0] <= (ch1[i] & 0xff) && (ch1[i] & 0xff) <= th1[1]; if (pass1 ? !c1 : c1) continue; boolean c2 = th2[0] <= (ch2[i] & 0xff) && (ch2[i] & 0xff) <= th2[1]; if (pass2 ? !c2 : c2) continue; boolean c3 = th3[0] <= (ch3[i] & 0xff) && (ch3[i] & 0xff) <= th3[1]; if (pass3 ? !c3 : c3) continue; pix[i] = true; } pix = applyMask(pix); return new BooleanImage(width, height, pix); }
// 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; } }
/** Adds the image in 'ip' to the end of the stack. */ public void addSlice(String sliceLabel, ImageProcessor ip) { if (ip.getWidth() != width || ip.getHeight() != height) throw new IllegalArgumentException("Dimensions do not match"); if (nSlices == 0) { cm = ip.getColorModel(); min = ip.getMin(); max = ip.getMax(); } addSlice(sliceLabel, ip.getPixels()); }
public final void run( final ImagePlus imp, final int width, final int height, final double minLambda, final double minPhi, final double hfov, final double vfov) { ip = imp.getProcessor().createProcessor(width, height); final ImagePlus impViewer = new ImagePlus("Panorama View", ip); /* initialize projection */ p.setMinLambda(minLambda); p.setMinPhi(minPhi); p.setLambdaPiScale(Math.PI / hfov * imp.getWidth()); p.setPhiPiScale(Math.PI / vfov * (imp.getHeight() - 1)); p.setTargetWidth(ip.getWidth()); p.setTargetHeight(ip.getHeight()); p.setF(0.5); System.out.println(p.getLambdaPiScale() + " " + p.getPhiPiScale()); /* TODO calculate proper size */ // final int cubeSize = 500; final int cubeSize = (int) Math.round(Math.max(p.getPhiPiScale(), p.getLambdaPiScale()) * 2.0 / Math.PI); frontSource = ip.createProcessor(cubeSize + 1, cubeSize + 1); backSource = ip.createProcessor(cubeSize + 1, cubeSize + 1); leftSource = ip.createProcessor(cubeSize + 1, cubeSize + 1); rightSource = ip.createProcessor(cubeSize + 1, cubeSize + 1); topSource = ip.createProcessor(cubeSize + 1, cubeSize + 1); bottomSource = ip.createProcessor(cubeSize + 1, cubeSize + 1); renderCubeFaces(hfov, vfov); /* instantiate and run mapper and painter */ final Mapper mapper = new CubeFaceMapper( frontSource, backSource, leftSource, rightSource, topSource, bottomSource, p); painter = new MappingThread(imp, impViewer, mapper, ip, p); impViewer.show(); gui = new GUI(impViewer); gui.backupGui(); gui.takeOverGui(); painter.start(); update(false); }
ImageProcessor expand(ImageProcessor ip, boolean hasEdgePixels) { if (hasEdgePixels) { ImageProcessor ip2 = ip.createProcessor(ip.getWidth() + 2, ip.getHeight() + 2); if (foreground == 0) { ip2.setColor(255); ip2.fill(); } ip2.insert(ip, 1, 1); // new ImagePlus("ip2", ip2).show(); return ip2; } else return ip; }
public void reset(ImageProcessor mask) { if (mask == null || snapshotPixels == null) return; if (mask.getWidth() != roiWidth || mask.getHeight() != roiHeight) throw new IllegalArgumentException(maskSizeError(mask)); byte[] mpixels = (byte[]) mask.getPixels(); for (int y = roiY, my = 0; y < (roiY + roiHeight); y++, my++) { int i = y * width + roiX; int mi = my * roiWidth; for (int x = roiX; x < (roiX + roiWidth); x++) { if (mpixels[mi++] == 0) pixels[i] = snapshotPixels[i]; i++; } } }
@Override public int[] getLineInt(int k, int dir) { final int width = ip.getWidth(); final int height = ip.getHeight(); final int[] ret = (int[]) xget(); switch (dir) { case Ox: { if (debug) System.out.println("fetching direction Ox"); final int lineno = height; int offset = k * width; int z = offset / (width * height); if (z >= 0 && z < lineno) { Object aux = ip.getPixels(); try { System.arraycopy(aux, offset % height, ret, 0, ret.length); // System.out.println(":"+offset/z); } catch (Exception e) { System.out.println("offset" + (offset % height)); e.printStackTrace(); } } return ret; } case Oy: { if (debug) System.out.println("fetching direction Oy"); final int lineno = width; int offset = k * height; k = k % width; int z = offset / (width * height); if (z >= 0 && z < lineno) { try { int[] pixels = (int[]) ip.getPixels(); if (pixels != null) for (int y = 0; y < height; y++) { ret[y] = pixels[k + y * width]; // System.out.print( "("+ k +" " +y +"),"); } } catch (Exception e) { // System.out.println("k "+ k ); e.printStackTrace(); } } return ret; } } return null; }
/** * Creates the Euclidian Distance Map of a (binary) byte image. * * @param ip The input image, not modified; must be a ByteProcessor. * @param backgroundValue Pixels in the input with this value are interpreted as background. Note: * for pixel value 255, write either -1 or (byte)255. * @param edgesAreBackground Whether out-of-image pixels are considered background * @return The EDM, containing the distances to the nearest background pixel. Returns null if the * thread is interrupted. */ public FloatProcessor makeFloatEDM( ImageProcessor ip, int backgroundValue, boolean edgesAreBackground) { int width = ip.getWidth(); int height = ip.getHeight(); FloatProcessor fp = new FloatProcessor(width, height); byte[] bPixels = (byte[]) ip.getPixels(); float[] fPixels = (float[]) fp.getPixels(); final int progressInterval = 100; int nProgressUpdates = height / progressInterval; // how often the progress bar is updated when passing once through y // range double progressAddendum = (nProgressUpdates > 0) ? 0.5 / nProgressUpdates : 0; for (int i = 0; i < width * height; i++) if (bPixels[i] != backgroundValue) fPixels[i] = Float.MAX_VALUE; int[][] pointBufs = new int[2][width]; // two buffers for two passes; low short contains x, high short y int yDist = Integer.MAX_VALUE; // this value is used only if edges are not background // pass 1 & 2: increasing y for (int x = 0; x < width; x++) { pointBufs[0][x] = NO_POINT; pointBufs[1][x] = NO_POINT; } for (int y = 0; y < height; y++) { if (edgesAreBackground) yDist = y + 1; // distance to nearest background point (along y) edmLine(bPixels, fPixels, pointBufs, width, y * width, y, backgroundValue, yDist); if (y % progressInterval == 0) { if (Thread.currentThread().isInterrupted()) return null; addProgress(progressAddendum); } } // pass 3 & 4: decreasing y for (int x = 0; x < width; x++) { pointBufs[0][x] = NO_POINT; pointBufs[1][x] = NO_POINT; } for (int y = height - 1; y >= 0; y--) { if (edgesAreBackground) yDist = height - y; edmLine(bPixels, fPixels, pointBufs, width, y * width, y, backgroundValue, yDist); if (y % progressInterval == 0) { if (Thread.currentThread().isInterrupted()) return null; addProgress(progressAddendum); } } fp.sqrt(); return fp; } // public FloatProcessor makeFloatEDM
public void run(ImageProcessor image) { /** * ********************************************************************************* Initial * phase ********************************************************************************* */ int width = image.getWidth(); int heigh = image.getHeight(); ByteProcessor bp = Service.getByteProcessor(image); /** * ********************************************************************************* Convolve * with LoG kernel * ********************************************************************************* */ Convolver convolver = new Convolver(); convolver.setNormalize(false); convolver.convolve(bp, log, (int) Math.sqrt(log.length), (int) Math.sqrt(log.length)); ImagePlus outImg = new ImagePlus("Later Log kernel", bp); outImg.show(); /** * ********************************************************************************* threshold * ********************************************************************************* */ ByteProcessor bpThreshold = Service.getByteProcessor(bp); int lut[] = new int[256]; int i = 0; for (; i < threshold; i++) lut[i] = 0; for (int j = i; j < lut.length; j++) lut[j] = 255; bpThreshold.applyTable(lut); ImagePlus outImg1 = new ImagePlus("Later threshold phase", bpThreshold); outImg1.show(); /** * ********************************************************************************* Find Zero * crossing ********************************************************************************* */ ByteProcessor out = new ByteProcessor(width, heigh); for (i = 0; i < width; i++) for (int j = 0; j < heigh; j++) out.set(i, j, 255); for (int x = 0; x < width - 1; x++) { for (int y = 0; y < heigh - 1; y++) { if (bpThreshold.get(x, y) != bpThreshold.get(x, y + 1)) out.set(x, y, 0); if (bpThreshold.get(x, y) != bpThreshold.get(x + 1, y)) out.set(x, y, 0); if (bpThreshold.get(x, y) != bpThreshold.get(x + 1, y + 1)) out.set(x, y, 0); } } ImagePlus outImg2 = new ImagePlus("Edge Find", out); outImg2.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; }
/*------------------------------------------------------------------*/ void putColumn(ImageProcessor ip, int x, double[] column) { int width = ip.getWidth(); if (ip.getHeight() != column.length) { throw new IndexOutOfBoundsException("Incoherent array sizes"); } if (ip.getPixels() instanceof float[]) { float[] floatPixels = (float[]) ip.getPixels(); for (int i = 0; (i < column.length); i++) { floatPixels[x] = (float) column[i]; x += width; } } else { throw new IllegalArgumentException("Float image required"); } } /* end putColumn */
public MappingThread( final ImagePlus impSource, final ImagePlus impTarget, final Mapper mapper, final ImageProcessor target, final PanoramaCamera<?> camera) { this.impSource = impSource; this.impTarget = impTarget; this.mapper = mapper; this.target = target; this.temp = target.createProcessor(target.getWidth(), target.getHeight()); temp.snapshot(); this.camera = camera; this.setName("MappingThread"); }
/** * 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}; }
/*------------------------------------------------------------------*/ public void getVerticalHessian(ImageProcessor ip, double tolerance) { if (!(ip.getPixels() instanceof float[])) { throw new IllegalArgumentException("Float image required"); } float[] floatPixels = (float[]) ip.getPixels(); int width = ip.getWidth(); int height = ip.getHeight(); double line[] = new double[height]; for (int x = 0; (x < width); x++) { getColumn(ip, x, line); getSplineInterpolationCoefficients(line, tolerance); getHessian(line); putColumn(ip, x, line); stepProgressBar(); } } /* end getVerticalHessian */
/*------------------------------------------------------------------*/ public void getHorizontalGradient(ImageProcessor ip, double tolerance) { if (!(ip.getPixels() instanceof float[])) { throw new IllegalArgumentException("Float image required"); } float[] floatPixels = (float[]) ip.getPixels(); int width = ip.getWidth(); int height = ip.getHeight(); double line[] = new double[width]; for (int y = 0; (y < height); y++) { getRow(ip, y, line); getSplineInterpolationCoefficients(line, tolerance); getGradient(line); putRow(ip, y, line); stepProgressBar(); } } /* end getHorizontalGradient */
/** * Fills pixels that are within roi and part of the mask. Does nothing if the mask is not the same * as the the ROI. */ public void fill(ImageProcessor mask) { if (mask == null) { fill(); return; } int roiWidth = this.roiWidth, roiHeight = this.roiHeight; int roiX = this.roiX, roiY = this.roiY; if (mask.getWidth() != roiWidth || mask.getHeight() != roiHeight) return; byte[] mpixels = (byte[]) mask.getPixels(); for (int y = roiY, my = 0; y < (roiY + roiHeight); y++, my++) { int i = y * width + roiX; int mi = my * roiWidth; for (int x = roiX; x < (roiX + roiWidth); x++) { if (mpixels[mi++] != 0) pixels[i] = fillColor; i++; } } }
public ImageStack makeStack(ImageProcessor ip, int w, int h, int b) { int stackSize = w * h; int width = ip.getWidth() / w; int height = ip.getHeight() / h; ImageStack stack = new ImageStack(width, height); for (int y = 0; y < h; y++) for (int x = 0; x < w; x++) { ip.setRoi(x * width, y * height, width, height); stack.addSlice(null, ip.crop()); } if (b > 0) { int cropwidth = width - b - b / 2; int cropheight = height - b - b / 2; StackProcessor sp = new StackProcessor(stack, ip); stack = sp.crop(b, b, cropwidth, cropheight); } return stack; }
boolean hasEdgePixels(ImageProcessor ip) { int width = ip.getWidth(); int height = ip.getHeight(); boolean edgePixels = false; for (int x = 0; x < width; x++) { // top edge if (ip.getPixel(x, 0) == foreground) edgePixels = true; } for (int x = 0; x < width; x++) { // bottom edge if (ip.getPixel(x, height - 1) == foreground) edgePixels = true; } for (int y = 0; y < height; y++) { // left edge if (ip.getPixel(0, y) == foreground) edgePixels = true; } for (int y = 0; y < height; y++) { // right edge if (ip.getPixel(width - 1, y) == foreground) edgePixels = true; } return edgePixels; }
public byte[] performExtraction() { width = grayImage.getWidth(); height = grayImage.getHeight(); byte[] pixels = (byte[]) grayImage.getPixels(); byte[] result = new byte[width * height]; int offset, index; for (int y = 1; y < height - 1; y++) { offset = y * width; for (int x = 1; x < width - 1; x++) { index = offset + x; result[index] = analyzeTexture(pixels, x, y, index); } } return result; }
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); }