Alex的博客

由于博客程序改版,且org域名无法备案。 故此博客不再更新,以转为新 站博文家。 https://www.32e.top



libpuzzle-php扩展比较图片间的相似性 (转载)

Libpuzzle

The Puzzle library is designed to quickly find visually similar images (gif, png, jpg), even if they have been resized, recompressed, recolored or slightly modified. The library is free, lightweight yet very fast, configurable, easy to use and it has been designed with security in mind.

采用libpuzzle扩展库,可以非常赞的比较两张图片的相似度。

安装

ubuntu 下:sudo apt-get install libpuzzle-php即可

当然可以按照传统的扩展php的方法进行安装,源码下载

运用实例

可用函数及其功能:

 

1. $signature=puzzle_fill_cvec_from_file($image_filename); // get the signature of a picture, 其实就是拿到一个图片的信息签名;
2. $d = puzzle_vector_normalized_distance($signature1, $signature2); //根据各自的签名信息,通过比较获得之间的差距,从而衡量二者的相似度
3. $compressed_signature = puzzle_compress_cvec($signature); //获取压缩状态下的签名,减小数据库存储

4. $signature = puzzle_uncompress_cvec($compressed_signature); //用时,解压缩

 

threshold阈值常量:

PUZZLE_CVEC_SIMILARITY_THRESHOLD==0.6; 
PUZZLE_CVEC_SIMILARITY_HIGH_THRESHOLD==0.7;
PUZZLE_CVEC_SIMILARITY_LOW_THRESHOLD==0.3;
PUZZLE_CVEC_SIMILARITY_LOWER_THRESHOLD==0.2

 

当$d低于阈值是我们才认为图片间是相类似的。如下:

Php代码 复制代码 收藏代码
  1. <?  
  2. if ($d < PUZZLE_CVEC_SIMILARITY_THRESHOLD) {  
  3.   echo "Pictures look similar\n";  
  4. }else{  
  5. echo "Pictures are different, distance=$d\n";  
  6. }  
  7. ?>  
浏览2295  评论0  Alex于 2013-7-2 23:27
发言