Search in sources :

Example 1 with ConfigFeatureToSceneRecognition

use of boofcv.abst.scene.ConfigFeatureToSceneRecognition in project BoofCV by lessthanoptimal.

the class TestRecognitionIO method save_load_FeatureToScene_Nister2006.

/**
 * Very basic test. Mostly just checks to see if things blow up or not
 */
@Test
void save_load_FeatureToScene_Nister2006() {
    File dir = new File(System.getProperty("java.io.tmpdir"), "feature_to_scene");
    try {
        var config = new ConfigFeatureToSceneRecognition();
        config.typeRecognize = ConfigFeatureToSceneRecognition.Type.NISTER_2006;
        ImageType<GrayU8> imageType = ImageType.SB_U8;
        var original = FactorySceneRecognition.createFeatureToScene(config, imageType);
        ((FeatureSceneRecognitionNister2006<TupleDesc_F64>) original.getRecognizer()).setDatabase(createDefaultNister2006());
        RecognitionIO.saveFeatureToScene(original, dir);
        WrapFeatureToSceneRecognition<GrayU8, TupleDesc_F64> found = RecognitionIO.loadFeatureToScene(dir, imageType);
        // Check a some things to make sure it actually loaded
        FeatureSceneRecognitionNister2006<TupleDesc_F64> foundRecognizer = found.getRecognizer();
        assertEquals(20, foundRecognizer.getDatabase().getImagesDB().size);
        assertEquals(5, foundRecognizer.getTree().nodes.size());
    } finally {
        // clean up
        if (dir.exists())
            UtilIO.deleteRecursive(dir);
    }
}
Also used : FeatureSceneRecognitionNister2006(boofcv.abst.scene.nister2006.FeatureSceneRecognitionNister2006) TupleDesc_F64(boofcv.struct.feature.TupleDesc_F64) GrayU8(boofcv.struct.image.GrayU8) RecognitionNearestNeighborInvertedFile(boofcv.alg.scene.ann.RecognitionNearestNeighborInvertedFile) InvertedFile(boofcv.alg.scene.bow.InvertedFile) File(java.io.File) ConfigFeatureToSceneRecognition(boofcv.abst.scene.ConfigFeatureToSceneRecognition) Test(org.junit.jupiter.api.Test)

Example 2 with ConfigFeatureToSceneRecognition

use of boofcv.abst.scene.ConfigFeatureToSceneRecognition in project BoofCV by lessthanoptimal.

the class ExampleSceneRecognition method main.

public static void main(String[] args) {
    String imagePath = UtilIO.pathExample("recognition/scene");
    List<String> images = UtilIO.listByPrefix(imagePath, null, ".jpg");
    Collections.sort(images);
    SceneRecognition<GrayU8> recognizer;
    // Except for real-time applications or when there are more than a few hundred images, you might want to
    // just learn the dictionary from scratch
    var saveDirectory = new File("example_recognition");
    // Tell it to process gray U8 images
    ImageType<GrayU8> imageType = ImageType.SB_U8;
    // Used to look up images one at a time from various sources. In this case a list of images.
    var imageIterator = new ImageFileListIterator<>(images, imageType);
    if (false) {
        // Set the line above to true and it will download a pre-built model. Useful when you have a lot of images
        // or simply want to skip the learning step
        System.out.println("Downloading pre-built model");
        recognizer = RecognitionIO.downloadDefaultSceneRecognition(new File("downloaded_models"), imageType);
        recognizer.setVerbose(System.out, BoofMiscOps.hashSet(BoofVerbose.RECURSIVE));
    } else if (saveDirectory.exists()) {
        System.out.println("Loading previously generated model");
        recognizer = RecognitionIO.loadFeatureToScene(saveDirectory, imageType);
        recognizer.setVerbose(System.out, BoofMiscOps.hashSet(BoofVerbose.RECURSIVE));
    } else {
        // If many applications, learning a new model is a small fraction of the compute time and since its
        // fit to the images it will be more accurate than a generic pre-built model
        System.out.println("Creating a new model");
        var config = new ConfigFeatureToSceneRecognition();
        // Use a hierarchical vocabulary tree, which is very fast and also one of the more accurate approaches
        config.typeRecognize = ConfigFeatureToSceneRecognition.Type.NISTER_2006;
        config.recognizeNister2006.learningMinimumPointsForChildren.setFixed(20);
        recognizer = FactorySceneRecognition.createFeatureToScene(config, imageType);
        // This will print out a lot of debugging information to stdout
        recognizer.setVerbose(System.out, BoofMiscOps.hashSet(BoofVerbose.RECURSIVE));
        // Learn the model from the initial set of images
        recognizer.learnModel(imageIterator);
    }
    // See if the recognition algorithm already has images loaded in to it
    if (recognizer.getImageIds(null).isEmpty()) {
        // Add images to the data base
        System.out.println("Adding images to the database");
        imageIterator.reset();
        while (imageIterator.hasNext()) {
            GrayU8 image = imageIterator.next();
            recognizer.addImage(images.get(imageIterator.getIndex()), image);
        }
        // This saves the model with the image database to disk
        System.out.println("Saving model");
        BoofMiscOps.profile(() -> RecognitionIO.saveFeatureToScene((WrapFeatureToSceneRecognition<GrayU8, ?>) recognizer, saveDirectory), "");
    }
    var gui = new ListDisplayPanel();
    // Specifies which image it will try to look up. In the example, related images are in sets of 3.
    int queryImage = 9;
    // Add the target which the other images are being matched against
    gui.addImage(UtilImageIO.loadImageNotNull(images.get(queryImage)), "Query", ScaleOptions.ALL);
    // Look up images
    var matches = new DogArray<>(SceneRecognition.Match::new);
    recognizer.query(imageIterator.loadImage(queryImage), /* filter */
    (id) -> true, /* limit */
    5, matches);
    for (int i = 0; i < matches.size; i++) {
        String file = matches.get(i).id;
        double error = matches.get(i).error;
        BufferedImage image = UtilImageIO.loadImageNotNull(file);
        String name = FilenameUtils.getBaseName(new File(file).getName());
        gui.addImage(image, String.format("%20s Error %6.3f", name, error), ScaleOptions.ALL);
    }
    System.out.println("Total images = " + images.size());
    System.out.println(images.get(queryImage) + " -> " + matches.get(0).id + " matches.size=" + matches.size);
    ShowImages.showWindow(gui, "Similar Images by Features", true);
}
Also used : ListDisplayPanel(boofcv.gui.ListDisplayPanel) ConfigFeatureToSceneRecognition(boofcv.abst.scene.ConfigFeatureToSceneRecognition) DogArray(org.ddogleg.struct.DogArray) BufferedImage(java.awt.image.BufferedImage) ImageFileListIterator(boofcv.io.image.ImageFileListIterator) WrapFeatureToSceneRecognition(boofcv.abst.scene.WrapFeatureToSceneRecognition) GrayU8(boofcv.struct.image.GrayU8) File(java.io.File)

Example 3 with ConfigFeatureToSceneRecognition

use of boofcv.abst.scene.ConfigFeatureToSceneRecognition in project BoofCV by lessthanoptimal.

the class TestRecognitionIO method save_load_FeatureToScene_NearestNeighbor.

/**
 * Very basic test. Mostly just checks to see if things blow up or not
 */
@Test
void save_load_FeatureToScene_NearestNeighbor() {
    File dir = new File(System.getProperty("java.io.tmpdir"), "feature_to_scene");
    try {
        var config = new ConfigFeatureToSceneRecognition();
        config.typeRecognize = ConfigFeatureToSceneRecognition.Type.NEAREST_NEIGHBOR;
        ImageType<GrayU8> imageType = ImageType.SB_U8;
        var original = FactorySceneRecognition.createFeatureToScene(config, imageType);
        createNearestNeighbor(original.getRecognizer());
        RecognitionIO.saveFeatureToScene(original, dir);
        WrapFeatureToSceneRecognition<GrayU8, TupleDesc_F64> found = RecognitionIO.loadFeatureToScene(dir, imageType);
        // Check a some things to make sure it actually loaded
        FeatureSceneRecognitionNearestNeighbor<TupleDesc_F64> foundRecognizer = found.getRecognizer();
        assertEquals(20, foundRecognizer.getDatabase().getImagesDB().size);
        assertEquals(9, foundRecognizer.getDatabase().getInvertedFiles().size());
    } finally {
        // clean up
        if (dir.exists())
            UtilIO.deleteRecursive(dir);
    }
}
Also used : TupleDesc_F64(boofcv.struct.feature.TupleDesc_F64) GrayU8(boofcv.struct.image.GrayU8) RecognitionNearestNeighborInvertedFile(boofcv.alg.scene.ann.RecognitionNearestNeighborInvertedFile) InvertedFile(boofcv.alg.scene.bow.InvertedFile) File(java.io.File) ConfigFeatureToSceneRecognition(boofcv.abst.scene.ConfigFeatureToSceneRecognition) Test(org.junit.jupiter.api.Test)

Aggregations

ConfigFeatureToSceneRecognition (boofcv.abst.scene.ConfigFeatureToSceneRecognition)3 GrayU8 (boofcv.struct.image.GrayU8)3 File (java.io.File)3 RecognitionNearestNeighborInvertedFile (boofcv.alg.scene.ann.RecognitionNearestNeighborInvertedFile)2 InvertedFile (boofcv.alg.scene.bow.InvertedFile)2 TupleDesc_F64 (boofcv.struct.feature.TupleDesc_F64)2 Test (org.junit.jupiter.api.Test)2 WrapFeatureToSceneRecognition (boofcv.abst.scene.WrapFeatureToSceneRecognition)1 FeatureSceneRecognitionNister2006 (boofcv.abst.scene.nister2006.FeatureSceneRecognitionNister2006)1 ListDisplayPanel (boofcv.gui.ListDisplayPanel)1 ImageFileListIterator (boofcv.io.image.ImageFileListIterator)1 BufferedImage (java.awt.image.BufferedImage)1 DogArray (org.ddogleg.struct.DogArray)1