pythonopencvphash

Using OpenCV's Image Hashing Module from Python


I want to use OpenCV's perceptual hashing functions from Python.

This isn't working.

import cv2
a_1 = cv2.imread('a.jpg')
cv2.img_hash_BlockMeanHash.compute(a_1)

I get:

TypeError: descriptor 'compute' requires a 'cv2.img_hash_ImgHashBase' object but received a 'numpy.ndarray'

And this is failing too

a_1_base = cv2.img_hash_ImgHashBase(a_1) 
cv2.img_hash_BlockMeanHash.compute(a_1_base)

I get:

TypeError: Incorrect type of self (must be 'img_hash_ImgHashBase' or its derivative)

Colab notebook showing this:

https://colab.research.google.com/drive/1x5ZxMBD3wFts2WKS4ip3rp4afDx0lGhi


Solution

  • 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