天天看點

LIRE的使用:建立索引

LIRE(Lucene Image REtrieval)提供一種的簡單方式來建立基于圖像特性的Lucene索引。利用該索引就能夠建構一個基于内容的圖像檢索(content- based image retrieval,CBIR)系統,來搜尋相似的圖像。LIRE使用的特性都取自MPEG-7标準: ScalableColor、ColorLayout、EdgeHistogram。

使用

DocumentBuilderFactory

 建立 

DocumentBuilder

,例如

DocumentBuilderFactory.getCEDDDocumentBuilder()

将圖檔加入索引index 需要以下2步:

  • 使用 

    DocumentBuilder

     建立Document:

    builder.createDocument(FileInputStream, String)

    .(第一個參數是圖檔檔案)
  • 将document 加入 index.

 LIRE支援很多種的特征值。具體可以看 

DocumentBuilderFactory

 類的源代碼。也可以使用 

ChainedDocumentBuilder

 同時使用多種特征值。

建立索引的方法如下代碼所示

/**
 * Simple index creation with Lire
 *
 * @author Mathias Lux, [email protected]
 */
public class Indexer {
    public static void main(String[] args) throws IOException {
        // Checking if arg[0] is there and if it is a directory.
        boolean passed = false;
        if (args.length > 0) {
            File f = new File(args[0]);
            System.out.println("Indexing images in " + args[0]);
            if (f.exists() && f.isDirectory()) passed = true;
        }
        if (!passed) {
            System.out.println("No directory given as first argument.");
            System.out.println("Run \"Indexer <directory>\" to index files of a directory.");
            System.exit(1);
        }
        // Getting all images from a directory and its sub directories.
        ArrayList<String> images = FileUtils.getAllImages(new File(args[0]), true);
 
        // Creating a CEDD document builder and indexing al files.
        DocumentBuilder builder = DocumentBuilderFactory.getCEDDDocumentBuilder();
        // Creating an Lucene IndexWriter
        IndexWriterConfig conf = new IndexWriterConfig(LuceneUtils.LUCENE_VERSION,
                new WhitespaceAnalyzer(LuceneUtils.LUCENE_VERSION));
        IndexWriter iw = new IndexWriter(FSDirectory.open(new File("index")), conf);
        // Iterating through images building the low level features
        for (Iterator<String> it = images.iterator(); it.hasNext(); ) {
            String imageFilePath = it.next();
            System.out.println("Indexing " + imageFilePath);
            try {
                BufferedImage img = ImageIO.read(new FileInputStream(imageFilePath));
                Document document = builder.createDocument(img, imageFilePath);
                iw.addDocument(document);
            } catch (Exception e) {
                System.err.println("Error reading image or indexing it.");
                e.printStackTrace();
            }
        }
        // closing the IndexWriter
        iw.close();
        System.out.println("Finished indexing.");
    }
}
           

原文:

http://www.semanticmetadata.net/wiki/doku.php?id=lire:createindex

繼續閱讀