如何从opencvimage_hash模块PHashImpl中获取hash

How to obtain the hash from the opencv image_hash module PHashImpl

我正在尝试使用 OpenCv 库中的 phash 算法实现计算图像的感知哈希。

因为我使用 C# 来完成任务,所以我使用 Emgu wrapper for OpenCv

计算非常先进:

var sourceImage = CvInvoke.Imread("some path to an image", ImreadModes.Color);
var hashAlgorithm = new PHash()
IOutputArray hash = new Mat();
hashAlgorithm.Compute(sourceImage, hash);

//hash is of type IOutpurArry how i can get the string version of the hash

因此,计算方法将散列变量分配给 IOutputArray 类型的算法结果。

我的问题是如何获得哈希的字符串表示形式?

从 Mat 对象获取字符串表示的方法是创建一个自己的函数。

例如,它可以是 Mat 中所有值的串联。这些是 byte 0-255 类型的像素强度。 为了获得相同大小的字符串,我们可以将它们转换为十六进制值。

var alg = new PHash();
var hashResult = new Mat();
//Compute the hash
alg.Compute(/*some image as Mat*/, hashResult);

// Get the data from the unmanage memeory
var data = new byte[hashResult.Width * hashResult.Height];
Marshal.Copy(hashResult.DataPointer, data, 0, hashResult.Width * hashResult.Height);

// Concatenate the Hex values representation
var hashHex = BitConverter.ToString(data).Replace("-", string.Empty);

// hashHex has the hex values concatenation as string;