Skip to content

Commit

Permalink
DrawTableValuesPlugin.java now correctly manages spatial calibration …
Browse files Browse the repository at this point in the history
…of images
  • Loading branch information
dlegland committed Aug 18, 2023
1 parent 6a98b8d commit 2190d59
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 33 deletions.
63 changes: 30 additions & 33 deletions src/main/java/inra/ijpb/plugins/DrawTableValuesPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -59,25 +59,26 @@ public class DrawTableValuesPlugin implements PlugIn, DialogListener
static int xOffsetSave = -10;
static int yOffsetSave = -10;
static String valueHeaderNameSave = null;
static String patternSave = null;

static String patternSave = "%5.2f";

ImagePlus targetImagePlus;

ImagePlus resultPlus;
/**
* The image to overlay, initialized as the current image when plugin is run
*/
ImagePlus imagePlus;

ResultsTable table = null;

GenericDialog gd = null;

String selectedHeaderName = selectedHeaderNameSave;
boolean calibratedPosition = false;
boolean calibratedPosition = calibratedPositionSave;
String xPosHeaderName = xPosHeaderNameSave;
String yPosHeaderName = yPosHeaderNameSave;
int xOffset = xOffsetSave;
int yOffset = yOffsetSave;
String valueHeaderName = valueHeaderNameSave;
String pattern = patternSave;


/* (non-Javadoc)
* @see ij.plugin.PlugIn#run(java.lang.String)
Expand All @@ -86,11 +87,10 @@ public class DrawTableValuesPlugin implements PlugIn, DialogListener
public void run(String arg0)
{
// Work on current image, and exit if no one is open
this.targetImagePlus = IJ.getImage();
this.imagePlus = IJ.getImage();

// Check that a table window is open
TextWindow[] textWindows = IJUtils.getTableWindows();
if (textWindows.length == 0)
if (IJUtils.getTableWindows().length == 0)
{
IJ.error("Requires at least one Table window");
return;
Expand All @@ -103,25 +103,18 @@ public void run(String arg0)
// parse dialog
if (gd.wasCanceled())
return;

parseDialogOptions();
saveDialogOptions();

drawValues(this.targetImagePlus);
drawValues(this.imagePlus);
}

private GenericDialog createDialog()
{
// Get the list of windows containing tables
TextWindow[] textWindows = IJUtils.getTableWindows();
if (textWindows.length == 0)
{
IJ.error("Requires at least one Table window");
return null;
}
String[] tableNames = new String[textWindows.length];
for (int i = 0; i < textWindows.length; i++) {
tableNames[i] = textWindows[i].getTitle();
}
String[] tableNames = IJUtils.getWindowNames(textWindows);

// Choose current table
TextPanel tp = textWindows[0].getTextPanel();
Expand All @@ -138,13 +131,13 @@ private GenericDialog createDialog()

this.gd = new GenericDialog("Draw Text from Column");
gd.addChoice("Results Table:", tableNames, tableNames[0]);
gd.addCheckbox("Calibrated Position:", false);
gd.addCheckbox("Calibrated Position:", this.calibratedPosition);
gd.addChoice("X-Position:", headings, defaultHeading);
gd.addChoice("Y-Position:", headings, defaultHeading);
gd.addNumericField("X-Offset:", this.xOffset, 0, 5, "pixels");
gd.addNumericField("Y-Offset:", this.yOffset, 0, 5, "pixels");
gd.addChoice("Values:", headings, defaultHeading);
gd.addStringField("Pattern:", "%5.2f", 10);
gd.addStringField("Pattern:", this.pattern, 10);

@SuppressWarnings("unchecked")
Vector<Choice> choices = gd.getChoices();
Expand All @@ -158,7 +151,7 @@ private GenericDialog createDialog()
}

/**
* analyse dialog, and setup inner fields of the class.
* Parses dialog options, and setup inner fields of the class.
*/
private void parseDialogOptions()
{
Expand Down Expand Up @@ -269,43 +262,47 @@ private void replaceStrings(Choice choice, String[] strings, String defaultStrin
*/
public void drawValues(ImagePlus target)
{
Overlay overlay = new Overlay();
// Calibration calib = target.getCalibration();

// coordinates of labels
double[] xPos = getColumnValues(this.table, this.xPosHeaderName);
double[] yPos = getColumnValues(this.table, this.yPosHeaderName);

// convert from (optionally calibrated) coordinates to pixel coordinates
if (this.calibratedPosition)
{
Calibration calib = target.getCalibration();
for (int i = 0; i < xPos.length; i++)
{
xPos[i] = xPos[i] * calib.pixelWidth + calib.xOrigin;
yPos[i] = yPos[i] * calib.pixelHeight + calib.yOrigin;
xPos[i] = (xPos[i] - calib.xOrigin) / calib.pixelWidth;
yPos[i] = (yPos[i] - calib.yOrigin) / calib.pixelHeight;
}
}

// retrieve values to display
double[] values = getColumnValues(this.table, this.valueHeaderName);


// update overlay by creating one ROI for each label
Overlay overlay = new Overlay();
for (int i = 0; i < xPos.length; i++)
{
String text = String.format(this.pattern, values[i]);
Roi roi = new TextRoi(
xPos[i] + this.xOffset,
yPos[i] + this.yOffset,
String.format(this.pattern, values[i]));
text);
overlay.add(roi);
}

target.setOverlay(overlay);
}


private double[] getColumnValues(ResultsTable table, String heading)
private double[] getColumnValues(ResultsTable table, String colName)
{
String[] allHeaders = table.getHeadings();

// Check if column header corresponds to row label header
boolean hasRowLabels = hasRowLabelColumn(table);
if (hasRowLabels && heading.equals(allHeaders[0]))
if (hasRowLabels && colName.equals(allHeaders[0]))
{
// need to parse row label column
int nr = table.size();
Expand All @@ -319,10 +316,10 @@ private double[] getColumnValues(ResultsTable table, String heading)
}

// determine index of column
int index = table.getColumnIndex(heading);
int index = table.getColumnIndex(colName);
if (index == ResultsTable.COLUMN_NOT_FOUND)
{
throw new RuntimeException("Unable to find column index from header: " + heading);
throw new RuntimeException("Unable to find column index from header: " + colName);
}
return table.getColumnAsDoubles(index);
}
Expand Down
10 changes: 10 additions & 0 deletions src/main/java/inra/ijpb/util/IJUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,16 @@ public static final TextWindow[] getTableWindows()

return windows.toArray(new TextWindow[0]);
}

public static final String[] getWindowNames(TextWindow[] textWindows)
{
String[] names = new String[textWindows.length];
for (int i = 0; i < textWindows.length; i++)
{
names[i] = textWindows[i].getTitle();
}
return names;
}

/**
* Extracts a list of integer labels from a string representation.
Expand Down

0 comments on commit 2190d59

Please sign in to comment.