python--several screenshots comparison way!

Source: Internet
Author: User

This record of several contrasting ways, mainly for mobile phone automation testing, by contrast to determine the correctness of the test, the way is as follows:

#-*-coding:utf-8-*-"Use: Use Python to implement multiple methods to achieve image recognition Author:syw '" Import cv2 import numpy asNP fromMatplotlibImportPyplot asPLT # The simplest implementation of a grayscale histogram as a similar comparison def classify_gray_hist (Image1,image2,size= ( the, the): # Calculate histogram first # Several parameters must be enclosed in square brackets # here is a histogram calculated directly from the grayscale, so the first channel is used, # can also be separated by the channel, get the Histogram of multiple channels # bins take as 16
      Image1=cv2.resize (image1,size) image2=cv2.resize (image2,size) Hist1= Cv2.calchist ([image1],[0],none,[ the],[0.0,255.0]) Hist2= Cv2.calchist ([image2],[0],none,[ the],[0.0,255.0]) # can compare the bottom histogram plt.plot (range ( the), Hist1,'R') Plt.plot (range ( the), Hist2,'b') Plt.show () # Calculates the coincident degree of the histogram degree=0     forIinchRange (len (hist1)):ifHist1[i]! =Hist2[i]: degree= degree + (1-ABS (Hist1[i]-hist2[i])/Max (Hist1[i],hist2[i]))Else: Degree= degree +1degree= degree/Len (hist1)returndegree # Similarity value for calculating histogram of single channel def calculate (image1,image2): Hist1= Cv2.calchist ([image1],[0],none,[ the],[0.0,255.0]) Hist2= Cv2.calchist ([image2],[0],none,[ the],[0.0,255.0]) # Calculates the coincidence degree of the histogram degree=0     forIinchRange (len (hist1)):ifHist1[i]! =Hist2[i]: degree= degree + (1-ABS (Hist1[i]-hist2[i])/Max (Hist1[i],hist2[i]))Else: Degree= degree +1degree= degree/Len (hist1)returndegree # by getting a histogram of each channel to calculate the similarity def classify_hist_with_split (Image1,image2,size= ( the, the) : # Resize the image after it is separated into three channels, then calculates 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     forIm1,im2inchZip (sub_image1,sub_image2): Sub_data+=Calculate (im1,im2) Sub_data= sub_data/3    returnsub_data # Average hash algorithm calculates def classify_ahash (image1,image2): Image1= Cv2.resize (Image1, (8,8)  #cv2. Resize (source, target, transform method), transform the picture into the desired size image2= Cv2.resize (Image2, (8,8)) Gray1= Cv2.cvtcolor (image1,cv2. Color_bgr2gray)#cv2. Cvtcolor (Input_image,flag) to convert the color space of a picture, the flag parameter determines the type of transformation. such as Bgr-> Gray flag can be set to CV2. Color_bgr2gray. Gray2=Cv2.cvtcolor (Image2,cv2. Color_bgr2gray) Hash1=Gethash (gray1) Hash2=Gethash (gray2)returnHamming_distance (HASH1,HASH2) def classify_phash (image1,image2): Image1= Cv2.resize (Image1, ( +, +)) Image2= Cv2.resize (Image2, ( +, +)) Gray1=Cv2.cvtcolor (Image1,cv2. Color_bgr2gray) Gray2=Cv2.cvtcolor (Image2,cv2. Color_bgr2gray) # Convert grayscale to floating-point, then DCT transform dct1=CV2.DCT (Np.float32 (gray1)) Dct2=CV2.DCT (Np.float32 (gray2)) # take the top left corner of the 8 *8, these represent the lowest frequency of the picture # This operation is equivalent to C + + Mask Operations implemented using OPENCV # in Python mask operations, you can directly remove a portion of the image matrix Dct1_roi= dct1[0:8,0:8] Dct2_roi= dct2[0:8,0:8] Hash1=Gethash (dct1_roi) Hash2=Gethash (Dct2_roi)returnhamming_distance (HASH1,HASH2) #输入灰度图, return hash def gethash (image): Avreage=Np.mean (image) #np. Mean () to fetch mean hash= []      forIinchRange (image.shape[0]):          forJinchRange (image.shape[1]):             ifIMAGE[I,J] >Avreage:hash.append (1)             Else: Hash.append (0)     returnHash # calculate Hamming distance def hamming_distance (hash1,hash2): Num=0     forIndexinchRange (len (HASH1)):ifHash1[index]! =Hash2[index]: num+=1    returnnum #返回值越小, the higher the image similarity if__name__ = ='__main__': Img1= Cv2.imread ('E:\\p1\\1.png', Cv2. Imread_color) #读入图片, total 2 parameters, the first parameter is the picture file name to be read in, and the second parameter is how to read the picture, including Cv2. Imread_color read a picture in color, cv2. Imread_unchanged reads a color picture and includes an alpha channel, Cv2. Imread_grayscale the image is read in grayscale mode Img2= Cv2.imread ('E:\\p2\\2.png', Cv2. Imread_color) C_IMG1= img1[ $: -, -: -] #截取图片的某一部分 C_img2= img2[ $: -, -: -] #syw, [ $: -] control is the height, [ -: -] control is the length, 500 represents the x1,800 represents the X2 cv2.imshow ('IMG1', C_IMG1) #创建一个窗口显示图片, a total of 2 parameters, the first parameter is "window display Picture title" Can create more than one window, but each window cannot have the same name, the second parameter is read in the picture cv2.imshow ('Img2', C_img2) #degree=classify_gray_hist (IMG1,IMG2) #degree=classify_hist_with_split (IMG1,IMG2) #degree=Classify_phash (IMG1,IMG2) #syw #degree=Classify_phash (C_IMG1,C_IMG2) degree=Classify_ahash (C_IMG1,C_IMG2) print degreeifdegree = =0or degree <Ten: Print"Pass"    Else: Print"fail"Cv2.waitkey (0) #键盘绑定函数. A total of one function, which represents the number of milliseconds to wait, will wait for a specific few milliseconds to see if the keyboard has input, the return value is an ASCII value, and if its argument is 0, it is an indefinite wait for keyboard input cv2.destroyallwindows () #删除建立的全部窗口 # Cv2.destroywindows (): #删除指定的窗口

python--several contrasting ways!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.