我想使用来自Python的OpenCV感知哈希函数。
这是行不通的。
import cv2
a_1 = cv2.imread('a.jpg')
cv2.img_hash_BlockMeanHash.compute(a_1)
我得到:
TypeError: descriptor 'compute' requires a 'cv2.img_hash_ImgHashBase' object but received a 'numpy.ndarray'
这也失败了
a_1_base = cv2.img_hash_ImgHashBase(a_1)
cv2.img_hash_BlockMeanHash.compute(a_1_base)
我得到:
TypeError: Incorrect type of self (must be 'img_hash_ImgHashBase' or its derivative)
Colab 笔记本显示此内容:
https://colab.research.google.com/drive/1x5ZxMBD3wFts2WKS4ip3rp4afDx0lGhi
OpenCV python接口与C++接口的常见兼容性差距(即类彼此继承的方式不同(。为此,有*_create()
静态函数。
所以你应该使用:
hsh = cv2.img_hash.BlockMeanHash_create()
hsh.compute(a_1)
在协作笔记本的副本中: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/(
我将向您展示如何使用OpenCV计算64位pHash。我定义了一个函数,该函数从传入的彩色 BGR cv2 图像返回无符号的 64 位整数 pHash:
import cv2
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
您需要安装并导入 cv2 才能正常工作。