Using OpenCV's Image Hashing Module from Python

It's a common compatibility gap that the OpenCV python interface has with the C++ interface (i.e. the classes don't inherit from each other the same way). There are the *_create() static functions for that.

So you should use:

hsh = cv2.img_hash.BlockMeanHash_create()
hsh.compute(a_1)

In a copy of your collab notebook: https://colab.research.google.com/drive/1CLJNPPbeO3CiQ2d8JgPxEINpr2oNMWPh#scrollTo=OdTtUegmPnf2


pip install opencv-python
pip install opencv-contrib-python    #img_hash in this one 

(https://pypi.org/project/opencv-python/)


Here I show you how to compute 64-bit pHash with OpenCV. I defined a function which returns unsigned, 64-bit integer:

def pHash(cv_image):
    imgg = cv2.cvtColor(cv_image, cv2.COLOR_BGR2GRAY);
    h=cv2.img_hash.pHash(imgg) # 8-byte hash
    pH=int.from_bytes(h.tobytes(), byteorder='big', signed=False)
    return pH

You need to have installed and import cv2 for this to work.