Python realizes image similarity calculation

Picture similarity

_
Overview

When I used a software to clean my phone today, I saw a better feature: delete similar pictures. The software recognizes the similar pictures in the album, deletes the similar pictures, and releases the phone storage.

I checked the basic implementation algorithm of this function on the Internet. There are many algorithms to find the similarity of the picture. The common ones are the mean hash algorithm, the difference perception algorithm, the perceptual hash algorithm, single-channel histogram, etc. There are specific implementations on the Internet. Algorithm, here I only demonstrate a single-channel histogram.

All the pictures mentioned in this tweet were taken in Wuhan. Bless Wuhan, bless Hubei, and bless China.





Project overview

Import a known picture, calculate the similarity between each picture and the known picture, and output the similarity.

The specific calculation strategy is: import the picture, calculate the histogram of the picture, normalize the picture, use compareHist() to compare the similarity.

Finally, the similarity is printed, and then the histogram value is output. The x-axis of the image refers to the pixel change between 0 and 255 of the picture, and the y-axis refers to the proportion of the 0-255 pixels.

_Project
realization

1. Import pictures

# 读取函数,用来读取文件夹中的所有函数,输入参数是文件名
def read_directory(directory_name):
    for filename in os.listdir(directory_name):
        strDic = directory_name + "//" + filename
        imageList.append(strDic)

2. Picture processing

# 读取函数,用来读取文件夹中的所有函数,输入参数是文件名
def read_directory(directory_name):
    for filename in os.listdir(directory_name):
        strDic = directory_name + "//" + filename
        imageList.append(strDic)

3. Print the result


for i in range(0, len(similar)):
    print(similar[i])
    lab = 'img' + str(i)
    plt.plot(pilex[i], label=lab)
_

Result display

1. Similarity results

Python realizes image similarity calculation

2. Similar pictures (similarity 0.89)

Python realizes image similarity calculation

3. Histogram display

Python realizes image similarity calculation

Guess you like

Origin blog.51cto.com/15069472/2577361