OpenCV 4.13.0
開源計算機視覺庫 (Open Source Computer Vision)
正在載入...
正在搜尋...
未找到匹配項
該模組提供了不同影像雜湊演算法的實現。

詳細說明

提供提取影像雜湊值的演算法,以及在大規模資料集中快速查詢最相似影像的方法。

所有函式的名稱空間是 cv::img_hash

支援的演算法

您可以從以下論文和網站了解更多關於影像雜湊的資訊

程式碼示例

#include "opencv2/core.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;
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)
{
if (argc != 3)
{
cerr << "必須輸入輸入影像和目標影像的路徑。例如:hash_samples lena.jpg lena2.jpg" << endl;
return -1;
}
ocl::setUseOpenCL(false);
Mat input = imread(argv[1]);
Mat target = imread(argv[2]);
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;
}
n 維密集陣列類
定義於 mat.hpp:840
一個用於測量經過時間的類。
定義在 utility.hpp:326
void start()
開始計算週期數。
定義在 utility.hpp:335
void stop()
停止計算週期數。
定義在 utility.hpp:341
void reset()
重置內部值。
定義 utility.hpp:430
double getTimeMilli() const
返回以毫秒為單位經過的時間。
定義 utility.hpp:365
std::shared_ptr< _Tp > Ptr
定義 cvstd_wrapper.hpp:23
Mat imread(const String &filename, int flags=IMREAD_COLOR_BGR)
從檔案載入影像。
int main(int argc, char *argv[])
定義 highgui_qt.cpp:3
定義 average_hash.hpp:11
定義 core.hpp:107
STL 名稱空間。

不同攻擊下的效能

效能圖表

與 PHash 庫的速度比較(來自 ukbench 的 100 張影像)

如您所見,img_hash 模組的雜湊計算速度遠超 PHash 庫

附註:我沒有列出平均雜湊、PHash 和顏色矩雜湊的比較,因為我在 PHash 中找不到它們。

動機

將有用的影像雜湊演算法收集到 OpenCV 中,這樣我們就無需一次又一次地重寫它們,或依賴於其他第三方庫(例如 PHash 庫)。BOVW 或相關匹配是良好且魯棒的方法,但與影像雜湊相比,它們非常慢。如果您需要處理大規模 CBIR(基於內容的影像檢索)問題,影像雜湊是一個更合理的解決方案。

更多資訊

您可以從以下連結瞭解更多關於 img_hash 模組的資訊,這些連結展示瞭如何從 ukbench 資料集中查詢相似影像,並提供了對不同攻擊(對比度、模糊、噪聲(高斯、椒鹽)、jpeg 壓縮、水印、縮放)的全面基準測試。

OpenCV 影像雜湊模組簡介 加速 OpenCV (img_hash) 的影像雜湊並介紹顏色矩雜湊

貢獻者

Tham Ngap Wei, thamn.nosp@m.gapw.nosp@m.ei@gm.nosp@m.ail..nosp@m.com

類  cv::img_hash::AverageHash
 計算輸入影像的平均雜湊值。 更多...
 
類  cv::img_hash::BlockMeanHash
 基於塊均值的影像雜湊。 更多...
 
類  cv::img_hash::ColorMomentHash
 基於顏色矩的影像雜湊。 更多...
 
類  cv::img_hash::ImgHashBase
 影像雜湊演算法的基類。 更多...
 
類  cv::img_hash::MarrHildrethHash
 基於 Marr-Hildreth 運算元的雜湊,速度最慢但區分度更高。 更多...
 
類  cv::img_hash::PHash
 pHash 更多...
 
類  cv::img_hash::RadialVarianceHash
 基於 Radon 變換的影像雜湊。 更多...
 

列舉

enum  cv::img_hash::BlockMeanHashMode {
  cv::img_hash::BLOCK_MEAN_HASH_MODE_0 = 0 ,
  cv::img_hash::BLOCK_MEAN_HASH_MODE_1 = 1
}
 

函式

void cv::img_hash::averageHash (cv::InputArray inputArr, cv::OutputArray outputArr)
 一次呼叫計算 img_hash::AverageHash
 
void cv::img_hash::blockMeanHash (cv::InputArray inputArr, cv::OutputArray outputArr, int mode=BLOCK_MEAN_HASH_MODE_0)
 計算輸入影像的塊均值雜湊。
 
void cv::img_hash::colorMomentHash (cv::InputArray inputArr, cv::OutputArray outputArr)
 計算輸入的顏色矩雜湊,該演算法來源於論文“Perceptual Hashing for Color Images Using Invariant Moments”。
 
void cv::img_hash::marrHildrethHash (cv::InputArray inputArr, cv::OutputArray outputArr, float alpha=2.0f, float scale=1.0f)
 計算輸入影像的平均雜湊值。
 
void cv::img_hash::pHash (cv::InputArray inputArr, cv::OutputArray outputArr)
 計算輸入影像的 pHash 值。
 
void cv::img_hash::radialVarianceHash (cv::InputArray inputArr, cv::OutputArray outputArr, double sigma=1, int numOfAngleLine=180)
 計算輸入影像的徑向方差雜湊。
 

列舉型別文件 (Enumeration Type Documentation)

◆ BlockMeanHashMode

#include <opencv2/img_hash/block_mean_hash.hpp>

列舉值 (Enumerator)
BLOCK_MEAN_HASH_MODE_0 
Python: cv.img_hash.BLOCK_MEAN_HASH_MODE_0

使用較少的塊生成 16*16/8 位元組的雜湊值

BLOCK_MEAN_HASH_MODE_1 
Python: cv.img_hash.BLOCK_MEAN_HASH_MODE_1

使用塊(步長/2),生成 31*31/8 + 1 位元組的雜湊值

函式文件 (Function Documentation)

◆ averageHash()

void cv::img_hash::averageHash ( cv::InputArray inputArr,
cv::OutputArray outputArr )
Python
cv.img_hash.averageHash(inputArr[, outputArr]) -> outputArr

#include <opencv2/img_hash/average_hash.hpp>

一次呼叫計算 img_hash::AverageHash

引數
inputArr需要計算雜湊值的輸入影像,型別應為 CV_8UC4、CV_8UC3 或 CV_8UC1。
outputArr輸入的雜湊值,它將包含 16 個十六進位制數字,返回型別為 CV_8U。

◆ blockMeanHash()

void cv::img_hash::blockMeanHash ( cv::InputArray inputArr,
cv::OutputArray outputArr,
int mode = BLOCK_MEAN_HASH_MODE_0 )
Python
cv.img_hash.blockMeanHash(inputArr[, outputArr[, mode]]) -> outputArr

#include <opencv2/img_hash/block_mean_hash.hpp>

計算輸入影像的塊均值雜湊。

引數
inputArr需要計算雜湊值的輸入影像,型別應為 CV_8UC4、CV_8UC3 或 CV_8UC1。
outputArr輸入的雜湊值,它將包含 16 個十六進位制數字,返回型別為 CV_8U。
mode模式

◆ colorMomentHash()

void cv::img_hash::colorMomentHash ( cv::InputArray inputArr,
cv::OutputArray outputArr )
Python
cv.img_hash.colorMomentHash(inputArr[, outputArr]) -> outputArr

#include <opencv2/img_hash/color_moment_hash.hpp>

計算輸入的顏色矩雜湊,該演算法來源於論文“Perceptual Hashing for Color Images Using Invariant Moments”。

引數
inputArr需要計算雜湊值的輸入影像,型別應為 CV_8UC4、CV_8UC3 或 CV_8UC1。
outputArr42 個雙精度浮點數(CV_64F(double))的雜湊值

◆ marrHildrethHash()

void cv::img_hash::marrHildrethHash ( cv::InputArray inputArr,
cv::OutputArray outputArr,
float alpha = 2.0f,
float scale = 1.0f )
Python
cv.img_hash.marrHildrethHash(inputArr[, outputArr[, alpha[, scale]]]) -> outputArr

#include <opencv2/img_hash/marr_hildreth_hash.hpp>

計算輸入影像的平均雜湊值。

引數
inputArr需要計算雜湊值的輸入影像,型別應為 CV_8UC4、CV_8UC3、CV_8UC1。
outputArr輸入的雜湊值,它將包含 16 個十六進位制數字,返回型別為 CV_8U。
alphaMarr 小波的尺度因子(預設=2)。
scale尺度因子的級別(預設 = 1)。

◆ pHash()

void cv::img_hash::pHash ( cv::InputArray inputArr,
cv::OutputArray outputArr )
Python
cv.img_hash.pHash(inputArr[, outputArr]) -> outputArr

#include <opencv2/img_hash/phash.hpp>

計算輸入影像的 pHash 值。

引數
inputArr需要計算雜湊值的輸入影像,型別應為 CV_8UC4、CV_8UC3、CV_8UC1。
outputArr輸入的雜湊值,它將包含 8 個位元組的值。

◆ radialVarianceHash()

void cv::img_hash::radialVarianceHash ( cv::InputArray inputArr,
cv::OutputArray outputArr,
double sigma = 1,
int numOfAngleLine = 180 )
Python
cv.img_hash.radialVarianceHash(inputArr[, outputArr[, sigma[, numOfAngleLine]]]) -> outputArr

#include <opencv2/img_hash/radial_variance_hash.hpp>

計算輸入影像的徑向方差雜湊。

引數
inputArr需要計算雜湊值的輸入影像,型別應為 CV_8UC4、CV_8UC3、CV_8UC1。
outputArr輸入的雜湊值
sigma高斯核的標準差
numOfAngleLine考慮的角度數