我有一个现有的 python 程序,它接受图像 URL 并对加载的图像进行操作。我尝试将图像数据传递到 pHash C++ 中的模块,然后获取图像哈希。我尝试过使用 python extension library将图像从 python 传递到 C 程序,但没有成功。整个过程的速度是首要考虑的。它包括获取查询图像的哈希值,从庞大的图像集合中找到相似的图像系统。因此,我觉得与其传递巨大的图像数据,然后转换为 CImg 图像对象,不如直接在 python 代码中计算哈希值,并将哈希值传递给 pHash 模块以找出相似的图像会更好、更容易。所以,我期待在 Python 中获得 DCT 图像哈希。
有谁知道如何在 python 中获得相同的哈希值吗?我不想重新发明轮子。我尝试用 google 搜索并在 python 中找到等效的函数,但没有成功。这是图像哈希函数:(来源:pHash 模块)
int ph_dct_imagehash(const char* file,ulong64 &hash){
if (!file){
return -1;
}
CImg<uint8_t> src;
try {
src.load(file);
} catch (CImgIOException ex){
return -1;
}
CImg<float> meanfilter(7,7,1,1,1);
CImg<float> img;
if (src.spectrum() == 3){
img = src.RGBtoYCbCr().channel(0).get_convolve(meanfilter);
} else if (src.spectrum() == 4){
int width = img.width();
int height = img.height();
int depth = img.depth();
img = src.crop(0,0,0,0,width-1,height-1,depth-1,2).RGBtoYCbCr().channel(0).get_convolve(meanfilter);
} else {
img = src.channel(0).get_convolve(meanfilter);
}
img.resize(32,32);
CImg<float> *C = ph_dct_matrix(32);
CImg<float> Ctransp = C->get_transpose();
CImg<float> dctImage = (*C)*img*Ctransp;
CImg<float> subsec = dctImage.crop(1,1,8,8).unroll('x');;
float median = subsec.median();
ulong64 one = 0x0000000000000001;
hash = 0x0000000000000000;
for (int i=0;i< 64;i++){
float current = subsec(i);
if (current > median)
hash |= one;
one = one << 1;
}
delete C;
return 0;
}
我非常感谢您的帮助。非常感谢
最佳答案
Imagehash模块有 phash 的实现,但我不知道它是否与您的 phash 实现兼容。
也就是说:我对 phash 库没有任何经验,但 C++ 版本可能更快(尤其是 PIL 不是最快的)
抱歉破坏了这篇文章,希望没问题
关于pHash 模块中使用的 Python 等效 DCT 图像哈希函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15229310/