Scripted Weka Segmentation on Image Directory yields no difference in the output

segmentation
macro
Tags: #<Tag:0x00007fd543087120> #<Tag:0x00007fd543086f90>

#1

Hello All, lurker for some time but first time poster. I am having an issue with the Trainable Weka Segmentation where I’m wanting to apply known classifiers and loaded data to a directory of tiff images. Each image is somewhat similar but also somewhat different in terms of the micro structure; but when I apply the trainable segmentation with the following ijm script:

print("Getting input directory.");

inputDir = getDirectory("C:\Users\clopez\Pictures\output tiffs");

print("Getting output directory");

outputDir = getDirectory("C:\Users\clopez\Pictures\output tiffs segmented");

print("Processing directory");

setBatchMode(true);
segmentFolder(inputDir, outputDir);
setBatchMode(false);


function segmentFolder(input,output)
{	
	listOfFiles = getFileList(input);	
	
	for(i = 0; i < listOfFiles.length-1; i++) 
	{
		if(endsWith(listOfFiles[i], ".tiff")) {
			print("File found, processing ...");
			segmentFile(input, output, listOfFiles[i]);
			print("File segmented.");	
		}
	 }
}


function segmentFile(input, output, filename)
{
	open(input+filename);
	run("Trainable Weka Segmentation");
	selectWindow("Trainable Weka Segmentation v3.2.5");
	call("trainableSegmentation.Weka_Segmentation.loadClassifier", "C:\\Users\\clopez\\Pictures\\output tiffs\\myClassifier_3-23-17.model");
	call("trainableSegmentation.Weka_Segmentation.loadData", "C:\\Users\\clopez\\Pictures\\output tiffs\\myData_3-23-17.arff");
	call("trainableSegmentation.Weka_Segmentation.trainClassifier");
	call("trainableSegmentation.Weka_Segmentation.getResult");
	selectWindow("Classified image");
	run("8-bit");
	setOption("BlackBackground", true);
	run("Make Binary");
	run("Options...", "iterations=8 count=1 black do=Close");
	run("Invert LUT");
	run("Watershed");
	run("Microstructure Analysis", "perimeter=[Crofton (4 dirs.)] add_porosity");
	run("Analyze Particles...", "pixel show=Outlines display exclude clear include summarize");
	selectWindow("Classified image");
	saveAs("Tiff", output+filename);
	close();
}

the images all come out basically the same as if it’s just iterating on the same image over and over again, and this is the confirmed by the measurement call. I was hoping to get some third party eyes on my script and see if it’s something there that’s forcing this. Any and all help is appreciated, thanks!


Definition of Solidity
#2

Dear @clopez,

your issue might be the call to trainableSegmentation.Weka_Segmentation.loadData(), although I am not sure.

You should only have to load a classifier and be able to apply it to a new image. As described on the Wiki page, you can use applyClassifier() for that purpose.

If you need further guidance, just post again.

Best,
Stefan


#3

Hello @stelfrich and @clopez,

Sorry to get so late into the conversation. The code looks OK, but since you are using macros, you might need to use some wait calls every now and then to avoid the commands being executed before they should. Have a look at the wiki example (line 8).

That being said, I would recommend you to use proper scripts instead of macros to prevent these errors from happening. I can help you with that if you need it.

ignacio


Fiji macro does not wait a command finished before running the next command