/** * A completely default {@link DoubleKMeans} used primarily as a convenience function for reading. */ protected DoubleKMeans() { this(new KMeansConfiguration<DoubleNearestNeighbours, double[]>()); }
@Override public String toString() { return String.format("%s: {K=%d, NN=%s}", this.getClass().getSimpleName(), this.conf.K, this.conf.getNearestNeighbourFactory().getClass().getSimpleName()); } }
@Override public SpatialClusters<?> create(byte[][] data) { if (this.precision == Precision.BYTE) { final KMeansConfiguration<ByteNearestNeighbours, byte[]> kmc = new KMeansConfiguration<ByteNearestNeighbours, byte[]>(); if (exactMode) { kmc.setNearestNeighbourFactory(new ByteNearestNeighboursExact.Factory()); } else { kmc.setNearestNeighbourFactory(new ByteNearestNeighboursKDTree.Factory()); } final HierarchicalByteKMeans tree = new HierarchicalByteKMeans(kmc, data[0].length, K, depth); System.err.printf("Building vocabulary tree\n"); return tree.cluster(data); } else { final KMeansConfiguration<IntNearestNeighbours, int[]> kmc = new KMeansConfiguration<IntNearestNeighbours, int[]>(); if (exactMode) { kmc.setNearestNeighbourFactory(new IntNearestNeighboursExact.Factory()); } else { kmc.setNearestNeighbourFactory(new IntNearestNeighboursKDTree.Factory()); } final HierarchicalIntKMeans tree = new HierarchicalIntKMeans(kmc, data[0].length, K, depth); System.err.printf("Building vocabulary tree\n"); return tree.cluster(ByteArrayConverter.byteToInt(data)); } }
@Override public List<? extends PixelSet> segment(final MBFImage image) { final MBFImage input = ColourSpace.convert(image, colourSpace); final float[][] imageData = imageToVector(input); final FloatCentroidsResult result = kmeans.cluster(imageData); final List<PixelSet> out = new ArrayList<PixelSet>(kmeans.getConfiguration().getK()); for (int i = 0; i < kmeans.getConfiguration().getK(); i++) out.add(new PixelSet()); final HardAssigner<float[], ?, ?> assigner = result.defaultHardAssigner(); final int height = image.getHeight(); final int width = image.getWidth(); for (int y = 0, i = 0; y < height; y++) { for (int x = 0; x < width; x++, i++) { final float[] pixel = imageData[i]; final int centroid = assigner.assign(pixel); out.get(centroid).addPixel(x, y); } } return out; } }
fkm.getConfiguration().setBlockSize(500);
kmeans.getConfiguration().setMaxIterations(100);
@Override public List<? extends PixelSet> segment(final MBFImage image) { final MBFImage input = ColourSpace.convert(image, colourSpace); final float[][] imageData = imageToVector(input); final FloatCentroidsResult result = kmeans.cluster(imageData); final List<PixelSet> out = new ArrayList<PixelSet>(kmeans.getConfiguration().getK()); for (int i = 0; i < kmeans.getConfiguration().getK(); i++) out.add(new PixelSet()); final HardAssigner<float[], ?, ?> assigner = result.defaultHardAssigner(); final int height = image.getHeight(); final int width = image.getWidth(); for (int y = 0, i = 0; y < height; y++) { for (int x = 0; x < width; x++, i++) { final float[] pixel = imageData[i]; final int centroid = assigner.assign(pixel); out.get(centroid).addPixel(x, y); } } return out; } }
fkm.getConfiguration().setBlockSize(500);
/** * Construct a new {@link HierarchicalIntKMeans} with the given parameters. * Uses the default parameters of the {@link KMeansConfiguration}. * * @param M Data dimensionality. * @param K Number of clusters per node. * @param depth Tree depth. */ public HierarchicalIntKMeans(int M, int K, int depth) { this(new KMeansConfiguration<IntNearestNeighbours, int[]>(), M, K, depth); }
@Override public String toString() { return String.format("%s: {K=%d, NN=%s}", this.getClass().getSimpleName(), this.conf.K, this.conf.getNearestNeighbourFactory().getClass().getSimpleName()); } }
/** * Construct a new {@link HierarchicalShortKMeans} with the given parameters. * Uses the default parameters of the {@link KMeansConfiguration}. * * @param M Data dimensionality. * @param K Number of clusters per node. * @param depth Tree depth. */ public HierarchicalShortKMeans(int M, int K, int depth) { this(new KMeansConfiguration<ShortNearestNeighbours, short[]>(), M, K, depth); }
@Override public String toString() { return String.format("%s: {K=%d, NN=%s}", this.getClass().getSimpleName(), this.conf.K, this.conf.getNearestNeighbourFactory().getClass().getSimpleName()); } }
/** * Construct a new {@link HierarchicalByteKMeans} with the given parameters. * Uses the default parameters of the {@link KMeansConfiguration}. * * @param M Data dimensionality. * @param K Number of clusters per node. * @param depth Tree depth. */ public HierarchicalByteKMeans(int M, int K, int depth) { this(new KMeansConfiguration<ByteNearestNeighbours, byte[]>(), M, K, depth); }
@Override public String toString() { return String.format("%s: {K=%d, NN=%s}", this.getClass().getSimpleName(), this.conf.K, this.conf.getNearestNeighbourFactory().getClass().getSimpleName()); } }
/** * A completely default {@link FloatKMeans} used primarily as a convenience function for reading. */ protected FloatKMeans() { this(new KMeansConfiguration<FloatNearestNeighbours, float[]>()); }