Search in sources :

Example 1 with NearestNeighbor

use of org.ddogleg.nn.NearestNeighbor in project BoofCV by lessthanoptimal.

the class PointCloudUtils_F64 method prune.

/**
 * Prunes points from the point cloud if they have very few neighbors
 *
 * @param cloud Point cloud
 * @param minNeighbors Minimum number of neighbors for it to not be pruned
 * @param radius search distance for neighbors
 */
public static void prune(List<Point3D_F64> cloud, int minNeighbors, double radius) {
    if (minNeighbors < 0)
        throw new IllegalArgumentException("minNeighbors must be >= 0");
    NearestNeighbor<Point3D_F64> nn = FactoryNearestNeighbor.kdtree(new KdTreePoint3D_F64());
    NearestNeighbor.Search<Point3D_F64> search = nn.createSearch();
    nn.setPoints(cloud, false);
    DogArray<NnData<Point3D_F64>> results = new DogArray<>(NnData::new);
    // It will always find itself
    minNeighbors += 1;
    // distance is Euclidean squared
    radius *= radius;
    for (int i = cloud.size() - 1; i >= 0; i--) {
        search.findNearest(cloud.get(i), radius, minNeighbors, results);
        if (results.size < minNeighbors) {
            cloud.remove(i);
        }
    }
}
Also used : FactoryNearestNeighbor(org.ddogleg.nn.FactoryNearestNeighbor) NearestNeighbor(org.ddogleg.nn.NearestNeighbor) KdTreePoint3D_F64(georegression.helper.KdTreePoint3D_F64) Point3D_F64(georegression.struct.point.Point3D_F64) NnData(org.ddogleg.nn.NnData) DogArray(org.ddogleg.struct.DogArray) KdTreePoint3D_F64(georegression.helper.KdTreePoint3D_F64)

Example 2 with NearestNeighbor

use of org.ddogleg.nn.NearestNeighbor 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)

Example 3 with NearestNeighbor

use of org.ddogleg.nn.NearestNeighbor in project BoofCV by lessthanoptimal.

the class PointCloudUtils_F64 method prune.

/**
 * Prunes points from the point cloud if they have very few neighbors
 *
 * @param cloud Point cloud
 * @param colors Color of each point.
 * @param minNeighbors Minimum number of neighbors for it to not be pruned
 * @param radius search distance for neighbors
 */
public static void prune(List<Point3D_F64> cloud, DogArray_I32 colors, int minNeighbors, double radius) {
    if (minNeighbors < 0)
        throw new IllegalArgumentException("minNeighbors must be >= 0");
    NearestNeighbor<Point3D_F64> nn = FactoryNearestNeighbor.kdtree(new KdTreePoint3D_F64());
    NearestNeighbor.Search<Point3D_F64> search = nn.createSearch();
    nn.setPoints(cloud, false);
    DogArray<NnData<Point3D_F64>> results = new DogArray<>(NnData::new);
    // It will always find itself
    minNeighbors += 1;
    // distance is Euclidean squared
    radius *= radius;
    for (int i = cloud.size() - 1; i >= 0; i--) {
        search.findNearest(cloud.get(i), radius, minNeighbors, results);
        if (results.size < minNeighbors) {
            cloud.remove(i);
            colors.remove(i);
        }
    }
}
Also used : FactoryNearestNeighbor(org.ddogleg.nn.FactoryNearestNeighbor) NearestNeighbor(org.ddogleg.nn.NearestNeighbor) KdTreePoint3D_F64(georegression.helper.KdTreePoint3D_F64) Point3D_F64(georegression.struct.point.Point3D_F64) NnData(org.ddogleg.nn.NnData) DogArray(org.ddogleg.struct.DogArray) KdTreePoint3D_F64(georegression.helper.KdTreePoint3D_F64)

Example 4 with NearestNeighbor

use of org.ddogleg.nn.NearestNeighbor in project BoofCV by lessthanoptimal.

the class PruneStructureFromSceneMetric method prunePoints.

/**
 * Prune a feature it has fewer than X neighbors within Y distance. Observations
 * associated with this feature are also pruned.
 *
 * Call {@link #pruneViews(int)} to makes sure the graph is valid.
 *
 * @param neighbors Number of other features which need to be near by
 * @param distance Maximum distance a point can be to be considered a feature
 */
public void prunePoints(int neighbors, double distance) {
    // Use a nearest neighbor search to find near by points
    Point3D_F64 worldX = new Point3D_F64();
    List<Point3D_F64> cloud = new ArrayList<>();
    for (int i = 0; i < structure.points.size; i++) {
        SceneStructureCommon.Point structureP = structure.points.data[i];
        structureP.get(worldX);
        cloud.add(worldX.copy());
    }
    NearestNeighbor<Point3D_F64> nn = FactoryNearestNeighbor.kdtree(new KdTreePoint3D_F64());
    NearestNeighbor.Search<Point3D_F64> search = nn.createSearch();
    nn.setPoints(cloud, false);
    DogArray<NnData<Point3D_F64>> resultsNN = new DogArray<>(NnData::new);
    // Create a look up table containing from old to new indexes for each point
    int[] oldToNew = new int[structure.points.size];
    // crash is bug
    Arrays.fill(oldToNew, -1);
    // List of point ID's which are to be removed.
    DogArray_I32 prunePointID = new DogArray_I32();
    // identify points which need to be pruned
    for (int pointId = 0; pointId < structure.points.size; pointId++) {
        SceneStructureCommon.Point structureP = structure.points.data[pointId];
        structureP.get(worldX);
        // distance is squared
        search.findNearest(cloud.get(pointId), distance * distance, neighbors + 1, resultsNN);
        // Don't prune if it has enough neighbors. Remember that it will always find itself.
        if (resultsNN.size() > neighbors) {
            oldToNew[pointId] = pointId - prunePointID.size;
            continue;
        }
        prunePointID.add(pointId);
        // Remove observations of this point
        for (int viewIdx = 0; viewIdx < structureP.views.size; viewIdx++) {
            SceneObservations.View v = observations.getView(structureP.views.data[viewIdx]);
            int pointIdx = v.point.indexOf(pointId);
            if (pointIdx < 0)
                throw new RuntimeException("Bad structure. Point not found in view's observation " + "which was in its structure");
            v.remove(pointIdx);
        }
    }
    pruneUpdatePointID(oldToNew, prunePointID);
}
Also used : FactoryNearestNeighbor(org.ddogleg.nn.FactoryNearestNeighbor) NearestNeighbor(org.ddogleg.nn.NearestNeighbor) KdTreePoint3D_F64(georegression.helper.KdTreePoint3D_F64) Point3D_F64(georegression.struct.point.Point3D_F64) NnData(org.ddogleg.nn.NnData) DogArray_I32(org.ddogleg.struct.DogArray_I32) DogArray(org.ddogleg.struct.DogArray) KdTreePoint3D_F64(georegression.helper.KdTreePoint3D_F64)

Aggregations

FactoryNearestNeighbor (org.ddogleg.nn.FactoryNearestNeighbor)4 NearestNeighbor (org.ddogleg.nn.NearestNeighbor)4 NnData (org.ddogleg.nn.NnData)4 DogArray (org.ddogleg.struct.DogArray)4 KdTreePoint3D_F64 (georegression.helper.KdTreePoint3D_F64)3 Point3D_F64 (georegression.struct.point.Point3D_F64)3 ListDisplayPanel (boofcv.gui.ListDisplayPanel)1 ConvertBufferedImage (boofcv.io.image.ConvertBufferedImage)1 BufferedImage (java.awt.image.BufferedImage)1 KdTreeEuclideanSq_F64 (org.ddogleg.nn.alg.distance.KdTreeEuclideanSq_F64)1 DogArray_I32 (org.ddogleg.struct.DogArray_I32)1