[英]Using OpenCV's Image Hashing Module from Python
我想使用 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
在这里,我将向您展示如何使用 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 才能正常工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.