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 use Python dHash algorithm

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly introduces "how to use the Python dHash algorithm". In the daily operation, I believe many people have doubts about how to use the Python dHash algorithm. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts about "how to use the Python dHash algorithm". Next, please follow the editor to study!

Description

1. Zoom out: zoom out to 9: 8, so it has 72 pixels.

2. Convert to grayscale image.

3. Calculate the difference value: the dHash algorithm works between adjacent pixels, so there are 8 different differences between 9 pixels per row, a total of 8 lines, resulting in 64 differences.

4. Get fingerprints: if the left pixel is brighter than the right pixel, record it as 1, otherwise it is 0.

5. Finally, the fingerprints of the two pictures are compared to obtain the hamming distance.

Example

#-*-coding: utf-8-*-# use python to implement multiple methods for image recognition import cv2import numpy as npfrom matplotlib import pyplot as plt # the simplest implementation using gray histogram as similarity comparison def classify_gray_hist (image1,image2,size = (256256)): # calculate histogram first # several parameters must be enclosed in square brackets # here histogram is calculated directly from grayscale. So the first channel is used, and # can also separate the channel. The histogram # bins of multiple channels is taken as 16 image1 = cv2.resize (image1,size) image2 = cv2.resize (image2,size) hist1 = cv2.calcHist ([image1], [0], None, [256,0.0255.0]) hist2 = cv2.calcHist ([image2], [0], None, [0.0255.0]) # the histogram plt.plot (range (256), hist1,'r') plt.plot (range (256), hist2) can be compared 'b') plt.show () # calculate the coincidence degree of the histogram degree = 0 for i in range (len (hist1)): if hist1 [I]! = hist2 [I]: degree = degree + (1-abs (hist1 [I]-hist2 [I]) / max (hist1 [I])) else: degree = degree + 1 degree = degree/len (hist1) return degree # calculate the similarity value def calculate (image1,image2) of the single-channel histogram: hist1 = cv2.calcHist ([image1]) [0], None, [256a], [0.0255.0]) hist2 = cv2.calcHist ([image2], [0], None, [256a], [0.0255.0]) # calculate the coincidence degree of the histogram degree = 0 for i in range (len (hist1)): if hist1 [I]! = hist2 [I]: degree = degree + (1-abs) / max Hist2 [I]) else: degree = degree + 1 degree = degree/len (hist1) return degree # calculate the similarity def classify_hist_with_split by obtaining the histogram of each channel (image1,image2,size = (256256)): # after resize the image Separate into three channels Then calculate the similarity value of each channel image1 = cv2.resize (image1,size) image2 = cv2.resize (image2,size) sub_image1 = cv2.split (image1) sub_image2 = cv2.split (image2) sub_data = 0 for im1,im2 in zip (sub_image1,sub_image2): sub_data + = calculate (im1,im2) sub_data = sub_data/3 return sub_data # average hash algorithm def classify_aHash (image1,image2): image1 = cv2.resize (image1 (8) image2 = cv2.resize (image2, (8)) gray1 = cv2.cvtColor (image1,cv2.COLOR_BGR2GRAY) gray2 = cv2.cvtColor (image2,cv2.COLOR_BGR2GRAY) hash2 = getHash (gray1) hash3 = getHash (gray2) return Hamming_distance (hash2,hash3) def classify_pHash (image1,image2): image1 = cv2.resize (image1, (32)) image2 = cv2.resize (image2, (32)) gray1 = cv2.cvtColor (image1,cv2.COLOR_BGR2GRAY) gray2 = cv2.cvtColor (image2) Cv2.COLOR_BGR2GRAY) # convert grayscale image to floating point Then perform the dct transform dct1 = cv2.dct (np.float32 (gray1)) dct2 = cv2.dct (np.float32 (gray2)) # take the 8x8 in the upper left corner, which represents the lowest frequency of the image # this operation is equivalent to the masking operation implemented in C++ using opencv # masking operation in python You can directly take out a certain part of the image matrix dct1_roi = dct1 [0dct2_roi 8] dct2_roi = dct2 [0dct2_roi 8] hash2 = getHash (dct1_roi) hash3 = getHash (dct2_roi) return Hamming_distance (hash2,hash3) # input grayscale image Return hashdef getHash (image): avreage = np.mean (image) hash = [] for i in range (image.shape [0]): for j in range (image.shape [1]): if image [iMae j] > avreage: hash.append (1) else: hash.append (0) return hash # calculate hamming distance def Hamming_distance (hash2 Hash3): num = 0 for index in range (len (hash2)): if hash2 [index]! = hash3 [index]: num + = 1 return num if _ name__ = ='_ main__': img1 = cv2.imread ('10.jpg') cv2.imshow (' img1',img1) img2 = cv2.imread ('11.jpg') cv2.imshow (' img2',img2) degree = classify_gray_hist (img1,img2) # degree = classify_hist_with_split (img1,img2) # degree = classify_aHash (img1 Img2) # degree = classify_pHash (img1,img2) print degree cv2.waitKey (0) so far The study on "how to use the Python dHash algorithm" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

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