LIRE的使用


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種方法結合后的查全率和查准率。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM