Issue with Weka Segmentation using through a macro

Hi all,

I’m using a macro, that was created through the record, to run the segmentation with Weka. It worked with ImageJ on Windows 10. But when I passed to ImageJ on Ubuntu, there are a bunch of error messages that pop up saying it can’t apply the classifier.
I’ve updated Java and Fiji but still not working.
You can find the code in below. The error says Caused by: java.lang.NullPointerException
Does anyone have an idea about this issue ?
Thank you

run("Trainable Weka Segmentation", "open=["+init_file+"]");
selectWindow("Trainable Weka Segmentation v3.2.34");
call("trainableSegmentation.Weka_Segmentation.loadClassifier", model);

for (i = 0; i < numImages; i+=1) {
	fname = "NA7_ID16B_1stpick_thetruth_NLM-UM" + IJ.pad(d2s(i,0),pad_index) + ".tif";
	call("trainableSegmentation.Weka_Segmentation.applyClassifier", pathname, fname, "showResults=false", "storeResults=true", "probabilityMaps=false", destination);
}```

Hi @Tuan-Tu_Nguyen,

I’m quite new to the world of scripting and macros, but have been trying to work on something similar to what you described these last days. I’ll see if I can help you (take my advice with a pinch of salt). I’m using Ubuntu 18.04 by the way and using Weka in a ImageJ macro .

Looking at your code, it seems to me there are some variables you have not defined, pathname and model…? Do you define those elsewhere in your macro? I’ll assume so.

For me, to get Weka working on a loop without any pop up windows there are some things to take into account:

  • when you start with
run("Trainable Weka Segmentation", "open=["+init_file+"]");

there needs to be an image open already, otherwise Weka opens a dialog box for you to choose something (if you place the code in the middle of a bigger script, I found it to be disruptive, since a user input would be necessary at that point, so I just open the first image of my loop beforehand).

  • I also add a 3s wait time before calling the model (“classifier” variable needs to be defined earlier too):
run("Trainable Weka Segmentation");
wait( 3000 ); 
selectWindow("Trainable Weka Segmentation v3.2.34");
call("trainableSegmentation.Weka_Segmentation.loadClassifier", classifier);
  • then I can introduce a loop to go through all the images to analyse and, in my case, generate a mask for particle analysis and save it as a tiff:
setBatchMode(true);
image_crop = getFileList(outputDir);

outputfolder = inputfolder + File.separator + 'masks'
File.makeDirectory(outputfolder);

//start analysis loop
	for (i=0; i<image_crop.length; i++) {
		
		selectWindow("Trainable Weka Segmentation v3.2.34");
		call("trainableSegmentation.Weka_Segmentation.applyClassifier", outputDir, image_crop[i], "showResults=true", "storeResults=false", "probabilityMaps=false", "");

		selectWindow("Classification result");
		run("8-bit");
		setSlice(1);
		setAutoThreshold("Default");
		run("Convert to Mask");
		output_path = outputfolder + File.separator + image_crop[i];
		saveAs("Tiff", output_path);
		title_analized = getTitle();
		run("Analyze Particles...", "size=0-Infinity display summarize");
		selectWindow(title_analized);
		run("Close");
		selectWindow(image_crop[i]);
		run("Close");
		 
		
}

My input and output directories have been defined at the beginning, as this is part of a longer script, but to make this work on its own, I would do this:

run("Close All");

//choose the trained classifier and input files directory
#@File(style="file", label="Classifier file") classifier
#@File(style="directory", label="images to test") inputfolder

images = getFileList(inputfolder);
	
	//to open just first image in folder with images to test
	for (l=0; l<1; l++) {

		path = inputfolder + File.separator + images[l];
		open(path);
		
}

wait(1000);

//start the Weka Segmentation tool and load the classifier
run("Trainable Weka Segmentation");
wait( 3000 ); 
selectWindow("Trainable Weka Segmentation v3.2.34");
call("trainableSegmentation.Weka_Segmentation.loadClassifier", classifier);

setBatchMode(true);

outputfolder = inputfolder + File.separator + 'masks'
File.makeDirectory(outputfolder);

//start analysis loop
	for (i=0; i<images.length; i++) {
		
		selectWindow("Trainable Weka Segmentation v3.2.34");
		call("trainableSegmentation.Weka_Segmentation.applyClassifier", inputfolder, images[i], "showResults=true", "storeResults=false", "probabilityMaps=false", "");
		selectWindow("Classification result");
		run("8-bit");
		setSlice(1);
		setAutoThreshold("Default");
		run("Convert to Mask");
		output_path = outputfolder + File.separator + images[i];
		saveAs("Tiff", output_path);
		title_analized = getTitle();
		run("Analyze Particles...", "size=0-Infinity display summarize");
		wait(1000);
		selectWindow(title_analized);
		run("Close");
		selectWindow(images[i]);
		run("Close");
		 
		}

setBatchMode(false);

I have just tested this and it works fine on an updated Fiji in Ubuntu 18.04.

I hope this is useful to you. Also, if you provide your complete script it makes it easier for other (more experienced) people to help out here.

Good work!

2 Likes

Dear @Tuan-Tu_Nguyen,

@NML is right, we might need more information to replicate your problem. Either some variable definitions are missing, or there is something else. In principle, macros should run the same way in all systems.

Maybe you can send us your full macro and the data you use?

1 Like

Hi all,
Sorry for my late reply.
Thanks @NML for your suggestions. Actually, I already did what you’ve suggested (adding wait time, …) (and yes, variables like pathname are well defined elsewhere in the code, as it worked in Windows 10) but still there is an error.
In the meantime, I’ve found another script in Beanshell that do exactly what I want in this link


And it works perfectly :slightly_smiling_face:

1 Like

Great that you found a work around.

Could it be something related with the paths, somehow a home directory being pointed to different between Windows vs Linux? Or the version of Weka you have on the Linux Fiji? (I know this is very basic, but I find myself making silly mistakes like those, but looking for more complicated problems…).

Good work!

1 Like