use of ij.ImagePlus in project GDSC-SMLM by aherbert.
the class PeakFit method addSingleFrameOverlay.
private void addSingleFrameOverlay() {
// If a single frame was processed add the peaks as an overlay if they are in memory
ImagePlus imp = this.imp;
if (fitMaxima && singleFrame > 0) {
if (source instanceof IJImageSource) {
String title = source.getName();
imp = WindowManager.getImage(title);
}
}
if (singleFrame > 0 && imp != null) {
MemoryPeakResults results = null;
for (PeakResults r : this.results.toArray()) if (r instanceof MemoryPeakResults) {
results = (MemoryPeakResults) r;
break;
}
if (results == null || results.size() == 0)
return;
ExtendedGenericDialog gd = new ExtendedGenericDialog(TITLE);
gd.enableYesNoCancel();
gd.hideCancelButton();
gd.addMessage("Add the fitted localisations as an overlay?");
gd.showDialog();
if (!gd.wasOKed())
return;
LUT lut = LUTHelper.createLUT(LutColour.ICE);
Overlay o = new Overlay();
ArrayList<PeakResult> list = (ArrayList<PeakResult>) results.getResults();
for (int i = 0, j = results.size() - 1; i < results.size(); i++, j--) {
PeakResult r = list.get(i);
PointRoi roi = new PointRoi(r.getXPosition(), r.getYPosition());
Color c = LUTHelper.getColour(lut, j, results.size());
roi.setStrokeColor(c);
roi.setFillColor(c);
if (imp.getStackSize() > 1)
roi.setPosition(singleFrame);
o.add(roi);
}
imp.setOverlay(o);
imp.getWindow().toFront();
}
}
use of ij.ImagePlus in project GDSC-SMLM by aherbert.
the class PeakFit method initialiseFitting.
/**
* Set-up the fitting using all the configured properties. Prepare the output results.
*/
public boolean initialiseFitting() {
if (source == null)
return false;
// Do this to ensure the serialised configuration is correct
updateFitConfiguration(config);
results.setSource(source);
if (maximaIdentification)
results.setName(source.getName() + " (Maxima)");
else if (fitMaxima)
results.setName(source.getName() + " (" + getSolverName() + " Fit Maxima)");
else
results.setName(source.getName() + " (" + getSolverName() + ")");
results.setBounds(bounds);
Calibration cal = calibration.clone();
// Account for the frame integration
// TODO - Should we change this so that if integrate frames is used then the data
// are converted to ExtendedPeakResult with a start and end frame
//cal.exposureTime *= integrateFrames;
//if (interlacedData)
//{
// cal.exposureTime *= ((double)dataBlock / (dataBlock + dataSkip));
//}
results.setCalibration(cal);
results.setConfiguration(XmlUtils.toXML(config));
addMemoryResults(results, false);
addImageResults(results);
addFileResults(results);
addTableResults(results);
addDefaultResults(results);
results.begin();
if (simpleFit && showImage) {
for (PeakResults r : results.toArray()) {
if (r instanceof IJImagePeakResults) {
ImagePlus i = ((IJImagePeakResults) r).getImagePlus();
Utils.log("Super-resolution image title = " + i.getTitle());
WindowManager.toFront(i.getWindow());
}
}
}
return true;
}
use of ij.ImagePlus in project GDSC-SMLM by aherbert.
the class PCPALMMolecules method createImage.
private boolean createImage(ArrayList<Molecule> molecules) {
if (molecules.isEmpty())
return false;
// Find the limits of the image
minx = maxx = molecules.get(0).x;
miny = maxy = molecules.get(0).y;
// Compute limits
for (int i = molecules.size(); i-- > 0; ) {
final Molecule m1 = molecules.get(i);
if (minx > m1.x)
minx = m1.x;
else if (maxx < m1.x)
maxx = m1.x;
if (miny > m1.y)
miny = m1.y;
else if (maxy < m1.y)
maxy = m1.y;
}
// // Debug all-vs-all comparison
// long start = System.nanoTime();
// double dMin2 = Double.POSITIVE_INFINITY;
// for (int i = molecules.size(); i-- > 0;)
// {
// final Molecule m1 = molecules.get(i);
// for (int j = i; j-- > 0;)
// {
// final Molecule m2 = molecules.get(j);
// if (dMin2 > m1.distance2(m2))
// dMin2 = m1.distance2(m2);
// }
// }
// long t1 = System.nanoTime() - start;
// Assign to a grid
final int gridSize = 500;
final double xBinSize = (maxx - minx) / gridSize;
final double yBinSize = (maxy - miny) / gridSize;
final int nXBins = 1 + (int) ((maxx - minx) / xBinSize);
final int nYBins = 1 + (int) ((maxy - miny) / yBinSize);
Molecule[][] grid = new Molecule[nXBins][nYBins];
for (Molecule m : molecules) {
final int xBin = (int) ((m.x - minx) / xBinSize);
final int yBin = (int) ((m.y - miny) / yBinSize);
// Build a single linked list
m.next = grid[xBin][yBin];
grid[xBin][yBin] = m;
}
// Find the minimum distance between molecules.
double dMin = Double.POSITIVE_INFINITY;
IJ.showStatus("Computing minimum distance ...");
IJ.showProgress(0);
Molecule[] neighbours = new Molecule[5];
for (int yBin = 0, currentIndex = 0, finalIndex = nXBins * nYBins; yBin < nYBins; yBin++) {
for (int xBin = 0; xBin < nXBins; xBin++) {
IJ.showProgress(currentIndex, finalIndex);
for (Molecule m1 = grid[xBin][yBin]; m1 != null; m1 = m1.next) {
// Build a list of which cells to compare up to a maximum of 4
// | 0,0 | 1,0
// ------------+-----
// -1,1 | 0,1 | 1,1
int count = 0;
neighbours[count++] = m1.next;
if (yBin < nYBins - 1) {
neighbours[count++] = grid[xBin][yBin + 1];
if (xBin > 0)
neighbours[count++] = grid[xBin - 1][yBin + 1];
}
if (xBin < nXBins - 1) {
neighbours[count++] = grid[xBin + 1][yBin];
if (yBin < nYBins - 1)
neighbours[count++] = grid[xBin + 1][yBin + 1];
}
// Compare to neighbours
while (count-- > 0) {
for (Molecule m2 = neighbours[count]; m2 != null; m2 = m2.next) {
if (dMin > m1.distance2(m2))
dMin = m1.distance2(m2);
}
}
}
}
}
IJ.showStatus("");
IJ.showProgress(1);
nmPerPixel = Math.sqrt(dMin);
log("Minimum distance between molecules = %g nm", nmPerPixel);
if (nmPerPixel == 0 && nmPerPixelLimit == 0) {
IJ.error(TITLE, "Zero minimum distance between molecules - please enter a nm/pixel limit for image reconstruction");
return false;
}
if (nmPerPixel < nmPerPixelLimit) {
log("Minimum distance adjusted to user defined limit %.2f nm", nmPerPixelLimit);
nmPerPixel = nmPerPixelLimit;
}
// Compute the minimum size we can use and stay within memory.
// Assume a 4um x 4um analysis section with 800nm feature radius.
double limit = (4000.0 + 800.0 + 1) / 4096;
if (nmPerPixel < limit) {
log("Minimum distance adjusted to %.2f nm to fit in memory", limit);
nmPerPixel = limit;
}
log("X-range %.2f - %.2f : Y-range %.2f - %.2f (nm)", minx, maxx, miny, maxy);
// Construct a binary representation
String namePrefix = results.getName() + " " + ((binaryImage) ? "Binary" : "Count") + " Image";
double lowResNmPerPixel;
double xrange = maxx - minx;
double yrange = maxy - miny;
if (xrange > 0 || yrange > 0) {
lowResNmPerPixel = FastMath.max(xrange, yrange) / lowResolutionImageSize;
} else {
// The resolution does not matter
lowResNmPerPixel = 100;
}
ImagePlus imp = displayImage(namePrefix + " (low res)", molecules, minx, miny, maxx, maxy, lowResNmPerPixel, false, binaryImage);
// Add an ROI to allow the user to select regions. PC-PALM recommends 2x2 to 4x4 um^2
int size = (int) (roiSizeInUm * 1000.0 / lowResNmPerPixel);
imp.setRoi(new Rectangle(0, 0, size, size));
if (showHighResolutionImage)
displayImage(namePrefix + " (high res)", molecules, minx, miny, maxx, maxy, nmPerPixel, false, binaryImage);
// Store the molecules, the data range and the dMin.
// This will be used by a filter plugin that crops sections from the image for PC analysis
PCPALMMolecules.molecules = molecules;
return true;
}
use of ij.ImagePlus in project GDSC-SMLM by aherbert.
the class IJImagePeakResults method begin.
/*
* (non-Javadoc)
*
* @see gdsc.utils.fitting.PeakResults#begin()
*/
public void begin() {
imageActive = false;
preBegin();
// Handle invalid bounds with an empty single pixel image
boolean validBounds = imageWidth > 0 && imageHeight > 0 && (double) imageWidth * (double) imageHeight < Integer.MAX_VALUE;
int w, h;
if (validBounds) {
w = imageWidth;
h = imageHeight;
} else {
Utils.log("ERROR: Unable to create image results '%s' due to invalid dimensions: width=%d, height=%d", title, imageWidth, imageHeight);
w = h = 1;
}
size = 0;
lastPaintSize = 0;
// Let some results appear before drawing
nextRepaintSize = 20;
nextPaintTime = System.currentTimeMillis() + repaintDelay;
imageLock = false;
data = new double[w * h];
// Use negative zero so that we know when positive zero has been written to the array.
if ((displayFlags & (DISPLAY_MAPPED | DISPLAY_MAP_ZERO)) == (DISPLAY_MAPPED | DISPLAY_MAP_ZERO))
EMPTY = -0.0f;
resetData();
imp = WindowManager.getImage(title);
currentFrame = 1;
ImageProcessor ip = createNewProcessor(w, h);
if (imp == null) {
imp = new ImagePlus(title, ip);
// Apply the fire lookup table
WindowManager.setTempCurrentImage(imp);
LutLoader lut = new LutLoader();
lut.run(lutName);
WindowManager.setTempCurrentImage(null);
if (displayImage)
imp.show();
} else {
// Copy the lookup table
ip.setColorModel(imp.getProcessor().getColorModel());
ImageStack stack = createNewImageStack(w, h);
stack.addSlice(null, ip);
// If resizing then remove adornments
if (stack.getWidth() != imp.getWidth() || stack.getHeight() != imp.getHeight()) {
imp.setOverlay(null);
imp.setRoi((Roi) null);
}
imp.setStack(stack);
if (displayImage)
imp.show();
else
imp.hide();
}
imp.setProperty("Info", createInfo());
if (calibration != null) {
Calibration cal = new Calibration();
String unit = "nm";
double unitPerPixel = calibration.getNmPerPixel() / scale;
if (unitPerPixel > 100) {
unit = "um";
unitPerPixel /= 1000.0;
}
cal.setUnit(unit);
cal.pixelHeight = cal.pixelWidth = unitPerPixel;
imp.setCalibration(cal);
}
// We cannot draw anything with no bounds
imageActive = validBounds;
}
use of ij.ImagePlus in project GDSC-SMLM by aherbert.
the class ImageROIPainter method mousePressed.
public void mousePressed(MouseEvent e) {
// If a multiple-line selection is made then show all the points
int index = textPanel.getSelectionStart();
if (index == -1)
return;
int index2 = textPanel.getSelectionEnd();
if (index == index2)
return;
ImagePlus imp = WindowManager.getImage(title);
if (imp == null)
return;
// Show all
int points = 0;
float[] x = new float[index2 - index + 1];
float[] y = new float[x.length];
int[] slice = new int[x.length];
while (index <= index2) {
double[] position = coordProvider.getCoordinates(textPanel.getLine(index));
if (position == null || position.length < 3)
continue;
slice[points] = (int) position[0];
x[points] = (float) position[1];
y[points] = (float) position[2];
points++;
index++;
}
if (points == 0)
return;
// Simple code to add the ROI onto a single slice: addRoi(imp, slice[0], new PointRoi(x, y, points));
// Add the ROI to each relevant slice
// Sort the slices
int[] indices = new int[points];
for (int i = 0; i < points; i++) indices[i] = i;
Sort.sort(indices, slice);
Overlay o = new Overlay();
// Create an ROI for each slice
int start = 0;
for (int i = 0; i < points; i++) {
if (slice[indices[i]] != slice[indices[start]]) {
appendRoi(x, y, slice, indices, o, start, i);
start = i;
}
}
appendRoi(x, y, slice, indices, o, start, points);
// Choose the first slice and add the final overlay
imp.setSlice(slice[indices[start]]);
if (imp.getWindow() != null)
imp.getWindow().toFront();
o.setStrokeColor(Color.green);
imp.setOverlay(o);
}
Aggregations