简体   繁体   中英

How to obtain the hash from the opencv image_hash module PHashImpl

I'm trying to compute the perceptual hash for an image using the phash algorithm implementation in the OpenCv library.

Since I'm using C# to accomplish the task I use the Emgu wrapper for OpenCv .

The computation is prety forward:

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

As a result the compute method assign the hash variable to the result of the algorithm wich is an IOutputArray type.

My question is how can i obtain a string representation of the hash?

The way to obtain a string representation from the Mat object is to creating an own function.

For example it could be the concatenation of all the values from the Mat. Those are the intensities of the pixels which will be of type byte 0-255. And in order to get same size strings we can convert them to Hex values.

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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM