use of ij.gui.Roi in project GDSC-SMLM by aherbert.
the class EMGainAnalysis method buildHistogram.
/**
* Build a histogram using pixels within the image ROI
*
* @param image
* The image
* @return The image histogram
*/
private static int[] buildHistogram(ImagePlus imp) {
ImageStack stack = imp.getImageStack();
Roi roi = imp.getRoi();
int[] data = getHistogram(stack.getProcessor(1), roi);
for (int n = 2; n <= stack.getSize(); n++) {
int[] tmp = getHistogram(stack.getProcessor(n), roi);
for (int i = 0; i < tmp.length; i++) data[i] += tmp[i];
}
// Avoid super-saturated pixels by using 98% of histogram
long sum = 0;
for (int i = 0; i < data.length; i++) {
sum += data[i];
}
final long sum2 = (long) (sum * 0.99);
sum = 0;
for (int i = 0; i < data.length; i++) {
sum += data[i];
if (sum > sum2) {
for (; i < data.length; i++) data[i] = 0;
break;
}
}
return data;
}
use of ij.gui.Roi in project GDSC-SMLM by aherbert.
the class PSFCreator method ignoreSpot.
private boolean ignoreSpot(int n, final double[] z, final double[] a, final double[] smoothA, final double[] xCoord, final double[] yCoord, final double[] sd, final double[] newZ, final double[] smoothX, final double[] smoothY, double[] smoothSd, final double cx, final double cy, final int cz, double csd) {
this.slice = cz;
// the addition of the data
if (interactiveMode) {
zCentre = cz;
psfWidth = csd * nmPerPixel;
// Store the data for replotting
this.z = z;
this.a = a;
this.smoothAz = z;
this.smoothA = smoothA;
this.xCoord = xCoord;
this.yCoord = yCoord;
this.sd = sd;
this.newZ = newZ;
this.smoothX = smoothX;
this.smoothY = smoothY;
this.smoothSd = smoothSd;
showPlots(z, a, z, smoothA, xCoord, yCoord, sd, newZ, smoothX, smoothY, smoothSd, cz);
// Draw the region on the input image as an overlay
imp.setSlice(cz);
imp.setOverlay(new Roi((int) (cx - boxRadius), (int) (cy - boxRadius), 2 * boxRadius + 1, 2 * boxRadius + 1), Color.GREEN, 1, null);
// Ask if the spot should be included
GenericDialog gd = new GenericDialog(TITLE);
gd.enableYesNoCancel();
gd.hideCancelButton();
gd.addMessage(String.format("Add spot %d to the PSF?\n \nEstimated centre using min PSF width:\n \nx = %.2f\ny = %.2f\nz = %d\nsd = %.2f\n", n, cx, cy, cz, csd));
gd.addSlider("Slice", z[0], z[z.length - 1], slice);
if (yesNoPosition != null) {
gd.centerDialog(false);
gd.setLocation(yesNoPosition);
}
gd.addDialogListener(new SimpleInteractivePlotListener());
gd.showDialog();
yesNoPosition = gd.getLocation();
return !gd.wasOKed();
}
return false;
}
use of ij.gui.Roi in project GDSC-SMLM by aherbert.
the class OverlayImage method addImage.
/**
* Adapted from ij.plugins.OverlayCommands#addImage(boolean) with the additional option for setting the zero pixels
* to transparent.
*/
void addImage() {
ImagePlus imp = IJ.getImage();
int[] wList = WindowManager.getIDList();
if (wList == null || wList.length < 2) {
IJ.error("Add Image...", "The command requires at least two open images.");
return;
}
String[] titles = new String[wList.length];
int count = 0;
for (int i = 0; i < wList.length; i++) {
ImagePlus imp2 = WindowManager.getImage(wList[i]);
if (imp2 != null && imp2 != imp && imp.getWidth() >= imp2.getWidth() && imp.getHeight() >= imp2.getHeight())
titles[count++] = imp2.getTitle();
}
if (count < 1) {
IJ.error("Add Image...", "The command requires at least one valid overlay image.");
return;
}
titles = Arrays.copyOf(titles, count);
int x = 0, y = 0;
Roi roi = imp.getRoi();
if (roi != null && roi.isArea()) {
Rectangle r = roi.getBounds();
x = r.x;
y = r.y;
}
GenericDialog gd = new GenericDialog("Add Image...");
gd.addChoice("Image to add:", titles, title);
gd.addNumericField("X location:", x, 0);
gd.addNumericField("Y location:", y, 0);
gd.addNumericField("Opacity (0-100%):", opacity, 0);
gd.addCheckbox("Transparent background", transparent);
gd.addCheckbox("Replace overlay", replace);
gd.showDialog();
if (gd.wasCanceled())
return;
title = gd.getNextChoice();
x = (int) gd.getNextNumber();
y = (int) gd.getNextNumber();
opacity = (int) gd.getNextNumber();
transparent = gd.getNextBoolean();
replace = gd.getNextBoolean();
ImagePlus overlay = WindowManager.getImage(title);
if (overlay == imp) {
IJ.error("Add Image...", "Image to be added cannot be the same as\n\"" + imp.getTitle() + "\".");
return;
}
if (overlay.getWidth() > imp.getWidth() && overlay.getHeight() > imp.getHeight()) {
IJ.error("Add Image...", "Image to be added cannnot be larger than\n\"" + imp.getTitle() + "\".");
return;
}
ImageRoi roi2 = new ImageRoi(x, y, overlay.getProcessor());
roi2.setZeroTransparent(transparent);
roi2.setName(overlay.getShortTitle());
if (opacity != 100)
roi2.setOpacity(opacity / 100.0);
Overlay overlayList = imp.getOverlay();
if (overlayList == null || replace)
overlayList = new Overlay();
overlayList.add(roi2);
imp.setOverlay(overlayList);
Undo.setup(Undo.OVERLAY_ADDITION, imp);
}
use of ij.gui.Roi in project GDSC-SMLM by aherbert.
the class PSFCreator method setup.
/*
* (non-Javadoc)
*
* @see ij.plugin.filter.PlugInFilter#setup(java.lang.String, ij.ImagePlus)
*/
public int setup(String arg, ImagePlus imp) {
SMLMUsageTracker.recordPlugin(this.getClass(), arg);
if (imp == null) {
IJ.noImage();
return DONE;
}
Roi roi = imp.getRoi();
if (roi == null || roi.getType() != Roi.POINT) {
IJ.error("Point ROI required");
return DONE;
}
this.imp = imp;
return showDialog();
}
use of ij.gui.Roi in project GDSC-SMLM by aherbert.
the class PSFCreator method getSpots.
/**
* @return Extract all the ROI points that are not within twice the box radius of any other spot
*/
private BasePoint[] getSpots() {
Roi roi = imp.getRoi();
if (roi != null && roi.getType() == Roi.POINT) {
Polygon p = ((PolygonRoi) roi).getNonSplineCoordinates();
int n = p.npoints;
Rectangle bounds = roi.getBounds();
BasePoint[] roiPoints = new BasePoint[n];
for (int i = 0; i < n; i++) {
roiPoints[i] = new BasePoint(bounds.x + p.xpoints[i], bounds.y + p.ypoints[i], 0);
}
// All vs all distance matrix
double[][] d = new double[n][n];
for (int i = 0; i < n; i++) for (int j = i + 1; j < n; j++) d[i][j] = d[j][i] = roiPoints[i].distanceXY2(roiPoints[j]);
// Spots must be twice as far apart to have no overlap of the extracted box region
double d2 = boxRadius * boxRadius * 4;
int ok = 0;
for (int i = 0; i < n; i++) {
if (noNeighbours(d, n, i, d2))
roiPoints[ok++] = roiPoints[i];
}
return Arrays.copyOf(roiPoints, ok);
}
return new BasePoint[0];
}
Aggregations