程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Two methods of Python image similarity and embedded space measurement learning

編輯:Python

Image similarity

Method #1

In this paper , We will introduce how to use the image similarity measure library to compare images . According to the document of the library , We can use eight different evaluation indicators to calculate the similarity between images .

Fortunately, , All the terrible mathematical operations have been realized for us , We can start measuring image similarity immediately . We just need to call the name of the selected evaluation index and pass two images as parameters . for example :

Please note that , The default indicator is psnr.

There are two ways to use this package : You can execute commands in the terminal or write separate Python Script .

If you want a quick assessment between two images , Please run the following command in the terminal :

( Optional ) add to — metric Flag to indicate the evaluation index to be used .

In this paper , I selected three evaluation indicators :rmse,psnr and ssim.

First , Let's understand the program we are going to build :

  • We will pass the original image as a parameter to our program .
  • This code will traverse all the images in the dataset folder , And calculate the similarity between each image and the original image .
  • Last , We will print the results of each evaluation indicator .

Programming

Let's use pip or pip3 Installation Library :

Next , Create a new Python Script file and paste the following code :

The above code analysis :

  • Import the necessary library files
  • There is no need to hard code the image every time the script runs , But use argv [1] Function provides the image name as a command line parameter .
  • We use OpenCV Python Package read image . My will be in a separate folder , be called test. if necessary , Please adjust the code .
  • To compare the results of different indicators , We're going to create a dictionary , The key is the name of the dataset image , And the value is the similarity value .
  • The image similarity measurement package requires images to have the same shape . Because we are comparing images with slightly different shapes , So we need to provide the same shape . So , We will use cv2.resize(data_img,dim,interpolation = cv2.INTER_AREA) function , In this function , The size of the dataset image will be adjusted according to the size of the original image .
  • We will traverse the dataset Directory , Resize each image , Then fill in each dictionary . Please note that , Due to size adjustment , The image may be distorted .
  • calc_closest_val(dict,checkMax) Function returns the most similar image . It also prints out the similarity value of each image . Please note that , According to some evaluation indicators ( Such as RMSE), value 0 Indicates that the data is very appropriate . For other indicators , vice versa - The higher the value , The better the match . That's why we use bool Parameters checkMax. It selects the closest image according to the minimum or maximum value in the dictionary .
  • Last , For convenience , We will display the most similar image according to each evaluation index .

demonstration

First , I will create a folder for the original images . then , I put the comparison images in the dataset folder .

This red apple will be our original query image :

We compare it with other fruits :

Now? , Let's run Python Program , Find the best match :

$ python3 measure_similarity.py red_apple.jpg

Output :

The difference between dataset/red_pear.jpg and the original image is :
0.8827639040117994
The difference between dataset/cherry.jpg and the original image is :
0.8542221298727691
The difference between dataset/green_apple.jpg and the original image is :
0.9379929447852137
The closest value: 0.9379929447852137
######################################################################
The difference between dataset/red_pear.jpg and the original image is :
0.018479494
The difference between dataset/cherry.jpg and the original image is :
0.022247538
The difference between dataset/green_apple.jpg and the original image is :
0.014238722
The closest value: 0.014238722
######################################################################
The difference between dataset/red_pear.jpg and the original image is :
55.925131298420894
The difference between dataset/cherry.jpg and the original image is :
55.43173546627284
The difference between dataset/green_apple.jpg and the original image is :
58.09926725713899
The closest value: 58.09926725713899
######################################################################
The most similar image accroding to SSIM: {
'dataset/green_apple.jpg': 0.9379929447852137}
The most similar image accroding to RMSE: {
'dataset/green_apple.jpg': 0.014238722}
The most similar image accroding to SRE: {
'dataset/green_apple.jpg': 58.09926725713899}

As you can see , The green apple is the winner . If you look at each result , You will find that the second most similar image is the red pear .

Now? , Let's see what happens when it's hard to predict the outcome . Let's put a picture of the red tomato in the dataset folder :

Here's what I'm going to say , Green apples and tomatoes look like red apples . Let's check the results according to the mathematics :

To make it more exciting , Let's try to compare more different images . Since I like painting , I took pictures of my own paintings for this experiment .

Original picture :

Image data set :

I saw two photos similar to the small house , These small houses are similar to the original photos .

Autumn scenery matches the house best .

How about these ?

At first glance , The result surprised me a little . however , When I watch carefully , The photo taken with the girl has a dark blue background , Just like the original image . The aurora borealis painting has similar mountains in the background .

As you can see , The results are different based on different evaluation indicators .

We have learned how to use different evaluation indicators to measure image similarity . The image similarity measure library implements these methods for us .

Which method is the best ?

It's hard to predict the exact result . Metrics compare images according to different aspects . It depends on how you compare the images .

Source code

The way 2

Embedding spatial metric learning

Metric learning is a method directly based on distance measurement , It aims to establish the similarity or difference between images . On the other hand , Depth measurement learning uses neural networks to automatically learn the distinguishing features from images , Then calculate the metrics . Its purpose is to train models that can embed input into high-dimensional space , In order to define the training program “ be similar ” Enter close to each other . Once trained, these models can generate embeddedness for downstream systems , This similarity is useful in these systems ; Examples include one form of a pre training embedded model as a ranking signal for a search or as another monitoring problem .

Image similarity search

Similarity search modeling

Source code

Refer to the - Yatu inter


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved