我最近也面临同样的问题,为了解决这个问题(简单快速的算法比较两个图像),我贡献了
img_hash module
要打开“控制”,您可以从
this link
.
img_哈希模块提供六种图像哈希算法,非常容易使用。
代码示例
起源Lena
模糊莉娜
调整Lena的大小
移动Lena
#include
#include
#include
#include
#include
#include
void compute(cv::Ptr<:img_hash::imghashbase> algo)
{
auto input = cv::imread("lena.png");
cv::Mat similar_img;
//detect similiar image after blur attack
cv::GaussianBlur(input, similar_img, {7,7}, 2, 2);
cv::imwrite("lena_blur.png", similar_img);
cv::Mat hash_input, hash_similar;
algo->compute(input, hash_input);
algo->compute(similar_img, hash_similar);
std::cout<
algo->compare(hash_input, hash_similar)<<:endl>
//detect similar image after shift attack
similar_img.setTo(0);
input(cv::Rect(0,10, input.cols,input.rows-10)).
copyTo(similar_img(cv::Rect(0,0,input.cols,input.rows-10)));
cv::imwrite("lena_shift.png", similar_img);
algo->compute(similar_img, hash_similar);
std::cout<
algo->compare(hash_input, hash_similar)<<:endl>
//detect similar image after resize
cv::resize(input, similar_img, {120, 40});
cv::imwrite("lena_resize.png", similar_img);
algo->compute(similar_img, hash_similar);
std::cout<
algo->compare(hash_input, hash_similar)<<:endl>
}
int main()
{
using namespace cv::img_hash;
//disable opencl acceleration may(or may not) boost up speed of img_hash
cv::ocl::setUseOpenCL(false);
//if the value after compare <= 8, that means the images
//very similar to each other
compute(ColorMomentHash::create());
//there are other algorithms you can try out
//every algorithms have their pros and cons
compute(AverageHash::create());
compute(PHash::create());
compute(MarrHildrethHash::create());
compute(RadialVarianceHash::create());
//BlockMeanHash support mode 0 and mode 1, they associate to
//mode 1 and mode 2 of PHash library
compute(BlockMeanHash::create(0));
compute(BlockMeanHash::create(1));
}
在这种情况下,colormomenthash给我们最好的结果
高斯模糊攻击:0.567521
轮班攻击:0.229728
调整攻击大小:0.229358
每种算法的优缺点
img_哈希的性能也很好
与Phash库的速度比较(UKbench的100张图像)