In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-09-14 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.
The market share of Chrome browser on the desktop has exceeded 70%, and users are complaining about
The world's first 2nm mobile chip: Samsung Exynos 2600 is ready for mass production.According to a r
A US federal judge has ruled that Google can keep its Chrome browser, but it will be prohibited from
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
About us Contact us Product review car news thenatureplanet
More Form oMedia: AutoTimes. Bestcoffee. SL News. Jarebook. Coffee Hunters. Sundaily. Modezone. NNB. Coffee. Game News. FrontStreet. GGAMEN
© 2024 shulou.com SLNews company. All rights reserved.