import ij.*; import ij.process.*; import ij.gui.*; import java.awt.*; import ij.plugin.filter.*; /* Performs the Kuwahara Filter, a noise-reduction filter that preserves edges. a a ab b b a a ab b b ac ac abcd bd bd c c cd d d c c cd d d In the case of a 5x5 sampling window, the mean brightness and the variance of each of the four 3x3 regions (a, b, c, d), are calculated and the value of the center pixel (abcd) is set to the mean value of the region that with the smallest variance. Description based on the one at: http://www.incx.nec.co.jp/imap-vision/library/wouter/kuwahara.html */ public class Kuwahara_Filter implements PlugInFilter { static int size = 5; static boolean filterRGB; boolean isRGB; public int setup(String arg, ImagePlus imp) { if (imp==null) return DONE; isRGB = imp.getBitDepth()==24; if (!showDialog()) return DONE; return IJ.setupDialog(imp, DOES_ALL-DOES_32+SUPPORTS_MASKING); } public void run(ImageProcessor ip) { if (isRGB) { if (filterRGB) filterRGB(ip); else filterIntensity(ip); } else filter(ip); } public void filter(ImageProcessor ip) { Rectangle roi = ip.getRoi(); int width = roi.width; int height = roi.height; int size2 = (size+1)/2; int offset = (size-1)/2; int width2 = ip.getWidth()+offset; int height2 = ip.getHeight()+offset; float[][] mean = new float[width2][height2]; float[][] variance = new float[width2][height2]; int x1start = roi.x; int y1start = roi.y; double sum, sum2; int n, v=0, xbase, ybase; for (int y1=y1start-offset; y1