use of ij.ImagePlus in project GDSC-SMLM by aherbert.
the class DrawClusters method run.
/*
* (non-Javadoc)
*
* @see ij.plugin.PlugIn#run(java.lang.String)
*/
public void run(String arg) {
SMLMUsageTracker.recordPlugin(this.getClass(), arg);
if (MemoryPeakResults.isMemoryEmpty()) {
IJ.error(TITLE, "No localisations in memory");
return;
}
if (!showDialog())
return;
// Load the results
MemoryPeakResults results = ResultsManager.loadInputResults(inputOption, false);
if (results == null || results.size() == 0) {
IJ.error(TITLE, "No results could be loaded");
return;
}
// Get the traces
Trace[] traces = TraceManager.convert(results);
if (traces == null || traces.length == 0) {
IJ.error(TITLE, "No traces could be loaded");
return;
}
// Filter traces to a min size
int maxFrame = 0;
int count = 0;
final int myMaxSize = (maxSize < minSize) ? Integer.MAX_VALUE : maxSize;
final boolean myDrawLines = (myMaxSize < 2) ? false : drawLines;
for (int i = 0; i < traces.length; i++) {
if (expandToSingles)
traces[i].expandToSingles();
if (traces[i].size() >= minSize && traces[i].size() <= myMaxSize) {
traces[count++] = traces[i];
traces[i].sort();
if (maxFrame < traces[i].getTail().getFrame())
maxFrame = traces[i].getTail().getFrame();
}
}
if (count == 0) {
IJ.error(TITLE, "No traces achieved the size limits");
return;
}
String msg = String.format(TITLE + ": %d / %s (%s)", count, Utils.pleural(traces.length, "trace"), Utils.pleural(results.size(), "localisation"));
IJ.showStatus(msg);
//Utils.log(msg);
Rectangle bounds = results.getBounds(true);
ImagePlus imp = WindowManager.getImage(title);
boolean isUseStackPosition = useStackPosition;
if (imp == null) {
// Create a default image using 100 pixels as the longest edge
double maxD = (bounds.width > bounds.height) ? bounds.width : bounds.height;
int w, h;
if (maxD == 0) {
// Note that imageSize can be zero (for auto sizing)
w = h = (imageSize == 0) ? 20 : imageSize;
} else {
// Note that imageSize can be zero (for auto sizing)
if (imageSize == 0) {
w = bounds.width;
h = bounds.height;
} else {
w = (int) (imageSize * bounds.width / maxD);
h = (int) (imageSize * bounds.height / maxD);
}
}
ByteProcessor bp = new ByteProcessor(w, h);
if (isUseStackPosition) {
ImageStack stack = new ImageStack(w, h, maxFrame);
for (int i = 1; i <= maxFrame; i++) // Do not clone as the image is empty
stack.setPixels(bp.getPixels(), i);
imp = Utils.display(TITLE, stack);
} else
imp = Utils.display(TITLE, bp);
// Enlarge
ImageWindow iw = imp.getWindow();
for (int i = 9; i-- > 0 && iw.getWidth() < 500 && iw.getHeight() < 500; ) {
iw.getCanvas().zoomIn(imp.getWidth() / 2, imp.getHeight() / 2);
}
} else {
// Check if the image has enough frames for all the traces
if (maxFrame > imp.getNFrames())
isUseStackPosition = false;
}
final float xScale = (float) (imp.getWidth() / bounds.getWidth());
final float yScale = (float) (imp.getHeight() / bounds.getHeight());
// Create ROIs and store data to sort them
Roi[] rois = new Roi[count];
int[][] frames = (isUseStackPosition) ? new int[count][] : null;
int[] indices = Utils.newArray(count, 0, 1);
double[] values = new double[count];
for (int i = 0; i < count; i++) {
Trace trace = traces[i];
int nPoints = trace.size();
float[] xPoints = new float[nPoints];
float[] yPoints = new float[nPoints];
int j = 0;
if (isUseStackPosition)
frames[i] = new int[nPoints];
for (PeakResult result : trace.getPoints()) {
xPoints[j] = (result.getXPosition() - bounds.x) * xScale;
yPoints[j] = (result.getYPosition() - bounds.y) * yScale;
if (isUseStackPosition)
frames[i][j] = result.getFrame();
j++;
}
Roi roi;
if (myDrawLines) {
roi = new PolygonRoi(xPoints, yPoints, nPoints, Roi.POLYLINE);
if (splineFit)
((PolygonRoi) roi).fitSpline();
} else {
roi = new PointRoi(xPoints, yPoints, nPoints);
((PointRoi) roi).setShowLabels(false);
}
rois[i] = roi;
switch(sort) {
case 0:
default:
break;
case // Sort by ID
1:
values[i] = traces[i].getId();
break;
case // Sort by time
2:
values[i] = traces[i].getHead().getFrame();
break;
case // Sort by size descending
3:
values[i] = -traces[i].size();
break;
case // Sort by length descending
4:
values[i] = -roi.getLength();
break;
case // Mean Square Displacement
5:
values[i] = -traces[i].getMSD();
break;
case // Mean / Frame
6:
values[i] = -traces[i].getMeanPerFrame();
break;
}
}
if (sort > 0)
Sort.sort(indices, values);
// Draw the traces as ROIs on an overlay
Overlay o = new Overlay();
LUT lut = LUTHelper.createLUT(DrawClusters.lut);
final double scale = 256.0 / count;
if (isUseStackPosition) {
// Add the tracks on the frames containing the results
final boolean isHyperStack = imp.isDisplayedHyperStack();
for (int i = 0; i < count; i++) {
final int index = indices[i];
final Color c = LUTHelper.getColour(lut, (int) (i * scale));
final PolygonRoi roi = (PolygonRoi) rois[index];
roi.setFillColor(c);
roi.setStrokeColor(c);
final FloatPolygon fp = roi.getNonSplineFloatPolygon();
// For each frame in the track, add the ROI track and a point ROI for the current position
for (int j = 0; j < frames[index].length; j++) {
addToOverlay(o, (Roi) roi.clone(), isHyperStack, frames[index][j]);
//PointRoi pointRoi = new PointRoi(pos.x + fp.xpoints[j], pos.y + fp.ypoints[j]);
PointRoi pointRoi = new PointRoi(fp.xpoints[j], fp.ypoints[j]);
pointRoi.setPointType(3);
pointRoi.setFillColor(c);
pointRoi.setStrokeColor(Color.black);
addToOverlay(o, pointRoi, isHyperStack, frames[index][j]);
}
}
} else {
// Add the tracks as a single overlay
for (int i = 0; i < count; i++) {
final Roi roi = rois[indices[i]];
roi.setStrokeColor(new Color(lut.getRGB((int) (i * scale))));
o.add(roi);
}
}
imp.setOverlay(o);
IJ.showStatus(msg);
}
use of ij.ImagePlus in project GDSC-SMLM by aherbert.
the class DriftCalculator method showStackDialog.
private ImageStack showStackDialog(String[] stackTitles) {
ExtendedGenericDialog gd = new ExtendedGenericDialog(TITLE);
gd.addHelp(About.HELP_URL);
gd.addMessage("Compute the drift using a reference stack alignment");
gd.addChoice("Stack_image", stackTitles, stackTitle);
gd.addMessage("Frame = previous + spacing");
gd.addNumericField("Start_frame", startFrame, 0);
gd.addSlider("Frame_spacing", 1, 20, frameSpacing);
String[] methods = ImageProcessor.getInterpolationMethods();
gd.addChoice("Interpolation_method", methods, methods[interpolationMethod]);
methods = new String[] { AlignImagesFFT.SubPixelMethod.CUBIC.toString(), AlignImagesFFT.SubPixelMethod.GAUSSIAN.toString() };
gd.addChoice("Sub-pixel_method", methods, subPixelMethod.toString());
gd.showDialog();
if (gd.wasCanceled())
return null;
stackTitle = gd.getNextChoice();
startFrame = (int) gd.getNextNumber();
frameSpacing = (int) gd.getNextNumber();
interpolationMethod = gd.getNextChoiceIndex();
subPixelMethod = (gd.getNextChoiceIndex() == 0) ? AlignImagesFFT.SubPixelMethod.CUBIC : AlignImagesFFT.SubPixelMethod.GAUSSIAN;
try {
Parameters.isAboveZero("Start frame", startFrame);
Parameters.isAboveZero("Frame spacing", frameSpacing);
} catch (IllegalArgumentException e) {
IJ.error(TITLE, e.getMessage());
return null;
}
ImagePlus imp = WindowManager.getImage(stackTitle);
if (imp != null && imp.getStackSize() > 1)
return imp.getImageStack();
return null;
}
use of ij.ImagePlus in project GDSC-SMLM by aherbert.
the class SplitResults method run.
/*
* (non-Javadoc)
*
* @see ij.plugin.PlugIn#run(java.lang.String)
*/
public void run(String arg) {
SMLMUsageTracker.recordPlugin(this.getClass(), arg);
if (MemoryPeakResults.isMemoryEmpty()) {
IJ.error(TITLE, "There are no fitting results in memory");
return;
}
String[] items = Utils.getImageList(Utils.GREY_8_16);
if (items.length == 0) {
IJ.error(TITLE, "There are no suitable mask images");
return;
}
// Show a dialog allowing the results set to be filtered
ExtendedGenericDialog gd = new ExtendedGenericDialog(TITLE);
gd.addMessage("Select a dataset to split");
ResultsManager.addInput(gd, inputOption, InputSource.MEMORY);
gd.addChoice("Object_mask", items, objectMask);
gd.addCheckbox("Show_object_mask", showObjectMask);
gd.addCheckbox("Non_mask_dataset", nonMaskDataset);
gd.showDialog();
if (gd.wasCanceled())
return;
inputOption = ResultsManager.getInputSource(gd);
objectMask = gd.getNextChoice();
showObjectMask = gd.getNextBoolean();
nonMaskDataset = gd.getNextBoolean();
MemoryPeakResults results = ResultsManager.loadInputResults(inputOption, false);
if (results == null || results.size() == 0) {
IJ.error(TITLE, "No results could be loaded");
return;
}
ImagePlus imp = WindowManager.getImage(objectMask);
if (imp == null) {
IJ.error(TITLE, "No object mask could be found");
return;
}
splitResults(results, imp.getProcessor());
}
use of ij.ImagePlus in project GDSC-SMLM by aherbert.
the class SpotAnalysis method createProfile.
private void createProfile() {
if (!parametersReady()) {
return;
}
double psfWidth, blur;
// Read settings
try {
psfWidth = Double.parseDouble(widthTextField.getText());
blur = Double.parseDouble(blurTextField.getText());
gain = Double.parseDouble(gainTextField.getText());
msPerFrame = Double.parseDouble(exposureTextField.getText());
} catch (NumberFormatException e) {
IJ.error(TITLE, "Invalid numbers in the input parameters");
return;
}
ImagePlus imp = WindowManager.getImage(inputChoice.getSelectedItem());
// This should not be a problem but leave it in for now
if (imp == null || (imp.getType() != ImagePlus.GRAY8 && imp.getType() != ImagePlus.GRAY16 && imp.getType() != ImagePlus.GRAY32)) {
IJ.showMessage(TITLE, "Images must be grayscale.");
return;
}
Roi roi = imp.getRoi();
if (roi == null || !roi.isArea()) {
IJ.showMessage(TITLE, "Image must have an area ROI");
return;
}
int recommendedSize = (int) Math.ceil(8 * psfWidth);
Rectangle bounds = roi.getBounds();
if (bounds.width < recommendedSize || bounds.height < recommendedSize) {
IJ.showMessage(TITLE, String.format("Recommend using an ROI of at least %d x %d for the PSF width", recommendedSize, recommendedSize));
return;
}
// Check no existing spots are within the ROI
if (resultsWithinBounds(bounds)) {
GenericDialog gd = new GenericDialog(TITLE);
gd.enableYesNoCancel();
gd.hideCancelButton();
gd.addMessage("The results list contains a spot within the selected bounds\n \nDo you want to continue?");
gd.showDialog();
if (!gd.wasOKed())
return;
}
createProfile(imp, bounds, psfWidth, blur);
}
use of ij.ImagePlus in project GDSC-SMLM by aherbert.
the class SpotAnalysis method setup.
private void setup() {
ImagePlus imp = WindowManager.getCurrentImage();
if (imp == null)
return;
fillImagesList();
}
Aggregations