/*
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)
	}
}
