public int setup(String arg, ImagePlus imp) { this.arg = arg; this.imp = imp; IJ.register(ParticleAnalyzer.class); if (imp == null) { IJ.noImage(); return DONE; } if (imp.getBitDepth() == 24 && !isThresholdedRGB(imp)) { IJ.error( "Particle Analyzer", "RGB images must be thresholded using\n" + "Image>Adjust>Color Threshold."); return DONE; } if (!showDialog()) return DONE; int baseFlags = DOES_ALL + NO_CHANGES + NO_UNDO; int flags = IJ.setupDialog(imp, baseFlags); processStack = (flags & DOES_STACKS) != 0; slice = 0; saveRoi = imp.getRoi(); if (saveRoi != null && saveRoi.getType() != Roi.RECTANGLE && saveRoi.isArea()) polygon = saveRoi.getPolygon(); imp.startTiming(); nextFontSize = defaultFontSize; nextLineWidth = 1; return flags; }
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(); }
void lineToArea(ImagePlus imp) { Roi roi = imp.getRoi(); if (roi == null || !roi.isLine()) { IJ.error("Line to Area", "Line selection required"); return; } ImageProcessor ip2 = new ByteProcessor(imp.getWidth(), imp.getHeight()); ip2.setColor(255); if (roi.getType() == Roi.LINE && roi.getStrokeWidth() > 1) ip2.fillPolygon(roi.getPolygon()); else roi.drawPixels(ip2); // 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(); }
void areaToLine(ImagePlus imp) { Roi roi = imp.getRoi(); if (roi == null || !roi.isArea()) { IJ.error("Area to Line", "Area selection required"); return; } Polygon p = roi.getPolygon(); if (p == null) return; int type1 = roi.getType(); if (type1 == Roi.COMPOSITE) { IJ.error("Area to Line", "Composite selections cannot be converted to lines."); return; } int type2 = Roi.POLYLINE; if (type1 == Roi.OVAL || type1 == Roi.FREEROI || type1 == Roi.TRACED_ROI || ((roi instanceof PolygonRoi) && ((PolygonRoi) roi).isSplineFit())) type2 = Roi.FREELINE; Roi roi2 = new PolygonRoi(p.xpoints, p.ypoints, p.npoints, type2); imp.setRoi(roi2); }
/* 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)); }
public void run(String arg) { ImagePlus imp = WindowManager.getCurrentImage(); Calibration cal = imp.getCalibration(); GenericDialog gd = new GenericDialog("Options"); int subsize = 32; gd.addNumericField("Subregion Size (pixels)?", subsize, 0); int stepsize = 16; gd.addNumericField("Step Size?", stepsize, 0); int shift = 3; gd.addNumericField("STICS temporal Shift?", shift, 0); float xoffset = 0.0f; gd.addNumericField("X_Offset", xoffset, 5, 15, null); float yoffset = 0.0f; gd.addNumericField("Y_Offset", yoffset, 5, 15, null); float multiplier = 8.0f; gd.addNumericField("Velocity Multiplier", multiplier, 5, 15, null); float ftime = 1.0f; gd.addNumericField("Frame_Time(min)", ftime, 5, 15, null); float scaling = (float) cal.pixelWidth; gd.addNumericField("Pixel_Size(um)", scaling, 5, 15, null); boolean norm = true; gd.addCheckbox("Normalize_Vector_lengths?", norm); boolean centered = true; gd.addCheckbox("Center_Vectors?", centered); float magthresh = 0.0f; gd.addNumericField("Magnitude_Threshhold?", magthresh, 5, 15, null); int rlength = 10; gd.addNumericField("Running_avg_length", rlength, 0); int inc = 5; gd.addNumericField("Start_frame_increment", inc, 0); gd.showDialog(); if (gd.wasCanceled()) { return; } subsize = (int) gd.getNextNumber(); stepsize = (int) gd.getNextNumber(); shift = (int) gd.getNextNumber(); xoffset = (float) gd.getNextNumber(); yoffset = (float) gd.getNextNumber(); multiplier = (float) gd.getNextNumber(); ftime = (float) gd.getNextNumber(); scaling = (float) gd.getNextNumber(); norm = gd.getNextBoolean(); centered = gd.getNextBoolean(); magthresh = (float) gd.getNextNumber(); rlength = (int) gd.getNextNumber(); inc = (int) gd.getNextNumber(); int width = imp.getWidth(); int xregions = 1 + (int) (((float) width - (float) subsize) / (float) stepsize); int newwidth = xregions * subsize; int height = imp.getHeight(); int yregions = 1 + (int) (((float) height - (float) subsize) / (float) stepsize); int newheight = yregions * subsize; ImageStack stack = imp.getStack(); int slices = imp.getNSlices(); int channels = imp.getNChannels(); int frames = imp.getNFrames(); if (frames == 1) { frames = slices; slices = 1; } Roi roi = imp.getRoi(); if (roi == null) { roi = new Roi(0, 0, width, height); } STICS_map map = new STICS_map(subsize, stepsize); Object[] tseries = jutils.get3DTSeries(stack, 0, 0, frames, slices, channels); map.update_STICS_map(tseries, width, height, 0, rlength, roi.getPolygon(), shift); FloatProcessor fp = map.get_map(scaling, ftime, stepsize, centered, norm, multiplier, stepsize, magthresh); ImageStack vector_stack = new ImageStack(fp.getWidth(), fp.getHeight()); vector_stack.addSlice("", fp); float[][] vel = map.get_scaled_velocities(scaling, ftime, stepsize); ImageStack velstack = new ImageStack(map.xregions, map.yregions); velstack.addSlice("", vel[0]); velstack.addSlice("", vel[1]); int velframes = 2; IJ.showStatus("frame " + 0 + " calculated"); for (int i = inc; i < (frames - rlength); i += inc) { map.update_STICS_map(tseries, width, height, i, rlength, roi.getPolygon(), shift); FloatProcessor fp2 = map.get_map(scaling, ftime, stepsize, centered, norm, multiplier, stepsize, magthresh); vector_stack.addSlice("", fp2); vel = map.get_scaled_velocities(scaling, ftime, stepsize); velstack.addSlice("", vel[0]); velstack.addSlice("", vel[1]); velframes += 2; IJ.showStatus("frame " + i + " calculated"); } (new ImagePlus("STICS Vectors", vector_stack)).show(); ImagePlus imp3 = new ImagePlus("Velocities", velstack); imp3.setOpenAsHyperStack(true); imp3.setDimensions(2, 1, velframes / 2); new CompositeImage(imp3, CompositeImage.COLOR).show(); }