Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to carry out Image Retrieval with opencv3/C++PHash algorithm

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

How to carry out opencv3/C++PHash algorithm image retrieval, for this problem, this article details the corresponding analysis and solution, hoping to help more small partners who want to solve this problem find a simpler and easier way.

PHash algorithm is Perceptual Hash algorithm, which calculates mean hash based on low frequency. A fingerprint character string is generated for each image, and similarity between images can be judged by comparing the character strings.

PHash algorithm principle

Convert the image to gray scale, then resize the image to 32*32 pixels and take the 8*8 pixel area in the upper left corner through DCT transform. Then calculate the mean of the gray values of these 64 pixels. The gray value of each pixel is compared with the mean value, and if it is greater than the mean value, it is recorded as 1, and if it is less than the mean value, it is recorded as 0, and the 64-bit hash value is obtained.

implementation of PHash algorithm

Turn pictures to grayscale

Reduce the picture size to 32*32

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

DCT transform

Mat srcDCT; dct(src, srcDCT);

Calculate the mean value of the 8*8 pixel area in the upper left corner of DCT and calculate the hash value.

double sum = 0;

for (int i = 0;

i

< 8; i++) for (int j = 0; j < 8; j++) sum += srcDCT.at(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(i,j) = srcDCT.at(i,j) >

average ? 1:0;

hash value match

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

That is, calculate the Hamming distance between the hash values of two images. The larger the Hamming distance, the less similar the two images are.

OpenCV achieved

As shown in the figure below, compare the Hamming distance between each image and the image person.jpg to measure the similarity between the two images.

#include #include #include #include #include #include #include #include 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

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report