Compare similarity of images using OpenCV with Python

前端 未结 4 1777
走了就别回头了
走了就别回头了 2020-12-07 12:04

I\'m trying to compare a image to a list of other images and return a selection of images (like Google search images) of this list with up to 70% of similarity.

I ge

4条回答
  •  不思量自难忘°
    2020-12-07 12:10

    For a simpler implementation of Earth Mover's Distance (aka Wasserstein Distance) in Python, you could use Scipy:

    from scipy.stats import wasserstein_distance
    from scipy.ndimage import imread
    import numpy as np
    
    def get_histogram(img):
      '''
      Get the histogram of an image. For an 8-bit, grayscale image, the
      histogram will be a 256 unit vector in which the nth value indicates
      the percent of the pixels in the image with the given darkness level.
      The histogram's values sum to 1.
      '''
      h, w = img.shape
      hist = [0.0] * 256
      for i in range(h):
        for j in range(w):
          hist[img[i, j]] += 1
      return np.array(hist) / (h * w)
    
    a = imread('a.jpg', mode='L')
    b = imread('b.jpg', mode='L')
    a_hist = get_histogram(a)
    b_hist = get_histogram(b)
    dist = wasserstein_distance(a_hist, b_hist)
    print(dist)
    

提交回复
热议问题