1

I have a bunch of images with each representing a shape. I have one image as a reference and six others that I need to sort out to find the one that resemble the most my reference image. Note that the six images tested were applied a kind of filter with dots and aren't perfectly centered like the reference image as you can see below :

reference image

corresponding image with filter and mispositioning

I implemented this code to mine without success : https://github.com/bnsreenu/python_for_microscopists/blob/master/191_measure_img_similarity.py My code :

from skimage.metrics import structural_similarity
import cv2
from PIL import Image

#Works well with images of different dimensions
def orb_sim(img1, img2):
  orb = cv2.ORB_create()
  # detect keypoints and descriptors
  kp_a, desc_a = orb.detectAndCompute(img1, None)
  kp_b, desc_b = orb.detectAndCompute(img2, None)
  # define the bruteforce matcher object
  bf = cv2.BFMatcher(cv2.NORM_HAMMING, crossCheck=True) 
  #perform matches
  matches = bf.match(desc_a, desc_b)
  #Look for similar regions with distance < 50. Goes from 0 to 100 so pick a number between
  similar_regions = [i for i in matches if i.distance < 50]  
  if len(matches) == 0:
    return 0
  return len(similar_regions) / len(matches)

#Needs images to be same dimensions
def structural_sim(img1, img2):
  sim, diff = structural_similarity(img1, img2, full=True)
  return sim

im_ref = cv2.imread('captcha_clean/umbrella.png', 0)
for i in range(1,7):
  path = "Tmp_Image/split_"+str(i)+".png"
  im = Image.open(path)
  path = "Tmp_Image/newsplit_"+str(i)+".png"
  new_im = im.resize((32,32))
  new_im.save(path)
  im_test = cv2.imread(path, 0)
  ssim = structural_sim(im_ref, im_test) #1.0 means identical. Lower = not similar
  print("For image "+str(i)+" similarity using SSIM is: ", ssim)
  orb_similarity = orb_sim(im_ref, im_test)  #1.0 means identical. Lower = not similar
  print("For image "+str(i)+" similarity using ORB is: ", orb_similarity)

In this case the corresponding image is number 4.

Here is the output :

For image 1 similarity using SSIM is:  -0.04562843656475159
For image 1 similarity using ORB is:  0
For image 2 similarity using SSIM is:  0.04770572948037391
For image 2 similarity using ORB is:  0
For image 3 similarity using SSIM is:  0.10395830102945436
For image 3 similarity using ORB is:  0
For image 4 similarity using SSIM is:  0.08297170823406234
For image 4 similarity using ORB is:  0
For image 5 similarity using SSIM is:  0.07766704880294842
For image 5 similarity using ORB is:  0
For image 6 similarity using SSIM is:  0.12072132618711812
For image 6 similarity using ORB is:  0

The most resembling looking image is the closest to 1.0 Here according to the algorithm this is image 6, which is false.

Is there any way to find the corresponding images despite these? I am open for any new method to solve my problem

  • *"I have tried this code without success."* It's a complex task, so *without success* is definitely not enough description of problem occurred. Show your code and explain what exactly doesn't work. – Olvin Roght Jul 12 '22 at 13:58
  • Maybe you can find some success from sci-kit (skimage), refer to my old question and see if this helps: [link](https://stackoverflow.com/questions/35877193/detecting-similar-points-between-two-pictures-and-then-overlaying-them-python) – Michael S. Jul 12 '22 at 14:01
  • @Olvin Roght Post edited, now you can see the code with explanations. – TimeTraveler87 Jul 12 '22 at 15:43

0 Answers0