Saving WEKA output in binary vs RGB

weka
imagej
segmentation

#1

Hi everyone!

I am classifying some images using the brilliant trainable WEKA segmentation plugin. During training and testing I applied my three-class classifier to a set of images, and received a set of black/grey/white output images. This was ideal for my next processing step.

I am now constructing a macro (or three) to automate the classification process and have found that all my classified images are now saved in funky RGB colours instead of greyscale. I can save a probability map but the probability map happens to have the opposite colour classification to before.

The difference seems to be in the number of images processed and saved at a time. If I apply the classifier through my script, my script loops through each individual image and creates a coloured output. If I manually apply the classifier to single images, I get a coloured output then as well. If I manually apply the classifier to three or more images, prompting the auto-save popup, I get the black/grey/white images. As far as I can tell from the macro recorder, there is no difference in the command passed to imageJ in each case, the difference is whether the commands are passed as a batch or singly.

I’d be very grateful if someone knows what the macro command is for switching between the two outputs. I’ve been through the documentation on Trainable Weka Segmentation but have not identified the solution. I know that I can use statistical region merging to force the desired output and will probably do so unless someone knows which setting this is.

Thanks!

My code:

		function WEKAmyPhotos(inputDir, inputFile, output){

				File.makeDirectory(output);

				selectWindow("Trainable Weka Segmentation v3.2.1");
		
				// apply the classifier:
				call("trainableSegmentation.Weka_Segmentation.applyClassifier",
					inputDir, 
					inputFile,
					"showResults=false", 
					"storeResults=true", 
					"probabilityMaps=true",
					output)

				// wait for the segmentation to complete. In future, this should be
				// linked to run time.
				wait(150);
		
		}

setBatchMode(true); 
myList = getFileList(myInput);
for (i = 0; i < myList.length; i++)
        WEKAmyPhotos(myInput, myList[i], myOutput);
setBatchMode(false);

Info:
I am a Windows 10 user running ImageJ 1.51g through Fiji. Trainable WEKA is version 3.2.1.


#2

Hello @Isabel_W and welcome to the forum!

Both the colorized and grayscale images have the same pixel values. The only difference is the first one has a color LUT and the second one a gray LUT. But if you use three classes, the actual pixels values of your images should only be 0, 1, and 2 in both cases. That being said, the problem of your macro is probably in the way you save the colored-LUT image, most likely as a real color image. Try applying a gray LUT to the images before saving them.

Cheers!


#3

Thanks @iarganda! This seems likely to be causing the change. This also means the LUT is switching from colour to greyscale within the trainable WEKA code then, even though the macro call is the same. Does anyone know whether the table can be set from within the WEKA functions? It would be more elegant to do it within the WEKA call than converting the images after.

Thanks!


#4

This was actually a bug. I have just made a new release of the plugin that fixes it. Thank you very much for reporting it!


#5

Oh cool, thank you @iarganda !


#6

The final macro for making a greyscale output from trainable WEKA:

	function WEKAmyPhotos(inputDir, inputFile, outputDir, outputFile){

		// make directory to store the output images	
		File.makeDirectory(outputDir);
		selectWindow("Trainable Weka Segmentation v3.2.2");

		// apply the classifier:
		call("trainableSegmentation.Weka_Segmentation.applyClassifier",
			inputDir, 
			inputFile,
			"showResults=true", 
			"storeResults=false", 
			"probabilityMaps=false",
			"")

		// wait for the segmentation to complete. In future, this should be
		// linked to run time.
		wait(150);

		// convert the colour result to an 8 bit greyscale image, which is
		// converted to more distinct black/white/grey values with a custom
		// lookup table:
		selectWindow("Classification result");
		run("8-bit");
		open("[specify custom LUT here]");
		saveAs("PNG", outputFile);

		// clean up
		close("Classification result");

	}

	myInput = the location of the images to process
	myFolder = where to store the end result
	
	setBatchMode(true); 
	myList = getFileList(myInput);
	for (i = 0; i < myList.length; i++)
	        WEKAmyPhotos(myInput, myList[i], myFolder, myFolder+"\\"+myList[i]);
	setBatchMode(false);