OpenCV多种图像哈希算法的实现比较

  • 操作系统:ubuntu22.04
  • OpenCV版本:OpenCV4.9
  • IDE:Visual Studio Code
  • 编程语言:C++11

算法描述

OpenCV提供用于提取图像哈希值的算法,以及在大规模数据集中快速找出最相似图像的方法。

所有函数的命名空间为:cv::img_hash。

支持的算法:

  • 平均哈希(也称为差分哈希)Average hash (also called Different hash)
  • PHash(也称为感知哈希)PHash (also called Perceptual hash)
  • Marr Hildreth 哈希 Marr Hildreth Hash
  • 径向方差哈希 Radial Variance Hash
  • 分块均值哈希(支持模式 0 和 1)Block Mean Hash (modes 0 and 1)
  • 颜色矩哈希 Color Moment Hash
    (这是目前唯一一个对旋转攻击具有抗性的哈希算法(-90~90 度))

你可以通过以下论文和网站了解更多关于图像哈希的内容:

  • "Implementation and benchmarking of perceptual image hash functions" 310
  • "Looks Like It" 145

示例代码

cpp 复制代码
#include "opencv2/core.hpp"
#include "opencv2/core/ocl.hpp"
#include "opencv2/highgui.hpp"
#include "opencv2/img_hash.hpp"
#include <iostream>

using namespace cv;
using namespace cv::img_hash;
using namespace std;

template < typename T > inline void test_one( const std::string& title, const Mat& a, const Mat& b )
{
    cout << "=== " << title << " ===" << endl;
    TickMeter tick;
    Mat hashA, hashB;
    Ptr< ImgHashBase > func;
    func = T::create();
    tick.reset();
    tick.start();
    func->compute( a, hashA );
    tick.stop();
    cout << "compute1: " << tick.getTimeMilli() << " ms" << endl;
    tick.reset();
    tick.start();
    func->compute( b, hashB );
    tick.stop();
    cout << "compute2: " << tick.getTimeMilli() << " ms" << endl;
    cout << "compare: " << func->compare( hashA, hashB ) << endl << endl;
    ;
}

int main( int argc, char** argv )
{
    ocl::setUseOpenCL( false );
    Mat input  = imread( "/media/dingxin/data/study/OpenCV/sources/images/img1.jpg");
    Mat target = imread( "/media/dingxin/data/study/OpenCV/sources/images/img1.jpg");
    test_one< AverageHash >( "AverageHash", input, target );
    test_one< PHash >( "PHash", input, target );
    test_one< MarrHildrethHash >( "MarrHildrethHash", input, target );
    test_one< RadialVarianceHash >( "RadialVarianceHash", input, target );
    test_one< BlockMeanHash >( "BlockMeanHash", input, target );
    return 0;
}

运行结果

bash 复制代码
=== AverageHash ===
compute1: 22.391 ms
compute2: 0.01228 ms
compare: 0

=== PHash ===
compute1: 0.048038 ms
compute2: 0.028032 ms
compare: 0

=== MarrHildrethHash ===
compute1: 40.5077 ms
compute2: 7.61326 ms
compare: 0

=== RadialVarianceHash ===
compute1: 0.640129 ms
compute2: 0.670026 ms
compare: 1

=== BlockMeanHash ===
compute1: 0.173648 ms
compute2: 0.169781 ms
compare: 0

不同攻击下的性能表现


性能图表

与 PHash 库的速度比较(来自 ukbench 的 100 张图像)

哈希计算图表


哈希比较图表 如你所见,img_hash 模块的哈希计算速度远超 PHash 库。

附注:我没有列出平均哈希、PHash 和颜色矩哈希的比较,因为在 PHash 库中找不到它们。

动机

将有用的图像哈希算法集成到 OpenCV 中,这样我们就无需反复重写这些算法或依赖第三方库(例如 PHash 库)。BOVW(Bag of Visual Words)或相关匹配虽然好且鲁棒,但与图像哈希相比非常慢。如果你需要处理大规模基于内容的图像检索(CBIR)问题,图像哈希是一个更为合理的解决方案。

更多信息

你可以从以下链接了解更多关于 img_hash 模块的信息。这些链接展示了如何从 ukbench 数据集中找到相似图像,并提供了对不同类型攻击(对比度、模糊、噪声(高斯、椒盐)、JPEG 压缩、水印、调整大小)的全面基准测试。

OpenCV 图像哈希模块简介
加速OpenCV图像哈(img_hash)并介绍颜色矩哈希

贡献者

Tham Ngap Wei, thamngapwei@gmail.com

相关推荐
2501_9247314743 分钟前
复杂路况下车牌识别准确率↑19%:陌讯动态特征融合算法实战解析
人工智能·算法·目标检测·计算机视觉·目标跟踪
LgZhu(Yanker)5 小时前
27、企业维修保养(M&R)全流程管理:从预防性维护到智能运维的进阶之路
大数据·运维·人工智能·erp·设备·维修·保养
ModelWhale6 小时前
“大模型”技术专栏 | 和鲸 AI Infra 架构总监朱天琦:大模型微调与蒸馏技术的全景分析与实践指南(上)
人工智能·大模型·大语言模型
lxmyzzs8 小时前
【图像算法 - 08】基于 YOLO11 的抽烟检测系统(包含环境搭建 + 数据集处理 + 模型训练 + 效果对比 + 调参技巧)
人工智能·yolo·目标检测·计算机视觉
霖008 小时前
ZYNQ实现FFT信号处理项目
人工智能·经验分享·神经网络·机器学习·fpga开发·信号处理
GIS数据转换器8 小时前
AI 技术在智慧城市建设中的融合应用
大数据·人工智能·机器学习·计算机视觉·系统架构·智慧城市
竹子_239 小时前
《零基础入门AI:传统机器学习进阶(从拟合概念到K-Means算法)》
人工智能·算法·机器学习
上海云盾-高防顾问9 小时前
DDoS 防护的未来趋势:AI 如何重塑安全行业?
人工智能·安全·ddos
Godspeed Zhao9 小时前
自动驾驶中的传感器技术17——Camera(8)
人工智能·机器学习·自动驾驶·camera·cis
摆烂工程师9 小时前
GPT-5 即将凌晨1点进行发布,免费用户可以使用 GPT-5
前端·人工智能·程序员