Search in sources :

Example 1 with InvertedFile

use of boofcv.alg.scene.bow.InvertedFile in project BoofCV by lessthanoptimal.

the class TestRecognitionIO method createNearestNeighbor.

private <TD extends TupleDesc<TD>> void createNearestNeighbor(FeatureSceneRecognitionNearestNeighbor<TD> recNN) {
    var db = recNN.getDatabase();
    db.getImagesDB().resize(20);
    for (int i = 0; i < 20; i++) {
        db.getImagesDB().set(i, rand.nextInt());
    }
    DogArray<InvertedFile> inverted = db.getInvertedFiles();
    for (int i = 0; i < 9; i++) {
        InvertedFile ld = inverted.grow();
        ld.add(rand.nextInt(20));
        ld.weights.add(rand.nextFloat());
    }
}
Also used : RecognitionNearestNeighborInvertedFile(boofcv.alg.scene.ann.RecognitionNearestNeighborInvertedFile) InvertedFile(boofcv.alg.scene.bow.InvertedFile)

Example 2 with InvertedFile

use of boofcv.alg.scene.bow.InvertedFile in project BoofCV by lessthanoptimal.

the class TestRecognitionIO method nearestNeighborBin_stream.

@Test
void nearestNeighborBin_stream() {
    var expected = new RecognitionNearestNeighborInvertedFile<>();
    expected.getImagesDB().resize(45);
    for (int i = 0; i < 20; i++) {
        InvertedFile iv = expected.getInvertedFiles().grow();
        int N = rand.nextInt(10);
        for (int j = 0; j < N; j++) {
            iv.addImage(rand.nextInt(1_000_000), rand.nextFloat());
        }
    }
    // Encode then decode
    ByteArrayOutputStream stream = new ByteArrayOutputStream();
    RecognitionIO.saveNearestNeighborBin(expected, stream);
    var found = new RecognitionNearestNeighborInvertedFile<>();
    InputStream input = new ByteArrayInputStream(stream.toByteArray());
    RecognitionIO.loadNearestNeighborBin(input, found);
    // See if they are identical
    compareInverted(expected.getInvertedFiles(), found.getInvertedFiles());
    assertEquals(expected.getImagesDB().size, found.getImagesDB().size);
}
Also used : RecognitionNearestNeighborInvertedFile(boofcv.alg.scene.ann.RecognitionNearestNeighborInvertedFile) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) RecognitionNearestNeighborInvertedFile(boofcv.alg.scene.ann.RecognitionNearestNeighborInvertedFile) InvertedFile(boofcv.alg.scene.bow.InvertedFile) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Test(org.junit.jupiter.api.Test)

Example 3 with InvertedFile

use of boofcv.alg.scene.bow.InvertedFile in project BoofCV by lessthanoptimal.

the class TestRecognitionIO method recognitionVocabularyTreeNister2006_stream.

@Test
void recognitionVocabularyTreeNister2006_stream() {
    // Create the data structure and fill it in with non default values
    RecognitionVocabularyTreeNister2006<TupleDesc_F64> db = createDefaultNister2006();
    // Encode then decode
    ByteArrayOutputStream stream = new ByteArrayOutputStream();
    RecognitionIO.saveBin(db, stream);
    var found = new RecognitionVocabularyTreeNister2006<TupleDesc_F64>();
    InputStream input = new ByteArrayInputStream(stream.toByteArray());
    RecognitionIO.loadBin(input, found);
    // See if all the important components were copied
    compareTrees(db.tree, found.tree);
    assertEquals(db.getImagesDB().size, found.getImagesDB().size);
    assertEquals(db.invertedFiles.size(), found.invertedFiles.size());
    for (int i = 0; i < db.invertedFiles.size(); i++) {
        InvertedFile e = db.invertedFiles.get(i);
        InvertedFile f = found.invertedFiles.get(i);
        assertEquals(e.size(), f.size());
        assertEquals(e.weights.size(), f.weights.size());
        assertEquals(e.weights.size(), f.size());
        for (int imageIdx = 0; imageIdx < e.size; imageIdx++) {
            int indexE = e.get(imageIdx);
            int indexF = f.get(imageIdx);
            assertEquals(indexE, indexF);
            float weightE = e.weights.get(imageIdx);
            float weightF = f.weights.get(imageIdx);
            assertEquals(weightE, weightF);
        }
    }
}
Also used : TupleDesc_F64(boofcv.struct.feature.TupleDesc_F64) RecognitionVocabularyTreeNister2006(boofcv.alg.scene.nister2006.RecognitionVocabularyTreeNister2006) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) RecognitionNearestNeighborInvertedFile(boofcv.alg.scene.ann.RecognitionNearestNeighborInvertedFile) InvertedFile(boofcv.alg.scene.bow.InvertedFile) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Test(org.junit.jupiter.api.Test)

Example 4 with InvertedFile

use of boofcv.alg.scene.bow.InvertedFile in project BoofCV by lessthanoptimal.

the class TestRecognitionIO method createDefaultNister2006.

@NotNull
private RecognitionVocabularyTreeNister2006<TupleDesc_F64> createDefaultNister2006() {
    var db = new RecognitionVocabularyTreeNister2006<TupleDesc_F64>();
    db.tree = createTree(6);
    db.getImagesDB().resize(20);
    for (int i = 0; i < 20; i++) {
        db.getImagesDB().set(i, rand.nextInt());
    }
    for (int i = 0; i < db.tree.nodes.size; i++) {
        InvertedFile ld = db.invertedFiles.grow();
        ld.add(rand.nextInt(20));
        ld.weights.add(rand.nextFloat());
    }
    return db;
}
Also used : RecognitionVocabularyTreeNister2006(boofcv.alg.scene.nister2006.RecognitionVocabularyTreeNister2006) RecognitionNearestNeighborInvertedFile(boofcv.alg.scene.ann.RecognitionNearestNeighborInvertedFile) InvertedFile(boofcv.alg.scene.bow.InvertedFile) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

RecognitionNearestNeighborInvertedFile (boofcv.alg.scene.ann.RecognitionNearestNeighborInvertedFile)4 InvertedFile (boofcv.alg.scene.bow.InvertedFile)4 RecognitionVocabularyTreeNister2006 (boofcv.alg.scene.nister2006.RecognitionVocabularyTreeNister2006)2 ByteArrayInputStream (java.io.ByteArrayInputStream)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 InputStream (java.io.InputStream)2 Test (org.junit.jupiter.api.Test)2 TupleDesc_F64 (boofcv.struct.feature.TupleDesc_F64)1 NotNull (org.jetbrains.annotations.NotNull)1