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
图像非常相似!
相关推荐
IT_陈寒12 小时前
React的useEffect把我坑惨了,这些闭包陷阱真要命
前端·人工智能·后端
财经资讯数据_灵砚智能12 小时前
基于全球经济类多源新闻的NLP情感分析与数据可视化(夜间-次晨)2026年5月1日
大数据·人工智能·python·信息可视化·自然语言处理
Flandern111112 小时前
# 学习AI Agent中了解到的几个概念
人工智能·学习
2601_9583205712 小时前
【零基础新手入门 】OpenClaw 2.6.6 对接阿里云百炼配置教程(包含安装包)
人工智能·阿里云·云计算·open claw·小龙虾·open claw安装·open claw一键安装
java1234_小锋12 小时前
Spring AI 2.0 开发Java Agent智能体 - Spring AI项目调用本地Ollama模型
java·人工智能·spring·spring ai2.0
深海鱼在掘金12 小时前
深入浅出 LangChain —— 第六章:记忆与状态管理
人工智能·langchain·agent
qq_2837200512 小时前
Python+LangChain 调用大模型全方案深度实战:原生调用、统一接口、流式输出、异步、自定义模型全解析
人工智能·langchain·agent·rag
葫三生12 小时前
三生原理文章被AtomGit‌开源社区收录的意义探析?
人工智能·深度学习·神经网络·算法·搜索引擎·开源·transformer
冬奇Lab12 小时前
一天一个开源项目(第90篇):cmux - 为 AI Agent 时代设计的原生终端复用器
人工智能·开源·资讯
Godspeed Zhao12 小时前
具身智能中的传感器技术41——事件相机1
人工智能·科技·机器学习·具身智能·事件相机