void handleMouseMove(int sx, int sy) { // Do rubber banding int tool = Toolbar.getToolId(); if (!(tool == Toolbar.POLYGON || tool == Toolbar.POLYLINE || tool == Toolbar.ANGLE)) { imp.deleteRoi(); imp.draw(); return; } drawRubberBand(sx, sy); degrees = Double.NaN; double len = -1; if (nPoints > 1) { double x1, y1, x2, y2; if (xpf != null) { x1 = xpf[nPoints - 2]; y1 = ypf[nPoints - 2]; x2 = xpf[nPoints - 1]; y2 = ypf[nPoints - 1]; } else { x1 = xp[nPoints - 2]; y1 = yp[nPoints - 2]; x2 = xp[nPoints - 1]; y2 = yp[nPoints - 1]; } degrees = getAngle( (int) Math.round(x1), (int) Math.round(y1), (int) Math.round(x2), (int) Math.round(y2)); if (tool != Toolbar.ANGLE) { Calibration cal = imp.getCalibration(); double pw = cal.pixelWidth, ph = cal.pixelHeight; if (IJ.altKeyDown()) { pw = 1.0; ph = 1.0; } len = Math.sqrt((x2 - x1) * pw * (x2 - x1) * pw + (y2 - y1) * ph * (y2 - y1) * ph); } } if (tool == Toolbar.ANGLE) { if (nPoints == 2) angle1 = degrees; else if (nPoints == 3) { double angle2 = getAngle(xp[1], yp[1], xp[2], yp[2]); degrees = Math.abs(180 - Math.abs(angle1 - angle2)); if (degrees > 180.0) degrees = 360.0 - degrees; } } String length = len != -1 ? ", length=" + IJ.d2s(len) : ""; double degrees2 = tool == Toolbar.ANGLE && nPoints == 3 && Prefs.reflexAngle ? 360.0 - degrees : degrees; String angle = !Double.isNaN(degrees) ? ", angle=" + IJ.d2s(degrees2) : ""; int ox = ic != null ? ic.offScreenX(sx) : sx; int oy = ic != null ? ic.offScreenY(sy) : sy; IJ.showStatus(imp.getLocationAsString(ox, oy) + length + angle); }
String getAngleAsString() { double angle1 = 0.0; double angle2 = 0.0; if (xpf != null) { angle1 = getFloatAngle(xpf[0], ypf[0], xpf[1], ypf[1]); angle2 = getFloatAngle(xpf[1], ypf[1], xpf[2], ypf[2]); } else { angle1 = getFloatAngle(xp[0], yp[0], xp[1], yp[1]); angle2 = getFloatAngle(xp[1], yp[1], xp[2], yp[2]); } degrees = Math.abs(180 - Math.abs(angle1 - angle2)); if (degrees > 180.0) degrees = 360.0 - degrees; double degrees2 = Prefs.reflexAngle && type == ANGLE ? 360.0 - degrees : degrees; return ", angle=" + IJ.d2s(degrees2); }
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; }
/** * Zooms out by making the source rectangle (srcRect) larger and centering it on (x,y). If we * can't make it larger, then make the window smaller. */ public void zoomOut(int x, int y) { if (magnification <= 0.03125) return; double oldMag = magnification; double newMag = getLowerZoomLevel(magnification); double srcRatio = (double) srcRect.width / srcRect.height; double imageRatio = (double) imageWidth / imageHeight; double initialMag = imp.getWindow().getInitialMagnification(); if (Math.abs(srcRatio - imageRatio) > 0.05) { double scale = oldMag / newMag; int newSrcWidth = (int) Math.round(srcRect.width * scale); int newSrcHeight = (int) Math.round(srcRect.height * scale); if (newSrcWidth > imageWidth) newSrcWidth = imageWidth; if (newSrcHeight > imageHeight) newSrcHeight = imageHeight; int newSrcX = srcRect.x - (newSrcWidth - srcRect.width) / 2; int newSrcY = srcRect.y - (newSrcHeight - srcRect.height) / 2; if (newSrcX < 0) newSrcX = 0; if (newSrcY < 0) newSrcY = 0; srcRect = new Rectangle(newSrcX, newSrcY, newSrcWidth, newSrcHeight); // IJ.log(newMag+" "+srcRect+" "+dstWidth+" "+dstHeight); int newDstWidth = (int) (srcRect.width * newMag); int newDstHeight = (int) (srcRect.height * newMag); setMagnification(newMag); setMaxBounds(); // IJ.log(newDstWidth+" "+dstWidth+" "+newDstHeight+" "+dstHeight); if (newDstWidth < dstWidth || newDstHeight < dstHeight) { // IJ.log("pack"); setDrawingSize(newDstWidth, newDstHeight); imp.getWindow().pack(); } else repaint(); return; } if (imageWidth * newMag > dstWidth) { int w = (int) Math.round(dstWidth / newMag); if (w * newMag < dstWidth) w++; int h = (int) Math.round(dstHeight / newMag); if (h * newMag < dstHeight) h++; x = offScreenX(x); y = offScreenY(y); Rectangle r = new Rectangle(x - w / 2, y - h / 2, w, h); if (r.x < 0) r.x = 0; if (r.y < 0) r.y = 0; if (r.x + w > imageWidth) r.x = imageWidth - w; if (r.y + h > imageHeight) r.y = imageHeight - h; srcRect = r; } else { srcRect = new Rectangle(0, 0, imageWidth, imageHeight); setDrawingSize((int) (imageWidth * newMag), (int) (imageHeight * newMag)); // setDrawingSize(dstWidth/2, dstHeight/2); imp.getWindow().pack(); } // IJ.write(newMag + " " + srcRect.x+" "+srcRect.y+" "+srcRect.width+" "+srcRect.height+" // "+dstWidth + " " + dstHeight); setMagnification(newMag); // IJ.write(srcRect.x + " " + srcRect.width + " " + dstWidth); setMaxBounds(); repaint(); }
/** * Returns the perimeter length of ROIs created using the wand tool and the particle analyzer. The * algorithm counts edge pixels as 1 and corner pixels as sqrt(2). It does this by calculating the * total length of the ROI boundary and subtracting 2-sqrt(2) for each non-adjacent corner. For * example, a 1x1 pixel ROI has a boundary length of 4 and 2 non-adjacent edges so the perimeter * is 4-2*(2-sqrt(2)). A 2x2 pixel ROI has a boundary length of 8 and 4 non-adjacent edges so the * perimeter is 8-4*(2-sqrt(2)). */ double getTracedPerimeter() { int sumdx = 0; int sumdy = 0; int nCorners = 0; int dx1 = xp[0] - xp[nPoints - 1]; int dy1 = yp[0] - yp[nPoints - 1]; int side1 = Math.abs(dx1) + Math.abs(dy1); // one of these is 0 boolean corner = false; int nexti, dx2, dy2, side2; for (int i = 0; i < nPoints; i++) { nexti = i + 1; if (nexti == nPoints) nexti = 0; dx2 = xp[nexti] - xp[i]; dy2 = yp[nexti] - yp[i]; sumdx += Math.abs(dx1); sumdy += Math.abs(dy1); side2 = Math.abs(dx2) + Math.abs(dy2); if (side1 > 1 || !corner) { corner = true; nCorners++; } else corner = false; dx1 = dx2; dy1 = dy2; side1 = side2; } double w = 1.0, h = 1.0; if (imp != null) { Calibration cal = imp.getCalibration(); w = cal.pixelWidth; h = cal.pixelHeight; } return sumdx * w + sumdy * h - (nCorners * ((w + h) - Math.sqrt(w * w + h * h))); }
/* * For a list of r² values, find the smallest r1² values such that a "ball" * of radius r1 centered at (dx,dy,dz) includes a "ball" of radius r * centered at the origin. "Ball" refers to a 3D integer grid. */ int[] scanCube(int dx, int dy, int dz, int[] distSqValues) { final int numRadii = distSqValues.length; int[] r1Sq = new int[numRadii]; if ((dx == 0) && (dy == 0) && (dz == 0)) { for (int rSq = 0; rSq < numRadii; rSq++) { r1Sq[rSq] = Integer.MAX_VALUE; } } else { final int dxAbs = -(int) Math.abs(dx); final int dyAbs = -(int) Math.abs(dy); final int dzAbs = -(int) Math.abs(dz); for (int rSqInd = 0; rSqInd < numRadii; rSqInd++) { final int rSq = distSqValues[rSqInd]; int max = 0; final int r = 1 + (int) Math.sqrt(rSq); int scank, scankj; int dk, dkji; // int iBall; int iPlus; for (int k = 0; k <= r; k++) { scank = k * k; dk = (k - dzAbs) * (k - dzAbs); for (int j = 0; j <= r; j++) { scankj = scank + j * j; if (scankj <= rSq) { iPlus = ((int) Math.sqrt(rSq - scankj)) - dxAbs; dkji = dk + (j - dyAbs) * (j - dyAbs) + iPlus * iPlus; if (dkji > max) max = dkji; } } } r1Sq[rSqInd] = max; } } return r1Sq; }
/*------------------------------------------------------------------*/ double getInitialCausalCoefficientMirrorOnBounds(double[] c, double z, double tolerance) { double z1 = z, zn = Math.pow(z, c.length - 1); double sum = c[0] + zn * c[c.length - 1]; int horizon = c.length; if (0.0 < tolerance) { horizon = 2 + (int) (Math.log(tolerance) / Math.log(Math.abs(z))); horizon = (horizon < c.length) ? (horizon) : (c.length); } zn = zn * zn; for (int n = 1; (n < (horizon - 1)); n++) { zn = zn / z; sum = sum + (z1 + zn) * c[n]; z1 = z1 * z; } return (sum / (1.0 - Math.pow(z, 2 * c.length - 2))); } /* end getInitialCausalCoefficientMirrorOnBounds */
/* if selection is closed shape, create a circle with the same area and centroid, otherwise use<br> the Pratt method to fit a circle to the points that define the line or multi-point selection.<br> Reference: Pratt V., Direct least-squares fitting of algebraic surfaces", Computer Graphics, Vol. 21, pages 145-152 (1987).<br> Original code: Nikolai Chernov's MATLAB script for Newton-based Pratt fit.<br> (http://www.math.uab.edu/~chernov/cl/MATLABcircle.html)<br> Java version: https://github.com/mdoube/BoneJ/blob/master/src/org/doube/geometry/FitCircle.java<br> @authors Nikolai Chernov, Michael Doube, Ved Sharma */ void fitCircle(ImagePlus imp) { Roi roi = imp.getRoi(); if (roi == null) { noRoi("Fit Circle"); return; } if (roi.isArea()) { // create circle with the same area and centroid ImageProcessor ip = imp.getProcessor(); ip.setRoi(roi); ImageStatistics stats = ImageStatistics.getStatistics(ip, Measurements.AREA + Measurements.CENTROID, null); double r = Math.sqrt(stats.pixelCount / Math.PI); imp.killRoi(); int d = (int) Math.round(2.0 * r); IJ.makeOval( (int) Math.round(stats.xCentroid - r), (int) Math.round(stats.yCentroid - r), d, d); return; } Polygon poly = roi.getPolygon(); int n = poly.npoints; int[] x = poly.xpoints; int[] y = poly.ypoints; if (n < 3) { IJ.error("Fit Circle", "At least 3 points are required to fit a circle."); return; } // calculate point centroid double sumx = 0, sumy = 0; for (int i = 0; i < n; i++) { sumx = sumx + poly.xpoints[i]; sumy = sumy + poly.ypoints[i]; } double meanx = sumx / n; double meany = sumy / n; // calculate moments double[] X = new double[n], Y = new double[n]; double Mxx = 0, Myy = 0, Mxy = 0, Mxz = 0, Myz = 0, Mzz = 0; for (int i = 0; i < n; i++) { X[i] = x[i] - meanx; Y[i] = y[i] - meany; double Zi = X[i] * X[i] + Y[i] * Y[i]; Mxy = Mxy + X[i] * Y[i]; Mxx = Mxx + X[i] * X[i]; Myy = Myy + Y[i] * Y[i]; Mxz = Mxz + X[i] * Zi; Myz = Myz + Y[i] * Zi; Mzz = Mzz + Zi * Zi; } Mxx = Mxx / n; Myy = Myy / n; Mxy = Mxy / n; Mxz = Mxz / n; Myz = Myz / n; Mzz = Mzz / n; // calculate the coefficients of the characteristic polynomial double Mz = Mxx + Myy; double Cov_xy = Mxx * Myy - Mxy * Mxy; double Mxz2 = Mxz * Mxz; double Myz2 = Myz * Myz; double A2 = 4 * Cov_xy - 3 * Mz * Mz - Mzz; double A1 = Mzz * Mz + 4 * Cov_xy * Mz - Mxz2 - Myz2 - Mz * Mz * Mz; double A0 = Mxz2 * Myy + Myz2 * Mxx - Mzz * Cov_xy - 2 * Mxz * Myz * Mxy + Mz * Mz * Cov_xy; double A22 = A2 + A2; double epsilon = 1e-12; double ynew = 1e+20; int IterMax = 20; double xnew = 0; int iterations = 0; // Newton's method starting at x=0 for (int iter = 1; iter <= IterMax; iter++) { iterations = iter; double yold = ynew; ynew = A0 + xnew * (A1 + xnew * (A2 + 4. * xnew * xnew)); if (Math.abs(ynew) > Math.abs(yold)) { if (IJ.debugMode) IJ.log("Fit Circle: wrong direction: |ynew| > |yold|"); xnew = 0; break; } double Dy = A1 + xnew * (A22 + 16 * xnew * xnew); double xold = xnew; xnew = xold - ynew / Dy; if (Math.abs((xnew - xold) / xnew) < epsilon) break; if (iter >= IterMax) { if (IJ.debugMode) IJ.log("Fit Circle: will not converge"); xnew = 0; } if (xnew < 0) { if (IJ.debugMode) IJ.log("Fit Circle: negative root: x = " + xnew); xnew = 0; } } if (IJ.debugMode) IJ.log("Fit Circle: n=" + n + ", xnew=" + IJ.d2s(xnew, 2) + ", iterations=" + iterations); // calculate the circle parameters double DET = xnew * xnew - xnew * Mz + Cov_xy; double CenterX = (Mxz * (Myy - xnew) - Myz * Mxy) / (2 * DET); double CenterY = (Myz * (Mxx - xnew) - Mxz * Mxy) / (2 * DET); double radius = Math.sqrt(CenterX * CenterX + CenterY * CenterY + Mz + 2 * xnew); if (Double.isNaN(radius)) { IJ.error("Fit Circle", "Points are collinear."); return; } CenterX = CenterX + meanx; CenterY = CenterY + meany; imp.killRoi(); IJ.makeOval( (int) Math.round(CenterX - radius), (int) Math.round(CenterY - radius), (int) Math.round(2 * radius), (int) Math.round(2 * radius)); }
String getInfo(ImagePlus imp, ImageProcessor ip) { String s = new String("\n"); s += "Title: " + imp.getTitle() + "\n"; Calibration cal = imp.getCalibration(); int stackSize = imp.getStackSize(); int channels = imp.getNChannels(); int slices = imp.getNSlices(); int frames = imp.getNFrames(); int digits = imp.getBitDepth() == 32 ? 4 : 0; if (cal.scaled()) { String unit = cal.getUnit(); String units = cal.getUnits(); s += "Width: " + IJ.d2s(imp.getWidth() * cal.pixelWidth, 2) + " " + units + " (" + imp.getWidth() + ")\n"; s += "Height: " + IJ.d2s(imp.getHeight() * cal.pixelHeight, 2) + " " + units + " (" + imp.getHeight() + ")\n"; if (slices > 1) s += "Depth: " + IJ.d2s(slices * cal.pixelDepth, 2) + " " + units + " (" + slices + ")\n"; double xResolution = 1.0 / cal.pixelWidth; double yResolution = 1.0 / cal.pixelHeight; int places = Tools.getDecimalPlaces(xResolution, yResolution); if (xResolution == yResolution) s += "Resolution: " + IJ.d2s(xResolution, places) + " pixels per " + unit + "\n"; else { s += "X Resolution: " + IJ.d2s(xResolution, places) + " pixels per " + unit + "\n"; s += "Y Resolution: " + IJ.d2s(yResolution, places) + " pixels per " + unit + "\n"; } } else { s += "Width: " + imp.getWidth() + " pixels\n"; s += "Height: " + imp.getHeight() + " pixels\n"; if (stackSize > 1) s += "Depth: " + slices + " pixels\n"; } if (stackSize > 1) s += "Voxel size: " + d2s(cal.pixelWidth) + "x" + d2s(cal.pixelHeight) + "x" + d2s(cal.pixelDepth) + " " + cal.getUnit() + "\n"; else s += "Pixel size: " + d2s(cal.pixelWidth) + "x" + d2s(cal.pixelHeight) + " " + cal.getUnit() + "\n"; s += "ID: " + imp.getID() + "\n"; String zOrigin = stackSize > 1 || cal.zOrigin != 0.0 ? "," + d2s(cal.zOrigin) : ""; s += "Coordinate origin: " + d2s(cal.xOrigin) + "," + d2s(cal.yOrigin) + zOrigin + "\n"; int type = imp.getType(); switch (type) { case ImagePlus.GRAY8: s += "Bits per pixel: 8 "; String lut = "LUT"; if (imp.getProcessor().isColorLut()) lut = "color " + lut; else lut = "grayscale " + lut; if (imp.isInvertedLut()) lut = "inverting " + lut; s += "(" + lut + ")\n"; if (imp.getNChannels() > 1) s += displayRanges(imp); else s += "Display range: " + (int) ip.getMin() + "-" + (int) ip.getMax() + "\n"; break; case ImagePlus.GRAY16: case ImagePlus.GRAY32: if (type == ImagePlus.GRAY16) { String sign = cal.isSigned16Bit() ? "signed" : "unsigned"; s += "Bits per pixel: 16 (" + sign + ")\n"; } else s += "Bits per pixel: 32 (float)\n"; if (imp.getNChannels() > 1) s += displayRanges(imp); else { s += "Display range: "; double min = ip.getMin(); double max = ip.getMax(); if (cal.calibrated()) { min = cal.getCValue((int) min); max = cal.getCValue((int) max); } s += IJ.d2s(min, digits) + " - " + IJ.d2s(max, digits) + "\n"; } break; case ImagePlus.COLOR_256: s += "Bits per pixel: 8 (color LUT)\n"; break; case ImagePlus.COLOR_RGB: s += "Bits per pixel: 32 (RGB)\n"; break; } double interval = cal.frameInterval; double fps = cal.fps; if (stackSize > 1) { ImageStack stack = imp.getStack(); int slice = imp.getCurrentSlice(); String number = slice + "/" + stackSize; String label = stack.getShortSliceLabel(slice); if (label != null && label.length() > 0) label = " (" + label + ")"; else label = ""; if (interval > 0.0 || fps != 0.0) { s += "Frame: " + number + label + "\n"; if (fps != 0.0) { String sRate = Math.abs(fps - Math.round(fps)) < 0.00001 ? IJ.d2s(fps, 0) : IJ.d2s(fps, 5); s += "Frame rate: " + sRate + " fps\n"; } if (interval != 0.0) s += "Frame interval: " + ((int) interval == interval ? IJ.d2s(interval, 0) : IJ.d2s(interval, 5)) + " " + cal.getTimeUnit() + "\n"; } else s += "Image: " + number + label + "\n"; if (imp.isHyperStack()) { if (channels > 1) s += " Channel: " + imp.getChannel() + "/" + channels + "\n"; if (slices > 1) s += " Slice: " + imp.getSlice() + "/" + slices + "\n"; if (frames > 1) s += " Frame: " + imp.getFrame() + "/" + frames + "\n"; } if (imp.isComposite()) { if (!imp.isHyperStack() && channels > 1) s += " Channels: " + channels + "\n"; String mode = ((CompositeImage) imp).getModeAsString(); s += " Composite mode: \"" + mode + "\"\n"; } } if (ip.getMinThreshold() == ImageProcessor.NO_THRESHOLD) s += "No Threshold\n"; else { double lower = ip.getMinThreshold(); double upper = ip.getMaxThreshold(); int dp = digits; if (cal.calibrated()) { lower = cal.getCValue((int) lower); upper = cal.getCValue((int) upper); dp = cal.isSigned16Bit() ? 0 : 4; } s += "Threshold: " + IJ.d2s(lower, dp) + "-" + IJ.d2s(upper, dp) + "\n"; } ImageCanvas ic = imp.getCanvas(); double mag = ic != null ? ic.getMagnification() : 1.0; if (mag != 1.0) s += "Magnification: " + IJ.d2s(mag, 2) + "\n"; if (cal.calibrated()) { s += " \n"; int curveFit = cal.getFunction(); s += "Calibration Function: "; if (curveFit == Calibration.UNCALIBRATED_OD) s += "Uncalibrated OD\n"; else if (curveFit == Calibration.CUSTOM) s += "Custom lookup table\n"; else s += CurveFitter.fList[curveFit] + "\n"; double[] c = cal.getCoefficients(); if (c != null) { s += " a: " + IJ.d2s(c[0], 6) + "\n"; s += " b: " + IJ.d2s(c[1], 6) + "\n"; if (c.length >= 3) s += " c: " + IJ.d2s(c[2], 6) + "\n"; if (c.length >= 4) s += " c: " + IJ.d2s(c[3], 6) + "\n"; if (c.length >= 5) s += " c: " + IJ.d2s(c[4], 6) + "\n"; } s += " Unit: \"" + cal.getValueUnit() + "\"\n"; } else s += "Uncalibrated\n"; FileInfo fi = imp.getOriginalFileInfo(); if (fi != null) { if (fi.url != null && !fi.url.equals("")) s += "URL: " + fi.url + "\n"; else if (fi.directory != null && fi.fileName != null) s += "Path: " + fi.directory + fi.fileName + "\n"; } ImageWindow win = imp.getWindow(); if (win != null) { Point loc = win.getLocation(); Dimension screen = IJ.getScreenSize(); s += "Screen location: " + loc.x + "," + loc.y + " (" + screen.width + "x" + screen.height + ")\n"; } Overlay overlay = imp.getOverlay(); if (overlay != null) { String hidden = imp.getHideOverlay() ? " (hidden)" : " "; int n = overlay.size(); String elements = n == 1 ? " element" : " elements"; s += "Overlay: " + n + elements + (imp.getHideOverlay() ? " (hidden)" : "") + "\n"; } else s += "No Overlay\n"; Roi roi = imp.getRoi(); if (roi == null) { if (cal.calibrated()) s += " \n"; s += "No Selection\n"; } else if (roi instanceof EllipseRoi) { s += "\nElliptical Selection\n"; double[] p = ((EllipseRoi) roi).getParams(); double dx = p[2] - p[0]; double dy = p[3] - p[1]; double major = Math.sqrt(dx * dx + dy * dy); s += " Major: " + IJ.d2s(major, 2) + "\n"; s += " Minor: " + IJ.d2s(major * p[4], 2) + "\n"; s += " X1: " + IJ.d2s(p[0], 2) + "\n"; s += " Y1: " + IJ.d2s(p[1], 2) + "\n"; s += " X2: " + IJ.d2s(p[2], 2) + "\n"; s += " Y2: " + IJ.d2s(p[3], 2) + "\n"; s += " Aspect ratio: " + IJ.d2s(p[4], 2) + "\n"; } else { s += " \n"; s += roi.getTypeAsString() + " Selection"; String points = null; if (roi instanceof PointRoi) { int npoints = ((PolygonRoi) roi).getNCoordinates(); String suffix = npoints > 1 ? "s)" : ")"; points = " (" + npoints + " point" + suffix; } String name = roi.getName(); if (name != null) { s += " (\"" + name + "\")"; if (points != null) s += "\n " + points; } else if (points != null) s += points; s += "\n"; Rectangle r = roi.getBounds(); if (roi instanceof Line) { Line line = (Line) roi; s += " X1: " + IJ.d2s(line.x1d * cal.pixelWidth) + "\n"; s += " Y1: " + IJ.d2s(yy(line.y1d, imp) * cal.pixelHeight) + "\n"; s += " X2: " + IJ.d2s(line.x2d * cal.pixelWidth) + "\n"; s += " Y2: " + IJ.d2s(yy(line.y2d, imp) * cal.pixelHeight) + "\n"; } else if (cal.scaled()) { s += " X: " + IJ.d2s(cal.getX(r.x)) + " (" + r.x + ")\n"; s += " Y: " + IJ.d2s(cal.getY(r.y, imp.getHeight())) + " (" + r.y + ")\n"; s += " Width: " + IJ.d2s(r.width * cal.pixelWidth) + " (" + r.width + ")\n"; s += " Height: " + IJ.d2s(r.height * cal.pixelHeight) + " (" + r.height + ")\n"; } else { s += " X: " + r.x + "\n"; s += " Y: " + yy(r.y, imp) + "\n"; s += " Width: " + r.width + "\n"; s += " Height: " + r.height + "\n"; } } return s; }
private void geterrors() { GenericDialog gd = new GenericDialog("Options"); float conf = 0.67f; gd.addNumericField("Confidence Limit", (int) (conf * 100.0f), 5, 10, null); gd.addChoice("Error Parameter", paramsnames, paramsnames[0]); double spacing = 0.01; gd.addNumericField("Chi^2 plot spacing (% of value)?", spacing * 100.0, 2, 10, null); boolean globalerror = false; gd.addCheckbox("Global Fit Error?", globalerror); int dataset = 0; gd.addNumericField("Data Set (for Global Error)", dataset, 0); gd.showDialog(); if (gd.wasCanceled()) { return; } conf = 0.01f * (float) gd.getNextNumber(); int paramindex = (int) gd.getNextChoiceIndex(); spacing = 0.01 * gd.getNextNumber(); globalerror = gd.getNextBoolean(); dataset = (int) gd.getNextNumber(); if (globalerror) { support_plane_errors erclass = new support_plane_errors(this, 0.0001, 50, true, 0.1); int[] erindeces = {paramindex, dataset}; // need to set up all the matrices int nsel = 0; int nparams = 11; for (int i = 0; i < ncurves; i++) { if (include[i]) { nsel++; } } double[][] params = new double[nsel][nparams]; String[][] tempformulas = new String[nsel][nparams]; double[][][] constraints = new double[2][nsel][nparams]; int[][] vflmatrix = new int[nsel][nparams]; float[][] tempdata = new float[nsel][xpts * ypts]; float[][] tempweights = new float[nsel][xpts * ypts]; int nfit = 0; int counter = 0; for (int i = 0; i < ncurves; i++) { if (include[i]) { for (int j = 0; j < nparams; j++) { params[counter][j] = globalparams[i][j]; tempformulas[counter][j] = globalformulas[i][j]; constraints[0][counter][j] = globalconstraints[0][i][j]; constraints[1][counter][j] = globalconstraints[1][i][j]; vflmatrix[counter][j] = globalvflmatrix[i][j]; if (vflmatrix[counter][j] == 0 || (j == 0 && vflmatrix[counter][j] == 2)) { nfit++; } } for (int j = 0; j < xpts; j++) { for (int k = 0; k < ypts; k++) { tempdata[counter][j + k * xpts] = (float) ((double) pch[i][j][k] / (double) nmeas[i]); tempweights[counter][j + k * xpts] = weights[i][j][k]; } } counter++; } } int dofnum = xpts * ypts * nsel - (nfit - 1) - 1; int dofden = xpts * ypts * nsel - nfit - 1; // double flim=FLimit(dofnum,dofden,(double)conf); double flim = (new jdist()).FLimit(dofnum, dofden, (double) conf); IJ.log("FLimit = " + (float) flim); if (flim == Double.NaN && flim < 1.0) { IJ.showMessage("Invalid Limiting F Value"); return; } double truespacing = Math.abs(params[erindeces[1]][erindeces[0]] * spacing); double[][] c2plot = erclass.geterrorsglobal( params, vflmatrix, tempformulas, paramsnames, constraints, tempdata, tempweights, flim, truespacing, erindeces); IJ.log("upper limit = " + c2plot[1][0] + " lower limit = " + c2plot[0][0]); int templength = c2plot[0].length; float[][] c2plotf = new float[2][templength - 1]; for (int i = 0; i < (templength - 1); i++) { c2plotf[0][i] = (float) c2plot[0][i + 1]; c2plotf[1][i] = (float) c2plot[1][i + 1]; } new PlotWindow4( "c2 plot", paramsnames[paramindex] + "[" + dataset + "]", "Chi^2", c2plotf[0], c2plotf[1]) .draw(); } else { support_plane_errors erclass = new support_plane_errors(this, 0.0001, 50, false, 0.1); int errindex = paramindex; float[] tempdata = new float[xpts * ypts]; float[] tempweights = new float[xpts * ypts]; for (int i = 0; i < xpts; i++) { for (int j = 0; j < ypts; j++) { tempdata[i + j * xpts] = (float) ((double) avg[i][j] / (double) nmeas[ncurves]); tempweights[i + j * xpts] = avgweights[i][j]; } } int nfit = 0; for (int i = 0; i < 7; i++) { if (avgfixes[i] == 0) { nfit++; } } int dofnum = xpts * ypts - (nfit - 1) - 1; int dofden = xpts * ypts - nfit - 1; double flim = (new jdist()).FLimit(dofnum, dofden, (double) conf); IJ.log("FLimit = " + (float) flim); if (flim == Double.NaN && flim < 1.0) { IJ.showMessage("Invalid Limiting F Value"); return; } double truespacing = Math.abs(avgparams[errindex] * spacing); double[][] c2plot = erclass.geterrors( avgparams, avgfixes, avgconstraints, tempdata, tempweights, flim, truespacing, errindex); IJ.log("upper limit = " + c2plot[1][0] + " lower limit = " + c2plot[0][0]); int templength = c2plot[0].length; float[][] c2plotf = new float[2][templength - 1]; for (int i = 0; i < (templength - 1); i++) { c2plotf[0][i] = (float) c2plot[0][i + 1]; c2plotf[1][i] = (float) c2plot[1][i + 1]; } new PlotWindow4("c2 plot", paramsnames[errindex], "Chi^2", c2plotf[0], c2plotf[1]).draw(); } }
/** * LocalThicknesstoCleanedUpLocalThickness * * <p>Input: 3D Local Thickness map (32-bit stack) * * <p>Output: Same as input with border voxels corrected for "jaggies." Non-background voxels * adjacent to background voxels are have their local thickness values replaced by the average of * their non-background neighbors that do not border background points. Bob Dougherty August 1, * 2007 * * <ul> * <li>August 10. Version 3 This version also multiplies the local thickness by 2 to conform * with the official definition of local thickness. * </ul> */ private ImagePlus localThicknesstoCleanedUpLocalThickness(ImagePlus imp, float[][] s) { final int w = imp.getWidth(); final int h = imp.getHeight(); final int d = imp.getStackSize(); IJ.showStatus("Cleaning up local thickness..."); // Create 32 bit floating point stack for output, sNew. ImageStack newStack = new ImageStack(w, h); sNew = new float[d][]; for (int k = 0; k < d; k++) { ImageProcessor ipk = new FloatProcessor(w, h); newStack.addSlice(null, ipk); sNew[k] = (float[]) ipk.getPixels(); } /* * First set the output array to flags: 0 for a background point -1 for * a non-background point that borders a background point s (input data) * for an interior non-background point */ for (int k = 0; k < d; k++) { for (int j = 0; j < h; j++) { final int wj = w * j; for (int i = 0; i < w; i++) { sNew[k][i + wj] = setFlag(s, i, j, k, w, h, d); } // i } // j } // k /* * Process the surface points. Initially set results to negative values * to be able to avoid including them in averages of for subsequent * points. During the calculation, positive values in sNew are interior * non-background local thicknesses. Negative values are surface points. * In this case the value might be -1 (not processed yet) or -result, * where result is the average of the neighboring interior points. * Negative values are excluded from the averaging. */ for (int k = 0; k < d; k++) { for (int j = 0; j < h; j++) { final int wj = w * j; for (int i = 0; i < w; i++) { final int ind = i + wj; if (sNew[k][ind] == -1) { sNew[k][ind] = -averageInteriorNeighbors(s, i, j, k, w, h, d); } } // i } // j } // k // Fix the negative values and double the results for (int k = 0; k < d; k++) { for (int j = 0; j < h; j++) { final int wj = w * j; for (int i = 0; i < w; i++) { final int ind = i + wj; sNew[k][ind] = (float) Math.abs(sNew[k][ind]); } // i } // j } // k IJ.showStatus("Clean Up Local Thickness complete"); String title = stripExtension(imp.getTitle()); ImagePlus impOut = new ImagePlus(title + "_CL", newStack); final double vW = imp.getCalibration().pixelWidth; // calibrate the pixel values to pixel width // so that thicknesses represent real units (not pixels) for (int z = 0; z < d; z++) { impOut.setSlice(z + 1); impOut.getProcessor().multiply(vW); } return impOut; }
public boolean get_errors(double[] params, int[] fixes) { GenericDialog gd = new GenericDialog("Error Options"); String[] methods = {"Support Plane", "Monte Carlo"}; gd.addChoice("Method", methods, methods[0]); float conf = 0.67f; gd.addNumericField("SP_Confidence Limit (%)", (int) (conf * 100.0f), 5, 10, null); String[] labels = {"P1", "P2", "P3", "P4", "P5", "P6", "P7", "P8", "P9", "P10"}; gd.addChoice("SP_Parameter", labels, labels[0]); double spacing = 0.01; gd.addNumericField("SP_Chi^2_plot_spacing (% of value)?", spacing * 100.0, 2, 10, null); int ntrials = 100; gd.addNumericField("MC_#_Trials", ntrials, 0); gd.showDialog(); if (gd.wasCanceled()) { return false; } int methodindex = gd.getNextChoiceIndex(); conf = 0.01f * (float) gd.getNextNumber(); int paramindex = gd.getNextChoiceIndex(); spacing = 0.01 * gd.getNextNumber(); ntrials = (int) gd.getNextNumber(); if (methodindex == 0) { support_plane_errors_v2 erclass = new support_plane_errors_v2(this, 0.0001, 50, false, 0.1); int errindex = paramindex; int nfit = 0; for (int i = 0; i < labels.length; i++) { if (fixes[i] == 0) { nfit++; } } int npts = tempdata.length; int dofnum = npts - (nfit - 1) - 1; int dofden = npts - nfit - 1; double flim = (new jdist()).FLimit(dofnum, dofden, (double) conf); IJ.log("FLimit = " + (float) flim); if (flim == Double.NaN && flim < 1.0) { IJ.showMessage("Invalid Limiting F Value"); return false; } double truespacing = Math.abs(params[errindex] * spacing); double[][] c2plot = erclass.geterrors( params, fixes, constraints, tempdata, weights, flim, truespacing, errindex); IJ.log("upper limit = " + c2plot[1][0] + " lower limit = " + c2plot[0][0]); IJ.log( "upper error = " + (c2plot[1][0] - params[errindex]) + " lower error = " + (params[errindex] - c2plot[0][0])); int templength = c2plot[0].length; float[][] c2plotf = new float[2][templength - 1]; for (int i = 0; i < (templength - 1); i++) { c2plotf[0][i] = (float) c2plot[0][i + 1]; c2plotf[1][i] = (float) c2plot[1][i + 1]; } new PlotWindow4("c2 plot", labels[errindex], "Chi^2", c2plotf[0], c2plotf[1]).draw(); } else { StringBuffer sb = new StringBuffer(); sb.append("Trial\t"); for (int i = 0; i < labels.length; i++) { if (fixes[i] == 0) sb.append(labels[i] + "\t"); } sb.append("chi^2"); tw = new TextWindow("Monte Carlo Results", sb.toString(), "", 400, 400); redirect = true; monte_carlo_errors_v2 erclass = new monte_carlo_errors_v2(this, 0.0001, 50, false, 0.1); double[][] errors = erclass.geterrors(params, fixes, constraints, tempdata, weights, ntrials); sb = new StringBuffer(); sb.append("StDev\t"); for (int i = 0; i < errors.length; i++) { float[] ferr = new float[errors[0].length]; for (int j = 0; j < ferr.length; j++) ferr[j] = (float) errors[i][j]; float stdev = jstatistics.getstatistic("StDev", ferr, null); sb.append("" + stdev); if (i < (errors.length - 1)) sb.append("\t"); } tw.append(sb.toString()); redirect = false; } return true; }
void jacobi(double[][] a, int n, double[] d, double[][] v) /* a matrice de depart, n taille du systeme, d valeurs propres v matrice de passage : vecteur propre normalises */ { int j, iq, ip, i; double tresh, theta, tau, t, sm, s, h, g, c; double[] b = new double[3]; double[] z = new double[3]; for (ip = 0; ip < n; ip++) { for (iq = 0; iq < n; iq++) { v[ip][iq] = 0.0; } v[ip][ip] = 1.0; } for (ip = 0; ip < n; ip++) { b[ip] = d[ip] = a[ip][ip]; z[ip] = 0.0; } // int nrot=0; for (i = 1; i <= 50; i++) { sm = 0.0; for (ip = 0; ip < n - 1; ip++) { for (iq = ip + 1; iq < n; iq++) sm += Math.abs(a[ip][iq]); } if (sm == 0.0) { return; } if (i < 4) tresh = 0.2 * sm / (n * n); else tresh = 0.0; for (ip = 0; ip < n - 1; ip++) { for (iq = ip + 1; iq < n; iq++) { g = 100.0 * Math.abs(a[ip][iq]); if (i > 4 && Math.abs(d[ip]) + g == Math.abs(d[ip]) && Math.abs(d[iq]) + g == Math.abs(d[iq])) a[ip][iq] = 0.0; else if (Math.abs(a[ip][iq]) > tresh) { h = d[iq] - d[ip]; if (Math.abs(h) + g == Math.abs(h)) t = (a[ip][iq]) / h; else { theta = 0.5 * h / (a[ip][iq]); t = 1.0 / (Math.abs(theta) + Math.sqrt(1.0 + theta * theta)); if (theta < 0.0) t = -t; } c = 1.0 / Math.sqrt(1 + t * t); s = t * c; tau = s / (1.0 + c); h = t * a[ip][iq]; z[ip] -= h; z[iq] += h; d[ip] -= h; d[iq] += h; a[ip][iq] = 0.0; for (j = 0; j <= ip - 1; j++) { ROTATE(a, j, ip, j, iq, tau, s); } for (j = ip + 1; j <= iq - 1; j++) { ROTATE(a, ip, j, j, iq, tau, s); } for (j = iq + 1; j < n; j++) { ROTATE(a, ip, j, iq, j, tau, s); } for (j = 0; j < n; j++) { ROTATE(v, j, ip, j, iq, tau, s); } // ++nrot; } } } for (ip = 0; ip < n; ip++) { b[ip] += z[ip]; d[ip] = b[ip]; z[ip] = 0.0; } } }
public void Calc_5Fr(ImagePlus imp1, ImagePlus imp2) { if (imp1.getType() != imp2.getType()) { error(); return; } if (imp1.getType() == 0) { // getType returns 0 for 8-bit, 1 for 16-bit bitDepth = "8-bit"; Prefs.set("ps.bitDepth", bitDepth); } else { bitDepth = "16-bit"; Prefs.set("ps.bitDepth", bitDepth); } int width = imp1.getWidth(); int height = imp1.getHeight(); if (width != imp2.getWidth() || height != imp2.getHeight()) { error(); return; } ImageStack stack1 = imp1.getStack(); // if (bgStackTitle != "NoBg") ImageStack stack2 = imp2.getStack(); ImageStack stack2 = imp2.getStack(); ImageProcessor ip = imp1.getProcessor(); int dimension = width * height; byte[] pixB; short[] pixS; float[][] pixF = new float[5][dimension]; float[][] pixFBg = new float[5][dimension]; float a; float b; float den; float aSmp; float bSmp; float denSmp; float aBg; float bBg; float denBg; float retF; float azimF; byte[] retB = new byte[dimension]; short[] retS = new short[dimension]; byte[] azimB = new byte[dimension]; short[] azimS = new short[dimension]; // Derived Variables: float swingAngle = 2f * (float) Math.PI * swing; float tanSwingAngleDiv2 = (float) Math.tan(swingAngle / 2.f); float tanSwingAngleDiv2DivSqrt2 = (float) (Math.tan(swingAngle / 2.f) / Math.sqrt(2)); float wavelengthDiv2Pi = wavelength / (2f * (float) Math.PI); // get the pixels of each slice in the stack and convert to float for (int i = 0; i < 5; i++) { if (bitDepth == "8-bit") { pixB = (byte[]) stack1.getPixels(i + 3); for (int j = 0; j < dimension; j++) pixF[i][j] = 0xff & pixB[j]; if (bgStackTitle != "NoBg") { pixB = (byte[]) stack2.getPixels(i + 3); for (int j = 0; j < dimension; j++) pixFBg[i][j] = 0xff & pixB[j]; } } else { pixS = (short[]) stack1.getPixels(i + 3); for (int j = 0; j < dimension; j++) pixF[i][j] = (float) pixS[j]; if (bgStackTitle != "NoBg") { pixS = (short[]) stack2.getPixels(i + 3); for (int j = 0; j < dimension; j++) pixFBg[i][j] = (float) pixS[j]; } } } // Algorithm // terms a and b for (int j = 0; j < dimension; j++) { denSmp = (pixF[1][j] + pixF[2][j] + pixF[3][j] + pixF[4][j] - 4 * pixF[0][j]) / 2; denBg = denSmp; a = (pixF[4][j] - pixF[1][j]); aSmp = a; aBg = a; b = (pixF[2][j] - pixF[3][j]); bSmp = b; bBg = b; if (bgStackTitle != "NoBg") { denBg = (pixFBg[1][j] + pixFBg[2][j] + pixFBg[3][j] + pixFBg[4][j] - 4 * pixFBg[0][j]) / 2; aBg = pixFBg[4][j] - pixFBg[1][j]; bBg = pixFBg[2][j] - pixFBg[3][j]; } // Special case of sample retardance half wave, denSmp = 0 if (denSmp == 0) { retF = (float) wavelength / 4; azimF = (float) (a == 0 & b == 0 ? 0 : (azimRef + 90 + 90 * Math.atan2(a, b) / Math.PI) % 180); } else { // Retardance, the background correction can be improved by separately considering sample // retardance values larger than a quarter wave if (bgStackTitle != "NoBg") { a = aSmp / denSmp - aBg / denBg; b = bSmp / denSmp - bBg / denBg; } else { a = aSmp / denSmp; b = bSmp / denSmp; } retF = (float) Math.atan(tanSwingAngleDiv2 * Math.sqrt(a * a + b * b)); if (denSmp < 0) retF = (float) Math.PI - retF; retF = retF * wavelengthDiv2Pi; // convert to nm if (retF > retCeiling) retF = retCeiling; // Orientation if ((bgStackTitle == "NoBg") || ((bgStackTitle != "NoBg") && (Math.abs(denSmp) < 1))) { a = aSmp; b = bSmp; } azimF = (float) (a == 0 & b == 0 ? 0 : (azimRef + 90 + 90 * Math.atan2(a, b) / Math.PI) % 180); } if (bitDepth == "8-bit") retB[j] = (byte) (((int) (255 * retF / retCeiling)) & 0xff); else retS[j] = (short) (4095 * retF / retCeiling); if (mirror == "Yes") azimF = 180 - azimF; if (bitDepth == "8-bit") azimB[j] = (byte) (((int) azimF) & 0xff); else azimS[j] = (short) (azimF * 10f); } // show the resulting images in slice 1 and 2 imp1.setSlice(3); if (bitDepth == "8-bit") { stack1.setPixels(retB, 1); stack1.setPixels(azimB, 2); } else { stack1.setPixels(retS, 1); stack1.setPixels(azimS, 2); } imp1.setSlice(1); IJ.selectWindow(imp1.getTitle()); Prefs.set("ps.sampleStackTitle", sampleStackTitle); Prefs.set("ps.bgStackTitle", bgStackTitle); Prefs.set("ps.mirror", mirror); Prefs.set("ps.wavelength", wavelength); Prefs.set("ps.swing", swing); Prefs.set("ps.retCeiling", retCeiling); Prefs.set("ps.azimRef", azimRef); Prefs.savePreferences(); }