代码之家  ›  专栏  ›  技术社区  ›  aurora

如何用狮身人面像实现颜色搜索?

  •  1
  • aurora  · 技术社区  · 15 年前

    使用MySQL按主色搜索照片非常简单。假设照片最主要颜色的r、g、b值已经存储在数据库中,这可以通过以下方式实现:

    SELECT * FROM colors
    WHERE    ABS(dominant_r - :r) < :threshold
    AND      ABS(dominant_g - :g) < :threshold
    AND      ABS(dominant_b - :b) < :threshold
    

    我想知道,是否有可能将颜色存储在sphinx中,并使用sphinx搜索引擎执行查询?

    谢谢!

    1 回复  |  直到 15 年前
        1
  •  1
  •   Remdex    15 年前

    我用狮身人面像搜索颜色。就在那里 http://code.google.com/p/hppg/ . 它是如何工作的? 非常简单,对于我存储的每种颜色,它都是数据库中的主要颜色。数据库表,witch用于sphinx索引,有一个名为“colors”的列,其内容按以下方式填充:

    /**
         * This part was changed based on formula 
         * 
         * It fits here better than algorithm
         * 
         * http://en.wikipedia.org/wiki/Tag_cloud
         * 15400 is number of maximum matches based on indexed thumbnail size 120x130 px.
         * */
    
       $max = 15400;
       $min = 25;
       $rmax = 50;
       $rmin = 1;
    
       $colorIndex = array();
       foreach ($colorsMaximumImage as $color)
       {
           $colorIndexString = trim(str_repeat(' pld'.$color['pallete_id'],round((($rmin*($color['count']-25))/($max-$min))*100)));
           if ($colorIndexString != '')
           $colorIndex[] = $colorIndexString;
       }
    

    我在这里使用了标记公式,以避免索引太大。Count是自定义托盘项目匹配的次数。它工作得很好,一些现场站点的例子可以在项目主页上找到。这样我们可以同时按颜色和关键字进行搜索:)。目前我仍在尝试取得最好的结果…

    推荐文章