How to use Trainable WEKA segmentation in a loop within a macro?

Hi ,

I have a set of images (frames of a movie) that I want to analyze with WEKA segmentation. I hold these images in a stack. They have a number of regions that are the same for every image but are different in size and shape.

To reduce computation time I create a stack for each of the ROIs by cropping the original pic. I can do WEKA segmentation on each of these new stacks succesfully. When I do this in a loop over the ROIs, I noticed that it takes considerable time to initiate the WEKA tool, especially the loading of the class.model file. So, I want to do this only once before looping over the ROI stacks.

I noticed that the first ROI stack must be open when I load and the WEKA tool. Therefore I placed the initiation of the segementation tool inside the loop, in a part that is skipped for the subsequent loops. After segmenting the first ROI stack, my macro does not work for the next ROI stack. I can do it by pressing the “Apply clasissifier” button after af breakpoint , but when I do this by pasting the same (recorded) command into my code, the segmentation results are not generated. I tried various alternatives without success.

Can anyone help me out?

I included an example of not working code to illustrate my approach below.


P.S. I also noted that the output of the WEKA tool is “Classified image” in one case and “Classification result” in the other…

for (iROI=0; iROI<nROIs; iROI++){

selectWindow(“selected_frames_1in100-1”); //original frames stack
roiManager(“Select”, iROI);
roiName = Roi.getName();

if ((useDeviceROI == false) && (roiName == “Device”)) {print(“skipping DeviceROI”); } else {

run(“Duplicate…”, “title=selected_frames_1in100_”+roiName+" duplicate"); //Duplicate the stack and …

setBackgroundColor(0, 0, 0);
run(“Clear Outside”, “stack”);
run(“Crop”); // …crop it to handle not much more than only the ROI
run(“Select None”);

saveAs(“Tiff”, “C:/Users/Fred/Flow experiments/sample_type/selected_frames/selected_frames_1in100_”+roiName+".tif");
//exit(“Emergency Exit”);

//run(“Trainable Weka Segmentation”, “open=[C:/Users/Fred/Flow experiments/sample_type/selected_frames/selected_frames_1in100_”+roiName+".tif]");
//call(“trainableSegmentation.Weka_Segmentation.applyClassifier”,“C:/Users/Fred/Flow experiments/sample_type/selected_frames/”,“selected_frames_1in100_”+roiName+".tif",“showResults=true”,“storeResults=false”,“probabilityMaps=false”,"");
if (iROI==0) {
run(“Trainable Weka Segmentation”);
call(“trainableSegmentation.Weka_Segmentation.loadClassifier”, “C:\Users\Fred\Flow experiments\sample_type\classifier2.model”);
call(“trainableSegmentation.Weka_Segmentation.loadData”, “C:\Users\Fred\Flow experiments\sample_type\data2.arff”);
selectWindow(“Classified image”);
waitForUser(“Right After Running WEKA0”);
} else{

waitForUser(“go by hand”); //breakpoint to manually call Apply Classifier by pressing the menu button
call(“trainableSegmentation.Weka_Segmentation.applyClassifier”, “C:\Users\Fred\Flow experiments\sample_type\selected_frames”, “selected_frames_1in100_P1.tif”, “showResults=true”, “storeResults=false”, “probabilityMaps=false”, “”);
//call(“trainableSegmentation.Weka_Segmentation.applyClassifier”, “C:\Users\Fred\Flow experiments\sample_type\selected_frames”, “selected_frames_1in100_”+roiName+".tif", “showResults=true”, “storeResults=false”, “probabilityMaps=false”, “”);
waitForUser(“Right After Running WEKA10”);
//call(“trainableSegmentation.Weka_Segmentation.loadData”, “C:\Users\Fred\Flow experiments\sample_type\data2.arff”);
waitForUser(“Right After Running WEKA1a”);
selectWindow(“Classification result”);
waitForUser(“Right After Running WEKA1b”);