/** * Estimate a multidimensional Gaussian from the data * * @param samples * the data * @return the Gaussian with the best fit to the data */ public static MultivariateGaussian estimate(double[][] samples) { final int ndims = samples[0].length; final CachingMultivariateGaussian gauss = new CachingMultivariateGaussian(); gauss.N = ndims; final MeanAndCovariance res = new MeanAndCovariance(samples); gauss.mean = res.mean; gauss.covar = res.covar; gauss.cacheValues(); return gauss; }
@Override protected float classifyPixel(Float[] pix) { return (float) gauss.estimateProbability(pix); }
@Override public double[][] sample(int count, Random rng) { final double[][] samples = new double[count][]; for (int i = 0; i < count; i++) samples[i] = sample(rng); return samples; }
@Override public SingleGaussianPixelModel clone() { final SingleGaussianPixelModel model = new SingleGaussianPixelModel(ndims); model.gauss = new CachingMultivariateGaussian(gauss.getMean().copy(), gauss.getCovariance().copy()); return null; } }
/** * Estimate a multidimensional Gaussian from the data * * @param samples * the data * @return the Gaussian with the best fit to the data */ public static MultivariateGaussian estimate(Matrix samples) { return estimate(samples.getArray()); }
/** * Construct the Gaussian with the provided center and covariance * * @param mean * centre of the Gaussian * @param covar * covariance of the Gaussian */ public CachingMultivariateGaussian(Matrix mean, Matrix covar) { N = mean.getColumnDimension(); this.mean = mean; this.covar = covar; cacheValues(); }
@Override public SingleGaussianPixelModel clone() { final SingleGaussianPixelModel model = new SingleGaussianPixelModel(ndims); model.gauss = new CachingMultivariateGaussian(gauss.getMean().copy(), gauss.getCovariance().copy()); return null; } }
@Override public void learnModel(MBFImage... images) { final List<float[]> data = new ArrayList<float[]>(); for (int i = 0; i < images.length; i++) { for (int y = 0; y < images[i].getHeight(); y++) { for (int x = 0; x < images[i].getWidth(); x++) { final float[] d = new float[ndims]; for (int j = 0; j < ndims; j++) { d[j] = images[i].getBand(j).pixels[y][x]; } data.add(d); } } } final float[][] arraydata = data.toArray(new float[data.size()][ndims]); gauss = CachingMultivariateGaussian.estimate(arraydata); }
/** * Construct the Gaussian with the zero mean and unit variance * * @param ndims * number of dimensions */ public CachingMultivariateGaussian(int ndims) { N = ndims; this.mean = new Matrix(1, N); this.covar = Matrix.identity(N, N); cacheValues(); }
/** * Estimate a multidimensional Gaussian from the data * * @param samples * the data * @return the Gaussian with the best fit to the data */ public static CachingMultivariateGaussian estimate(float[][] samples) { final int ndims = samples[0].length; final CachingMultivariateGaussian gauss = new CachingMultivariateGaussian(); gauss.N = ndims; final MeanAndCovariance res = new MeanAndCovariance(samples); gauss.mean = res.mean; gauss.covar = res.covar; gauss.cacheValues(); return gauss; }
@Override public void learnModel(MBFImage... images) { final List<float[]> data = new ArrayList<float[]>(); for (int i = 0; i < images.length; i++) { for (int y = 0; y < images[i].getHeight(); y++) { for (int x = 0; x < images[i].getWidth(); x++) { final float[] d = new float[ndims]; for (int j = 0; j < ndims; j++) { d[j] = images[i].getBand(j).pixels[y][x]; } data.add(d); } } } final float[][] arraydata = data.toArray(new float[data.size()][ndims]); gauss = CachingMultivariateGaussian.estimate(arraydata); }
@Override protected float classifyPixel(Float[] pix) { return (float) gauss.estimateProbability(pix); }