溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

如何進(jìn)行opencv3/C++PHash算法圖像檢索

發(fā)布時(shí)間:2021-12-20 13:48:11 來源:億速云 閱讀:275 作者:柒染 欄目:編程語言

如何進(jìn)行opencv3/C++PHash算法圖像檢索,針對這個(gè)問題,這篇文章詳細(xì)介紹了相對應(yīng)的分析和解答,希望可以幫助更多想解決這個(gè)問題的小伙伴找到更簡單易行的方法。

PHash算法即感知哈希算法/Perceptual Hash algorithm,計(jì)算基于低頻的均值哈希.對每張圖像生成一個(gè)指紋字符串,通過對該字符串比較可以判斷圖像間的相似度.

PHash算法原理

將圖像轉(zhuǎn)為灰度圖,然后將圖片大小調(diào)整為32*32像素并通過DCT變換,取左上角的8*8像素區(qū)域。然后計(jì)算這64個(gè)像素的灰度值的均值。將每個(gè)像素的灰度值與均值對比,大于均值記為1,小于均值記為0,得到64位哈希值。

PHash算法實(shí)現(xiàn)

將圖片轉(zhuǎn)為灰度值

將圖片尺寸縮小為32*32

resize(src, src, Size(32, 32));

DCT變換

Mat srcDCT;  dct(src, srcDCT);

計(jì)算DCT左上角8*8像素區(qū)域均值,求hash值

double sum = 0; 

for (int i = 0; 

i < 8; i++)  for (int j = 0; j < 8; j++)   

sum += srcDCT.at<float>(i,j); double average = sum/64; 

Mat phashcode= Mat::zeros(Size(8, 8), CV_8U); 

for (int i = 0; i < 8; i++)  for (int j = 0; j < 8; j++)   

phashcode.at<char>(i,j) = srcDCT.at<float>(i,j) > average ? 1:0;

hash值匹配

int d = 0;  for (int n = 0; n < srchash.size[1]; n++)   if (srchash.at<uchar>(0,n) != dsthash.at<uchar>(0,n)) d++;

即,計(jì)算兩幅圖哈希值之間的漢明距離,漢明距離越大,兩圖片越不相似。

OpenCV實(shí)現(xiàn)

如圖在下圖中對比各個(gè)圖像與圖person.jpg的漢明距離,以此衡量兩圖之間的額相似度。

#include <iostream> #include <stdio.h>#include <fstream>#include <io.h>#include <string>#include <opencv2\opencv.hpp> #include <opencv2\core\core.hpp>#include <opencv2\core\mat.hpp>using namespace std; using namespace cv; 

int fingerprint(Mat src, Mat* hash);

int main(){ Mat src = imread("E:\\image\\image\\image\\person.jpg", 0); 

 if(src.empty()) {  

cout << "the image is not exist" << endl;  

 return -1; } Mat srchash, dsthash;

 fingerprint(src, &srchash); 

for(int i = 1; i <= 8; i++) {   string path0 = "E:\\image\\image\\image\\person";  

string number;   stringstream ss;  

 ss << i;   ss >> number;   

string path = "E:\\image\\image\\image\\person" + number +".jpg";  

 Mat dst = imread(path, 0);   

if(dst.empty())  {   cout << "the image is not exist" << endl;    return -1;

  }  fingerprint(dst, &dsthash);  

int d = 0;  for (int n = 0; n < srchash.size[1]; n++)   

if (srchash.at<uchar>(0,n) != dsthash.at<uchar>(0,n)) d++;   

cout <<"person" << i <<" distance= " <<d<<"\n";  } system("pause"); 

return 0;}

int fingerprint(Mat src, Mat* hash){ resize(src, src, Size(32, 32)); 

src.convertTo(src, CV_32F); Mat srcDCT;  

dct(src, srcDCT); 

srcDCT = abs(srcDCT); 

double sum = 0; 

for (int i = 0; i < 8; i++)  for (int j = 0; j < 8; j++)   sum += srcDCT.at<float>(i,j); 

double average = sum/64; 

Mat phashcode= Mat::zeros(Size(8, 8), CV_8U); 

for (int i = 0; i < 8; i++)  for (int j = 0; j < 8; j++)   phashcode.at<char>(i,j) = srcDCT.at<float>(i,j) > average ? 1:0; 

*hash = phashcode.reshape(0,1).clone(); return 0;}

輸出漢明距離:

可以看出若將閾值設(shè)置為20則可將后三張其他圖片篩選掉。

關(guān)于如何進(jìn)行opencv3/C++PHash算法圖像檢索問題的解答就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,如果你還有很多疑惑沒有解開,可以關(guān)注億速云行業(yè)資訊頻道了解更多相關(guān)知識。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI