OpenCV哈希算法------Marr-Hildreth 边缘检测哈希算法

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

算法描述

该类实现了 Marr-Hildreth 边缘检测哈希算法(Marr-Hildreth Hash),用于图像相似性比较。它基于 Marr-Hildreth 边缘检测器(也称为 Laplacian of Gaussian, LoG)提取图像边缘信息,并生成二进制哈希值。

这种哈希方法对图像中的边缘结构非常敏感,适合用于:

  • 图像检索
  • 图像去重
  • 检测图像是否经过裁剪、旋转或轻微变形

公共成员函数

  1. compute(InputArray inputArr, OutputArray outputArr)

计算输入图像的 Marr-Hildreth 哈希值。

参数说明:

参数 类型 描述
inputArr InputArray 输入图像,支持灰度图 (CV_8UC1) 或彩色图 (CV_8UC3)
outputArr OutputArray 输出的哈希值,类型为 CV_8U 的一维 Mat
示例:
cpp 复制代码
Mat hash;
marr_hash->compute(image, hash);
  1. compare(const Mat& hashOne, const Mat& hashTwo)

比较两个哈希值之间的差异,返回 汉明距离(Hamming Distance)。

参数说明:

参数 类型 描述
hashOne const Mat& 第一个哈希值
hashTwo const Mat& 第二个哈希值
返回值:
  • 返回两个哈希之间的 汉明距离
  • 值越小表示图像越相似

示例:

cpp 复制代码
double distance = marr_hash->compare(hash1, hash2);
if (distance < threshold) {
    std::cout << "图像相似" << std::endl;
}

代码示例

cpp 复制代码
#include <opencv2/opencv.hpp>
#include <opencv2/img_hash.hpp>
#include <iostream>

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

int main()
{
    // 加载图像(支持彩色图或灰度图)
    Mat img1 = imread("/media/dingxin/data/study/OpenCV/sources/images/img1.jpg", IMREAD_COLOR);
    Mat img2 = imread("/media/dingxin/data/study/OpenCV/sources/images/img2.jpg", IMREAD_COLOR);

    if (img1.empty() || img2.empty()) {
        cerr << "无法加载图像!" << endl;
        return -1;
    }

    // 创建 MarrHildrethHash 对象(可选参数 sigma)
    Ptr<MarrHildrethHash> marr_hash = MarrHildrethHash::create(1.2); // sigma = 1.2

    // 计算哈希值
    Mat hash1, hash2;
    marr_hash->compute(img1, hash1);
    marr_hash->compute(img2, hash2);

    // 比较哈希值(返回汉明距离)
    double distance = marr_hash->compare(hash1, hash2);
    cout << "汉明距离: " << distance << endl;

    if (distance < 10) {  // 可根据实际调整阈值
        cout << "图像非常相似!" << endl;
    } else {
        cout << "图像不相似。" << endl;
    }

    return 0;
}

运行结果

bash 复制代码
汉明距离: 9
图像非常相似!
相关推荐
xingxing_F20 分钟前
Topaz Video AI for Mac AI视频无损放大 视频画质增强
人工智能·macos·音视频
普蓝机器人30 分钟前
面向智慧农业的自主移动果蔬采摘机器人:融合视觉识别与自动驾驶的智能化农作系统研究
人工智能·学习·机器人·移动机器人·三维仿真导航
卷福同学32 分钟前
AI浏览器comet拉新,一单20美元(附详细教程)
人工智能·后端
说私域1 小时前
基于开源链动2+1模式AI智能名片S2B2C商城小程序的市场份额扩张路径研究
人工智能·小程序·开源
文火冰糖的硅基工坊1 小时前
[人工智能-大模型-72]:模型层技术 - 模型训练六大步:①数据预处理 - 基本功能与对应的基本组成函数
开发语言·人工智能·python
晚霞apple2 小时前
三维重建技术的未来创新方向
论文阅读·人工智能·深度学习·神经网络·机器学习
NocoBase2 小时前
GitHub 上最值得关注的 14 个开源 AI 低代码工具
人工智能·低代码·github
无风听海2 小时前
神经网络之语义空间
人工智能·深度学习·神经网络
cxr8282 小时前
AI提示工程第一性原理:精通原子提示,激发语言模型的基本单位
人工智能·语言模型·自然语言处理