[英]Using OpenCV's Image Hashing Module from Python
I want to use OpenCV's perceptual hashing functions from Python.我想使用 Python 中的 OpenCV感知哈希函数。
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: Colab 笔记本显示:
https://colab.research.google.com/drive/1x5ZxMBD3wFts2WKS4ip3rp4afDx0lGhi https://colab.research.google.com/drive/1x5ZxMBD3wFts2WKS4ip3rp4afDx0lGhi
It's a common compatibility gap that the OpenCV python interface has with the C++ interface (ie the classes don't inherit from each other the same way).这是 OpenCV python 接口与 C++ 接口之间的常见兼容性差距(即类不会以相同的方式相互继承)。 There are the
*_create()
static functions for that.有
*_create()
静态函数。
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在您的协作笔记本的副本中: 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/ ) ( https://pypi.org/project/opencv-python/ )
Here I show you how to compute 64-bit pHash with OpenCV.在这里,我将向您展示如何使用 OpenCV 计算 64 位 pHash。 I defined a function which returns unsigned, 64-bit integer pHash from a color BGR cv2 image passed-in:
我定义了一个函数,它从传入的颜色 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
You need to have installed and import cv2 for this to work.您需要安装并导入 cv2 才能正常工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.