Python中pHash模块使用的DCT图像哈希函数等价物
我有一个现成的Python程序,它可以接受一个图片的链接,然后对加载的图片进行处理。我尝试把图片数据传给一个叫做pHash的C++模块,然后获取图片的哈希值。我还试过用Python扩展库把图片从Python传到C程序,但都没成功。整个过程的速度是我最看重的。这包括获取查询图片的哈希值,以及从一个庞大的图片库中找到相似的图片。因此,我觉得与其传输大量的图片数据,然后再转换成CImg图片对象,不如直接在Python代码中计算哈希值,然后把哈希值传给pHash模块去找相似的图片,这样更简单也更高效。所以,我希望能在Python中获取DCT图片哈希。
有没有人知道怎么在Python中得到相同的哈希值?我不想重新发明轮子。我试着在网上搜索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;
}
我非常感谢你的帮助。谢谢!
1 个回答
2
Imagehash模块里有一个叫做phash的功能,但我不确定它和你用的phash是否能一起用。
不过,我没有使用过phash这个库,但听说它的C++版本可能会更快一些(尤其是因为PIL的速度不是最快的)。
抱歉把这个帖子翻出来,希望这样没问题。