码迷,mamicode.com
首页 > 其他好文 > 详细

转:LIRE的使用

时间:2015-05-19 20:44:35      阅读:336      评论:0      收藏:0      [点我收藏+]

标签:

LIRE的使用:创建索引

LIRE(Lucene Image REtrieval)提供一种的简单方式来创建基于图像特性的Lucene索引。利用该索引就能够构建一个基于内容的图像检索(content- based image retrieval,CBIR)系统,来搜索相似的图像。LIRE使用的特性都取自MPEG-7标准: ScalableColor、ColorLayout、EdgeHistogram。

使用DocumentBuilderFactory 创建 DocumentBuilder,例如DocumentBuilderFactory.getCEDDDocumentBuilder()

将图片加入索引index 需要以下2步:

  • 使用 DocumentBuilder 创建Documentbuilder.createDocument(FileInputStream, String).(第一个参数是图片文件)
  • document 加入 index.

LIRE支持很多种的特征值。具体可以看 DocumentBuilderFactory 类的源代码。也可以使用 ChainedDocumentBuilder 同时使用多种特征值。

创建索引的方法如下代码所示

/**  
 * Simple index creation with Lire  
 *  
 * @author Mathias Lux, mathias@juggle.at  
 */   
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.");   
    }   
}

LIRE的使用:搜索相似的图片

使用 ImageSearcherFactory 创建 ImageSearcher例如ImageSearcherFactory.createDefaultSearcher()

 ImageSearcher 可以通过 InputStream BufferedImage,或者一个描述图像的Lucene Document 进行检索。 例如使用search(BufferedImage, IndexReader) 或者search(Document, IndexReader).

返回的结果是一个 ImageSearchHits 类似于Lucene 中的Hits

/**  
 * Simple image retrieval with Lire  
 * @author Mathias Lux, mathias <at> juggle <dot> at  
 */   
public class Searcher {   
    public static void main(String[] args) throws IOException {   
        // Checking if arg[0] is there and if it is an image.   
        BufferedImage img = null;   
        boolean passed = false;   
        if (args.length > 0) {   
            File f = new File(args[0]);   
            if (f.exists()) {   
                try {   
                    img = ImageIO.read(f);   
                    passed = true;   
                } catch (IOException e) {   
                    e.printStackTrace();     
                }   
            }   
        }   
        if (!passed) {   
            System.out.println("No image given as first argument.");   
            System.out.println("Run \"Searcher <query image>\" to search for <query image>.");   
            System.exit(1);   
        }   
    
        IndexReader ir = DirectoryReader.open(FSDirectory.open(new File("index")));   
        ImageSearcher searcher = ImageSearcherFactory.createCEDDImageSearcher(10);   
    
        ImageSearchHits hits = searcher.search(img, ir);   
        for (int i = 0; i < hits.length(); i++) {   
            String fileName = hits.doc(i).getValues(DocumentBuilder.FIELD_NAME_IDENTIFIER)[0];   
            System.out.println(hits.score(i) + ": \t" + fileName);   
        }   
    }   
}

LIRe提供的6种图像特征描述方法的评测

Rui Gan等人(看名字来说应该是中国人,机构写的Sun Yat-sen University应该是中山大学,但是很不幸没有找到相应的中文论文)在论文《Using LIRe to Implement Image Retrieval System Based on Multi-Feature Descriptor》中,测试了开源基于内容的图像检索类库LIRe的各种图像特征的性能。在此记录一下以作参考。

这里再提一下LIRe 的简介:LIRE(Lucene Image REtrieval)提供一种的简单方式来创建基于图像特性的Lucene索引。利用该索引就能够构建一个基于内容的图像检索(content- based image retrieval,CBIR)系统,来搜索相似的图像。LIRE使用的特性都取自MPEG-7标准: ScalableColor、ColorLayout、EdgeHistogram,目前已经支持其他更多的特性。此外该类库还提供一个搜索该索引的方 法。

 

本文测试了LIRe提供的以下6种特征描述方法:

技术分享

实验以供选择了13个种类,一共100张图片做测试,这些图如下图所示(只是一部分):

测试的步骤不再多说,就是使用LIRe的6种特征描述方法分别建立6个索引,然后分别检索。最后得到的实验结果如图所示:

注:6种特征描述方法分别标以A,B,C,D,E,F,G。其中C为最常见的颜色直方图。

查准率(Precision)如下表所示。

技术分享

   技术分享

 

查全率(Recall)如下表所示。

技术分享

 

查全率和查准率合计如下表所示。

左边一栏对不同种类的图片分别给出了最适合的特征描述方法。

右边一栏对不同种类的图片分别给出了6种方法结合后的查全率和查准率。

技术分享

转:LIRE的使用

标签:

原文地址:http://www.cnblogs.com/xkfz007/p/4515387.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!