c# - 使用SOLR计算两个ulong之间的 "similarity"/"bitcount"

标签 c# solr bit-manipulation solrnet phash

我们有一个图像数据库,我在其中使用 Dr. Neal Krawetz's method 计算了 PHASH由 David Oftedal 实现.

部分示例代码计算这些 long 之间的差异在这里:

ulong hash1 = AverageHash(theImage);
ulong hash2 = AverageHash(theOtherImage);

uint BitCount(ulong theNumber)
{
    uint count = 0;
    for (; theNumber > 0; theNumber >>= 8) {
        count += bitCounts[(theNumber & 0xFF)];
    }
    return count;
}

Console.WriteLine("Similarity: " + ((64 - BitCount(hash1 ^ hash2)) * 100.0) / 64.0 + "%");

挑战在于我只知道这些哈希之一,我想查询 SOLR 以按相似度顺序查找其他哈希。

一些注意事项:

  1. 在这里使用 SOLR(我唯一的选择是 HBASE)
  2. 希望避免将任何自定义 java 安装到 solr 中(乐于安装现有插件)
  3. 乐于用 C# 进行大量预处理
  4. 乐于使用多个字段将数据存储为位串、长整型等
  5. 使用 SOLRNet 作为客户端

编辑,一些额外的信息(抱歉我陷入了这个问题并开始假设它是一个广为人知的领域)。这是 C# 控制台/示例应用程序的直接下载:http://01101001.net/Imghash.zip

此控制台应用程序的示例输出为:

004143737f7f7f7f phash-test-001.jpg
0041417f7f7f7f7f phash-test-002.jpg
相似度:95.3125%

最佳答案

您可以使用 Solr's Fuzzy Search为此,您必须在页面上向下滚动一点。

Solr's standard query parser supports fuzzy searches based on the Levenshtein Distance or Edit Distance algorithm. Fuzzy searches discover terms that are similar to a specified term without necessarily being an exact match. To perform a fuzzy search, use the tilde ~ symbol at the end of a single-word term.

假设您有一个如下所示的架构,其中此字段 phash 包含您计算出的 phash。

<fields>
    <!-- ... all your other fields ... -->
    <field name="phash" type="string" indexed="true" stored="true" />
</fields>

你可以执行这样的查询

q=phash:004143737f7f7f7f~0.8&
fl=score,phash

这将返回所有具有 PHASH 且 Levenshtein Distance 或 Edit Distance 至少为 80% 的文档。您不会得到您在问题中给出的 95.3125%,但会计算匹配/不匹配字符的 87.5%。

当你想看到那个值时,你可以执行下面的查询

q=phash:004143737f7f7f7f~0.8&
fl=score,phash,strdist("0041417f7f7f7f7f", phash, edit)

这是一个 function call to fetch the String Distance使用 Levenstein 或 Edit 距离,将提供类似于

+----------------+---------------------------------------+
|hash            |strdist("0041417f7f7f7f7f", hash, edit)|
+----------------+---------------------------------------+
|0041417f7f7f7f7f|1.0                                    |
+----------------+---------------------------------------+
|004143737f7f7f7f|0.875                                  |
+----------------+---------------------------------------+

当您想缩小 95.3125%87,5% 之间的差距时,您应该考虑将 PHASH 存储为八进制而不是十六进制值。

关于c# - 使用SOLR计算两个ulong之间的 "similarity"/"bitcount",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21773189/

相关文章:

c# - 命名空间 'Management' 中不存在类型或命名空间名称 'Microsoft.SqlServer' - SqlParser 和 VSTS 自动构建

java - 垃圾回收 : how is Eden space (and the other generation sizes) calculated?

java - Solrj 日期请求

c - 为什么这个 C 算术移位实现不起作用?

algorithm - 把这个 if-then 逻辑变成一个 bool 表达式?

c# - Visual studio 2010 Bing map 按经度和纬度查找位置

javascript - 在格式化数字输入时验证数字输入

c# - 在MVVM中处理事件时如何发送事件处理程序的第二个参数?

java - 使用 SolrJ 和 Solr4 进行分面

有效地将 8 位整数模式复制为 32 位整数?