LIRE(Lucene Image Retrieval)

来源:互联网 发布:恐怖黎明优化补丁下载 编辑:程序博客网 时间:2024/05/16 05:37
 

LIRE(Lucene Image Retrieval)相似图像索引和搜索机制


众说周知,lucene是一个开源的强大的索引工具,但是它仅限于文本索引。基于内容的图像检索(CBIR)要求我们利用图像的一些基本特征(如颜色纹理形状以及sift,surf等等)搜索相似的图片,LIRE(Lucene Image Retrieval)是一款基于lucene的图像特征索引工具,它能帮助我们方便的对图像特征建立索引和搜索,作者也在不断加入新的特征供用户使用。如果你熟悉lucene,那么用LIRE提取特征建立索引是非常方便的。

LIRE官网:http://www.semanticmetadata.net/lire/

包和源码:http://code.google.com/p/lire/

基本使用示例:http://www.semanticmetadata.net/wiki/doku.php?id=start

API:http://www.itec.uni-klu.ac.at/lire/nightly/api/index.html

本文不讨论API的调用方法,我粗略的读了下lire的源码,在这里对它的机制做个简单的说明。


LireFeature是图像特征的接口,具体的特征提取,距离计算,表示都有各自实现的类。值得一提的是,LIRE的作者实现了非常多的特征提取方法,而且都是java实现的,也没有借助opencv等工具,在此之前我能看到的sift特征提取都是c或者c++实现的。

[java] view plaincopy
  1. public interface LireFeature {  
  2.     public void extract(BufferedImage bimg);  
  3.   
  4.     public byte[] getByteArrayRepresentation();  
  5.   
  6.     public void setByteArrayRepresentation(byte[] in);  
  7.   
  8.     public void setByteArrayRepresentation(byte[] in, int offset, int length);  
  9.   
  10.     public double[] getDoubleHistogram();  
  11.   
  12.     float getDistance(LireFeature feature);  
  13.   
  14.     java.lang.String getStringRepresentation();  
  15.   
  16.     void setStringRepresentation(java.lang.String s);  
  17. }  


DocumentBuilder是建立Document的接口类,Document就是lucene中的文档,它建立的文档包含了图像的某个特征和图像的标识字符串两个Field。

[java] view plaincopy
  1. public Document createDocument(BufferedImage image, String identifier) throws FileNotFoundException;  

ChainedDocumentBuilder可以建立将多个特征综合起来的文档。

DocumentBuilderFactory是DocumentBuilder的工厂类,由它初始化各个特征的DocumentBuilder。


通过调用createDocument就能返回每个图像对应特征和标识的文档,用lucene的IndexWriter就能将它写入索引文件。


SimpleResult是单个搜索的结果,它包含3个成员变量,分别是距离(相似度),文档和索引号。它实现了Comparable接口,排序的方法是按照相似度的由高到底排序,如果相似度一样,就按照索引号在前的排在前面。

[java] view plaincopy
  1. public class SimpleResult implements Comparable<SimpleResult> {  
  2.     private float distance;  
  3.     private Document document;  
  4.     private int indexNumber = 0;  
  5.   
  6.    public int compareTo(SimpleResult o) {  
  7.         int compareValue = (int) Math.signum(distance - ((SimpleResult) o).distance);  
  8.         if (compareValue==0 && !document.equals(o.document)) {  
  9.             return (int) Math.signum(indexNumber-o.indexNumber);  
  10.         }  
  11.         return compareValue;  
  12.     }  
  13.   
  14.     @Override  
  15.     public boolean equals(Object obj) {  
  16.         // it's not the same if it's not the same class.  
  17.         if (! (obj instanceof SimpleResult)) return false;  
  18.         // it's the same if the document is the same, regardless of the distance.  
  19.         else return (document.equals(((SimpleResult)obj).document) && indexNumber == ((SimpleResult)obj).indexNumber);  
  20.     }  
  21. }  

ImageSearcherFactory是搜索的工厂类,由它初始化各个特征的搜索类。


这里主要讲一下GenericFastImageSearcher类,很多特征都能通过它来搜索,它的成员变量maxHits为搜索结果的个数,TreeSet<SimpleResult> docs是排序的搜索结果,float maxDistance搜索结果中的最大距离。

[java] view plaincopy
  1. protected float findSimilar(IndexReader reader, LireFeature lireFeature) throws IOException {  
  2.     maxDistance = -1f;  
  3.     overallMaxDistance = -1f;  
  4.   
  5.     // clear result set ...  
  6.     docs.clear();  
  7.     // Needed for check whether the document is deleted.  
  8.     Bits liveDocs = MultiFields.getLiveDocs(reader);  
  9.     Document d;  
  10.     float tmpDistance;  
  11.     int docs = reader.numDocs();  
  12.     for (int i = 0; i < docs; i++) {  
  13.         if (reader.hasDeletions() && !liveDocs.get(i)) continue// if it is deleted, just ignore it.  
  14.   
  15.         d = reader.document(i);  
  16.         tmpDistance = getDistance(d, lireFeature);  
  17.         assert (tmpDistance >= 0);  
  18.         // calculate the overall max distance to normalize score afterwards  
  19.         if (overallMaxDistance < tmpDistance) {  
  20.             overallMaxDistance = tmpDistance;  
  21.         }  
  22.         // if it is the first document:  
  23.         if (maxDistance < 0) {  
  24.             maxDistance = tmpDistance;  
  25.         }  
  26.         // if the array is not full yet:  
  27.         if (this.docs.size() < maxHits) {  
  28.             this.docs.add(new SimpleResult(tmpDistance, d, i));  
  29.             if (tmpDistance > maxDistance) maxDistance = tmpDistance;  
  30.         } else if (tmpDistance < maxDistance) {  
  31.             // if it is nearer to the sample than at least on of the current set:  
  32.             // remove the last one ...  
  33.             this.docs.remove(this.docs.last());  
  34.             // add the new one ...  
  35.             this.docs.add(new SimpleResult(tmpDistance, d, i));  
  36.             // and set our new distance border ...  
  37.             maxDistance = this.docs.last().getDistance();  
  38.         }  
  39.     }  
  40.     return maxDistance;  
  41. }  
从它的findSimilar方法可以看出,它的实现是线性检索的,也就是从头到尾遍历所有文档,并且维护一个maxHits大小的TreeSet,TreeSet里面放的是距离最小的maxHits个搜索结果。如果有不理解此过程的童鞋,可以参考一下“TOP N搜索算法”。


[java] view plaincopy
  1. public ImageSearchHits search(BufferedImage image, IndexReader reader) throws IOException {  
  2.     logger.finer("Starting extraction.");  
  3.     LireFeature lireFeature = null;  
  4.     SimpleImageSearchHits searchHits = null;  
  5.     try {  
  6.         lireFeature = (LireFeature) descriptorClass.newInstance();  
  7.         // Scaling image is especially with the correlogram features very important!  
  8.         BufferedImage bimg = image;  
  9.         if (Math.max(image.getHeight(), image.getWidth()) > GenericDocumentBuilder.MAX_IMAGE_DIMENSION) {  
  10.             bimg = ImageUtils.scaleImage(image, GenericDocumentBuilder.MAX_IMAGE_DIMENSION);  
  11.         }  
  12.         lireFeature.extract(bimg);  
  13.         logger.fine("Extraction from image finished");  
  14.   
  15.         float maxDistance = findSimilar(reader, lireFeature);  
  16.         searchHits = new SimpleImageSearchHits(this.docs, maxDistance);  
  17.     } catch (InstantiationException e) {  
  18.         logger.log(Level.SEVERE, "Error instantiating class for generic image searcher: " + e.getMessage());  
  19.     } catch (IllegalAccessException e) {  
  20.         logger.log(Level.SEVERE, "Error instantiating class for generic image searcher: " + e.getMessage());  
  21.     }  
  22.     return searchHits;  
  23. }  
search方法返回的是ImageSearchHits,它就是ArrayList<SimpleResult> results;并且利用findSimilar遍历时最大的distance做了个归一化操作, result.setDistance(1f - result.getDistance() / maxDistance);作用就是将距离变成了相似度(0到1),并且数值越大就越相似。


以上就是我粗略看的一些部分,可以看出lire的搜索过程其实是线性搜索的,由于具体的特征表示没来的及看,不知道它到底有没有利用lucene的倒排优势(对于高维数据还真不知道怎么利用倒排表)。对于大规模的数据速度估计够呛,好在lire的作者也意识到了这一点,http://www.semanticmetadata.net/2013/03/20/large-image-data-sets-with-lire-some-new-numbers/作者表示正在把LSH算法(Locality sensitive hashing)集成进去,LSH对于这种高维数据的相似检索速度可以说是质的飞跃。


自己做的一些实验,大概13万张图片,用的CEDD特征,索引文件只用了不到30M的大小,检索速度没测,目测1秒以内。

左上第一张图片就是输入图片。


0 0
原创粉丝点击