Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added Preview function for on-the-fly update ROIs #12

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/main/java/de/csbdresden/stardist/Opt.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ public class Opt {
public static final String SHOW_PROB_DIST = "Show CNN Output";
public static final String SET_THRESHOLDS = "Set optimized postprocessing thresholds (for selected model)";
public static final String RESTORE_DEFAULTS = "Restore Defaults";
public static final String PREVIEW = "Preview";

// TODO: add descriptions for all options

Expand All @@ -56,6 +57,7 @@ public class Opt {
DEFAULTS.put(VERBOSE, false);
DEFAULTS.put(CSBDEEP_PROGRESS_WINDOW, false);
DEFAULTS.put(SHOW_PROB_DIST, false);
DEFAULTS.put(PREVIEW, false);
}

static Object getDefault(final String key) {
Expand Down
68 changes: 67 additions & 1 deletion src/main/java/de/csbdresden/stardist/StarDist2D.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,15 @@
import java.util.stream.IntStream;

import javax.swing.JOptionPane;
import javax.swing.SwingUtilities;

import ij.plugin.frame.RoiManager;
import org.scijava.Cancelable;
import org.scijava.ItemIO;
import org.scijava.ItemVisibility;
import org.scijava.command.Command;
import org.scijava.command.CommandModule;
import org.scijava.command.Previewable;
import org.scijava.menu.MenuConstants;
import org.scijava.plugin.Menu;
import org.scijava.plugin.Parameter;
Expand Down Expand Up @@ -49,7 +53,7 @@
@Menu(label = "StarDist"),
@Menu(label = "StarDist 2D", weight = 1)
})
public class StarDist2D extends StarDist2DBase implements Command {
public class StarDist2D extends StarDist2DBase implements Command, Previewable, Cancelable {

@Parameter(label="", visibility=ItemVisibility.MESSAGE, initializer="checkForCSBDeep")
private final String msgTitle = "<html>" +
Expand Down Expand Up @@ -147,6 +151,12 @@ public class StarDist2D extends StarDist2DBase implements Command {
@Parameter(label=Opt.RESTORE_DEFAULTS, callback="restoreDefaults")
private Button restoreDefaults;

@Parameter(label=Opt.PREVIEW)
private boolean preview = (boolean) Opt.getDefault(Opt.PREVIEW);

// @Parameter(label=Opt.PREVIEW, callback="refreshROI")
// private Button updateROI;

// ---------

private void restoreDefaults() {
Expand All @@ -163,6 +173,7 @@ private void restoreDefaults() {
verbose = (boolean) Opt.getDefault(Opt.VERBOSE);
showCsbdeepProgress = (boolean) Opt.getDefault(Opt.CSBDEEP_PROGRESS_WINDOW);
showProbAndDist = (boolean) Opt.getDefault(Opt.SHOW_PROB_DIST);
preview = (boolean) Opt.getDefault(Opt.PREVIEW);
}

private void percentileBottomChanged() {
Expand Down Expand Up @@ -400,4 +411,59 @@ public static void main(final String... args) throws Exception {
ij.command().run(StarDist2D.class, true, params);
}

private void refresh() {
final HashMap<String, Object> params = new HashMap<>();

params.put("input", input);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For timelapse input data: should the preview only be for the current frame (how to even get that?) or the entire sequence (potentially slow)?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It depends on the actual processing logic of StarDist. Does it probably take into account of whole timeseries of the input image? Then, it will be slow as you expected. How do you handle ROIs over time in this case?

params.put("modelChoice", modelChoice);
params.put("normalizeInput", normalizeInput);
params.put("percentileBottom", percentileBottom);
params.put("percentileTop", percentileTop);
params.put("probThresh", probThresh);
params.put("nmsThresh", nmsThresh);
params.put("outputType", "ROI Manager");
params.put("nTiles", nTiles);
params.put("excludeBoundary", excludeBoundary);
params.put("roiPosition", Opt.ROI_POSITION_AUTO);

command.run(StarDist2D.class, false, params);
}

private void refreshROI() {
if (!SwingUtilities.isEventDispatchThread()) {
SwingUtilities.invokeLater(this::refresh);
} else {
refresh();
}
}

@Override
public void preview() {
if (preview) {
refreshROI();
}
}

@Override
public void cancel() {
}

@Override
public boolean isCanceled() {
return false;
}

@Override
public void cancel(String s) {
if (RoiManager.getInstance() == null) {
new RoiManager().reset();
} else {
RoiManager.getInstance().reset();
}
}

@Override
public String getCancelReason() {
return null;
}
}
7 changes: 5 additions & 2 deletions src/main/java/de/csbdresden/stardist/StarDist2DBase.java
Original file line number Diff line number Diff line change
Expand Up @@ -112,9 +112,12 @@ protected void exportROIs(Candidates polygons, int framePosition, long numFrames
roiManager.add(bboxRoi, -1);
}
}
if (roiManager.isVisible()) roiManager.repaint();
if (roiManager.isVisible()) {
roiManager.repaint();
roiManager.runCommand("show all");
}
}

protected void setRoiPosition(Roi roi, int framePosition, String roiPosition) {
switch (roiPosition) {
case Opt.ROI_POSITION_STACK:
Expand Down