General

The LIRE library allows the creation of a Lucene Index for content based image retrieval (CBIR). Furthermore methods for searching the index are provided.

This library is part of the Caliph & Emir project and aims to provide the CBIR features to other Java projects in an easy and light weight way.

Creating an Index

Use DocumentBuilderFactory to create a DocumentBuilder, which will create Lucene Documents from images. Add this documents to an index like this:
    System.out.println(">> Indexing " + images.size() + " files.");
    DocumentBuilder builder = DocumentBuilderFactory.getExtensiveDocumentBuilder();
    IndexWriter iw = new IndexWriter(indexPath, new SimpleAnalyzer(LuceneUtils.LUCENE_VERSION), true);
    int count = 0;
    long time = System.currentTimeMillis();
    for (String identifier : images) {
        Document doc = builder.createDocument(new FileInputStream(identifier), identifier);
        iw.addDocument(doc);
        count ++;
        if (count % 25 == 0) System.out.println(count + " files indexed.");
    }
    long timeTaken = (System.currentTimeMillis() - time);
    float sec = ((float) timeTaken) / 1000f;

    System.out.println(sec + " seconds taken, " + (timeTaken / count) + " ms per image.");
    iw.optimize();
    iw.close();

Searching in an Index

Use the ImageSearcherFactory for creating an ImageSearcher, which will retrieve the images for you from the index.
    IndexReader reader = IndexReader.open(indexPath);
    ImageSearcher searcher = ImageSearcherFactory.createDefaultSearcher();
    FileInputStream imageStream = new FileInputStream("image.jpg");
    BufferedImage bimg = ImageIO.read(imageStream);
    // searching for an image:
    ImageSearchHits hits = null;
    hits = searcher.search(bimg, reader);
    for (int i = 0; i < 5; i++) {
        System.out.println(hits.score(i) + ": " + hits.doc(i).getField(DocumentBuilder.FIELD_NAME_IDENTIFIER).stringValue());
    }

    // searching for a document:
    Document document = hits.doc(0);
    hits = searcher.search(document, reader);
    for (int i = 0; i < 5; i++) {
        System.out.println(hits.score(i) + ": " + hits.doc(i).getField(DocumentBuilder.FIELD_NAME_IDENTIFIER).stringValue());
    }