Need help or advice ? Come to the Icy club ! - Every Wenesday morning from 9h30 to 12h30 - Francois Jacob Building - Main hall - Pasteur
Register

KMeans Thresholding with Gaussian Filter

by danyfel80 / Daniel Felipe González Obando

Performs a binary threshold using KMeans on an image smoothened with a Gaussian filter (kernel 3x3).
Similar to the protocol tutorial (without ROIs)

version 2 (last version):

download to use and modify in Icy. How do I use scripts ? A Problem ? ask the community.
Changelog for this version: Added documentation
/*
import the different Classes needed
	- FileDialog is used to select file and Loader to load the image
	- Package filtering is used to create the kernel and perform the convolution
	- Package thresholder is needed to do the threshold with 
	  Thresholder and KMeans classes.
*/
importClass(Packages.plugins.tprovoost.scripteditor.uitools.filedialogs.FileDialog)
importClass(Packages.icy.file.Loader)
importClass(Packages.plugins.adufour.filtering.Convolution1D)
importClass(Packages.plugins.adufour.filtering.Kernels1D)
importClass(Packages.plugins.adufour.thresholder.Thresholder)
importClass(Packages.plugins.adufour.thresholder.KMeans)
importClass(Packages.icy.file.Saver)

/*
 * Request image file and verify if path is not empty
 */
f = FileDialog.open()
if (!f.toString().equals("")) {
	/*
	 * Load sequence from f and check if seq is valid
	 */
	seq = Loader.loadSequence(f, 0, true)
	if (seq != null) {
		/*
		 * Set the kernel of 3x3
		 */
		sigma = 3
		kernelX = Kernels1D.CUSTOM_GAUSSIAN.createGaussianKernel1D(sigma).getData()
		kernelY = Kernels1D.CUSTOM_GAUSSIAN.createGaussianKernel1D(sigma).getData()
		/*
		 * Create a copy of the original sequence and perform convolution
		 */
		seqf = seq.getCopy()
		Convolution1D.convolve(seqf, kernelX, kernelY, null)
	
		/*
		 * Show gaussian result
		 */
		gui.addSequence(seqf)
	
		/*
		 * Find threshold using KMeans method and perform binary threshold
		 */
		kmeans = KMeans.computeKMeansThresholds(seq, 2, 2, 255)
		result = Thresholder.threshold(seqf, 2, kmeans, false)
		result.setName(seq.getName() + " - thresholded")
	
		/*
		 * Show result of binary Threshold
		 */
		gui.addSequence(result)

		/*
		 * Save result to file
		 */
		//f = FileDialog.save()
		//Saver.save(result, f, true, true)
	}
}

This script has no documentation. A Problem ? ask the community.


Previous versions:


version 1:

download to use and modify in Icy.
Changelog for this version: initial version
importClass(Packages.plugins.adufour.filtering.Convolution1D)
importClass(Packages.plugins.adufour.filtering.Kernels1D)
importClass(Packages.icy.file.Saver)
importClass(Packages.icy.file.Loader)
importClass(Packages.plugins.tprovoost.scripteditor.uitools.filedialogs.FileDialog)
importClass(Packages.plugins.adufour.thresholder.Thresholder)
importClass(Packages.plugins.adufour.thresholder.KMeans)
importClass(Packages.icy.sequence.SequenceUtil)
f = FileDialog.open()
seq = Loader.loadSequence(f, 0, true)

sigma = 3
kernelX = Kernels1D.CUSTOM_GAUSSIAN.createGaussianKernel1D(sigma).getData()
kernelY = Kernels1D.CUSTOM_GAUSSIAN.createGaussianKernel1D(sigma).getData()
seqf = seq.getCopy()
Convolution1D.convolve(seqf, kernelX, kernelY, null)

gui.addSequence(seqf)

kmeans = KMeans.computeKMeansThresholds(seq, 2, 2, 255)
result = Thresholder.threshold(seqf, 2, kmeans, false)
result.setName(seq.getName() + " - thresholded")

gui.addSequence(result)

//f = FileDialog.save()
//Saver.save(result, f, true, true)


Icy script publication Id : J4J9L4