Search in sources :

Example 1 with KdTreeEuclideanSq_F64

use of org.ddogleg.nn.alg.distance.KdTreeEuclideanSq_F64 in project BoofCV by lessthanoptimal.

the class ExampleColorHistogramLookup method main.

public static void main(String[] args) {
    String imagePath = UtilIO.pathExample("recognition/vacation");
    List<String> images = UtilIO.listByPrefix(imagePath, null, ".jpg");
    Collections.sort(images);
    // Different color spaces you can try
    List<double[]> points = coupledHueSat(images);
    // List<double[]> points = independentHueSat(images);
    // List<double[]> points = coupledRGB(images);
    // List<double[]> points = histogramGray(images);
    // A few suggested image you can try searching for
    int target = 0;
    // int target = 28;
    // int target = 38;
    // int target = 46;
    // int target = 65;
    // int target = 77;
    double[] targetPoint = points.get(target);
    // Use a generic NN search algorithm. This uses Euclidean distance as a distance metric.
    NearestNeighbor<double[]> nn = FactoryNearestNeighbor.exhaustive(new KdTreeEuclideanSq_F64(targetPoint.length));
    NearestNeighbor.Search<double[]> search = nn.createSearch();
    DogArray<NnData<double[]>> results = new DogArray(NnData::new);
    nn.setPoints(points, true);
    search.findNearest(targetPoint, -1, 10, results);
    ListDisplayPanel gui = new ListDisplayPanel();
    // Add the target which the other images are being matched against
    gui.addImage(UtilImageIO.loadImageNotNull(images.get(target)), "Target", ScaleOptions.ALL);
    // The results will be the 10 best matches, but their order can be arbitrary. For display purposes
    // it's better to do it from best fit to worst fit
    Collections.sort(results.toList(), Comparator.comparingDouble((NnData o) -> o.distance));
    // Add images to GUI -- first match is always the target image, so skip it
    for (int i = 1; i < results.size; i++) {
        String file = images.get(results.get(i).index);
        double error = results.get(i).distance;
        BufferedImage image = UtilImageIO.loadImage(file);
        gui.addImage(image, String.format("Error %6.3f", error), ScaleOptions.ALL);
    }
    ShowImages.showWindow(gui, "Similar Images", true);
}
Also used : FactoryNearestNeighbor(org.ddogleg.nn.FactoryNearestNeighbor) NearestNeighbor(org.ddogleg.nn.NearestNeighbor) NnData(org.ddogleg.nn.NnData) ListDisplayPanel(boofcv.gui.ListDisplayPanel) DogArray(org.ddogleg.struct.DogArray) BufferedImage(java.awt.image.BufferedImage) ConvertBufferedImage(boofcv.io.image.ConvertBufferedImage) KdTreeEuclideanSq_F64(org.ddogleg.nn.alg.distance.KdTreeEuclideanSq_F64)

Aggregations

ListDisplayPanel (boofcv.gui.ListDisplayPanel)1 ConvertBufferedImage (boofcv.io.image.ConvertBufferedImage)1 BufferedImage (java.awt.image.BufferedImage)1 FactoryNearestNeighbor (org.ddogleg.nn.FactoryNearestNeighbor)1 NearestNeighbor (org.ddogleg.nn.NearestNeighbor)1 NnData (org.ddogleg.nn.NnData)1 KdTreeEuclideanSq_F64 (org.ddogleg.nn.alg.distance.KdTreeEuclideanSq_F64)1 DogArray (org.ddogleg.struct.DogArray)1