Skip to content

Commit

Permalink
Save and load example ROIs
Browse files Browse the repository at this point in the history
This fixes #58
  • Loading branch information
mlt committed Jan 29, 2024
1 parent 5c68807 commit 82edfbb
Show file tree
Hide file tree
Showing 2 changed files with 133 additions and 0 deletions.
60 changes: 60 additions & 0 deletions src/main/java/trainableSegmentation/WekaSegmentation.java
Original file line number Diff line number Diff line change
Expand Up @@ -7671,4 +7671,64 @@ public String getModelVersion()
}
return null;
}

/**
* Write example ROIs to file.
* @param filename to write to
* @return success status
*/
public boolean saveExamples(String filename) {
File sFile = null;
boolean saveOK = true;


IJ.log("Saving example ROIs to file...");

try {
sFile = new File(filename);
OutputStream os = new FileOutputStream(sFile);
if (sFile.getName().endsWith(".gz"))
{
os = new GZIPOutputStream(os);
}
ObjectOutputStream objectOutputStream = new ObjectOutputStream(os);
objectOutputStream.writeObject(numOfClasses);
objectOutputStream.writeObject(classLabels);
objectOutputStream.writeObject(examples);
objectOutputStream.close();
}
catch (Exception e)
{
IJ.error("Save Failed", "Error when saving example ROIs into a file");
saveOK = false;
}
if (saveOK)
IJ.log("Saved example ROIs into " + filename );

return saveOK;
}

@SuppressWarnings("unchecked")
public boolean loadExamples(String filename) {
File selected = new File(filename);
try {
InputStream is = new FileInputStream( selected );
if (selected.getName().endsWith(".gz"))
is = new GZIPInputStream(is);

ObjectInputStream objectInputStream = new ObjectInputStream(is);
numOfClasses = (int) objectInputStream.readObject();
classLabels = (String[]) objectInputStream.readObject();
examples = (Vector<ArrayList<Roi>>[]) objectInputStream.readObject();
if (examples.length != trainingImage.getNSlices()) {
IJ.log("Number of loaded slices is different!");
}
objectInputStream.close();
}
catch (Exception e) {
IJ.error("Load Failed", "Error when loading example ROIs from a file");
return false;
}
return true;
}
}
73 changes: 73 additions & 0 deletions src/main/java/trainableSegmentation/Weka_Segmentation.java
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,8 @@ public class Weka_Segmentation implements PlugIn
private java.awt.List[] exampleList = null;
/** array of buttons for adding each trace class */
private JButton [] addExampleButton = null;
private JButton saveExamplesButton = null;
private JButton loadExamplesButton = null;

// Macro recording constants (corresponding to
// static method names to be called)
Expand Down Expand Up @@ -242,6 +244,10 @@ public class Weka_Segmentation implements PlugIn
public static final String LOAD_DATA = "loadData";
/** name of the macro method to save the current data into an ARFF file */
public static final String SAVE_DATA = "saveData";
/** name of the macro method to load example ROIs data from file */
public static final String LOAD_EXAMPLE = "loadExample";
/** name of the macro method to save the current example ROIs data into a file */
public static final String SAVE_EXAMPLE = "saveExample";
/** name of the macro method to create a new class */
public static final String CREATE_CLASS = "createNewClass";
/** name of the macro method to launch the Weka Chooser */
Expand Down Expand Up @@ -352,6 +358,12 @@ public Weka_Segmentation()
saveDataButton.setToolTipText("Save current segmentation into an ARFF file");
saveDataButton.setEnabled(false);

loadExamplesButton = new JButton ("Load");
loadExamplesButton.setToolTipText("Load example ROIs");

saveExamplesButton = new JButton ("Save");
saveExamplesButton.setToolTipText("Save example ROIs");

addClassButton = new JButton ("Create new class");
addClassButton.setToolTipText("Add one more label to mark different areas");

Expand Down Expand Up @@ -433,6 +445,12 @@ else if(e.getSource() == loadDataButton){
else if(e.getSource() == saveDataButton){
saveTrainingData();
}
else if(e.getSource() == loadExamplesButton){
win.loadExamplesData();
}
else if(e.getSource() == saveExamplesButton){
win.saveExamplesData();
}
else if(e.getSource() == addClassButton){
addNewClass();
}
Expand Down Expand Up @@ -700,6 +718,15 @@ private class CustomWindow extends StackWindow
annotationsPanel.setBorder(BorderFactory.createTitledBorder("Labels"));
annotationsPanel.setLayout(boxAnnotation);

loadExamplesButton.addActionListener(listener);
annotationsPanel.add(loadExamplesButton, annotationsConstraints);
annotationsConstraints.gridx = 1;
saveExamplesButton.addActionListener(listener);
annotationsPanel.add(saveExamplesButton, annotationsConstraints);
annotationsConstraints.gridx = 0;
annotationsConstraints.gridwidth = GridBagConstraints.REMAINDER;
annotationsConstraints.gridy++;

for(int i = 0; i < wekaSegmentation.getNumOfClasses(); i++)
{
exampleList[i].addActionListener(listener);
Expand Down Expand Up @@ -1344,6 +1371,52 @@ public ImagePlus getTrainingImage()
{
return trainingImage;
}

/**
* Save example ROIs
*/
public void saveExamplesData() {
SaveDialog sd = new SaveDialog("Save example ROIs as...", "examples",".twse");
if (sd.getFileName()==null)
return;

// Record
String[] arg = new String[] { sd.getDirectory() + sd.getFileName() };
record(SAVE_EXAMPLE, arg);

if( !wekaSegmentation.saveExamples(sd.getDirectory() + sd.getFileName()) )
{
IJ.error("Error while writing example ROIs into a file");
return;
}
}

public void loadExamplesData() {
OpenDialog od = new OpenDialog( "Choose example ROIs file", "" );
if (od.getFileName()==null)
return;
IJ.log("Loading example ROIs from " + od.getDirectory() + od.getFileName() + "...");

// Record
String[] arg = new String[] { od.getDirectory() + od.getFileName() };
record(LOAD_EXAMPLE, arg);

if( !wekaSegmentation.loadExamples(od.getDirectory() + od.getFileName()) )
{
IJ.error("Error when loading Weka classifier from file");
return;
}

for (;numOfClasses < wekaSegmentation.getNumOfClasses(); numOfClasses++)
addClass();

displayImage.killRoi();
drawExamples();
updateExampleLists();
revalidate();

IJ.log("Loaded example ROIs " + od.getDirectory() + od.getFileName());
}
}// end class CustomWindow

/**
Expand Down

0 comments on commit 82edfbb

Please sign in to comment.