我有包含图像和 DCT 哈希的集合。如何在 LINQ where 子句中使用 Similarity(long hash, long hashOther) 方法仅选择相似图像。
此查询无效:
var results = imageCollection
.AsQueryable()
.Where(x=>ImageHash.Similarity(x.Hash, hash) >= 50)
.ToList();
出现序列化错误。
谢谢!
我有包含图像和 DCT 哈希的集合。如何在 LINQ where 子句中使用 Similarity(long hash, long hashOther) 方法仅选择相似图像。
此查询无效:
var results = imageCollection
.AsQueryable()
.Where(x=>ImageHash.Similarity(x.Hash, hash) >= 50)
.ToList();
出现序列化错误。
谢谢!
请记住,您针对 MongoDB 编写的任何 LINQ 查询最终都必须转换为 MongoDB 查询语言中的等效查询。您编写的 LINQ 查询无法转换为原始 MongoDB 查询,因为这部分:
.Where(x=>ImageHash.Similarity(x.Hash, hash) >= 50)
在 MongoDB 查询语言中没有等价物。
您需要做的是获取客户端的所有 Image _id 和 Hash 值并运行您的相似性标准客户端。一旦获得了所有足够相似的图像的 _id 值,您就可以自己获取图像。
希望您的查询还包括其他条件,以便只需要通过相似性条件客户端运行图像的子集。
它或多或少看起来像这样:
var matchingIds = collection.FindAllAs<BsonDocument>
.SetFields("_id", "Hash")
.AsEnumerable() // force client side evaluation of the rest of the query
.Where(x => ImageHash.Similarity(x["Hash"].AsByteArray, hash) > 50))
.Select(x => x["_id"]);
var query = Query.In("_id", matchingIds);
var matchingImages = collection.Find(query);
foreach (var image in matchingImages)
{
// process matching image
}
正如您在评论中所说,您是否尝试过 POCO 课程?
public class MyImage
{
[BsonId]
public ObjectId id {get;set;}
public string Hash {get;set;}
}
var results = imageCollection
.AsQueryable(MyImage)
.Where(x=>ImageHash.Similarity(x.Hash, hash) >= 50)
.ToList();