Reputation: 11
Ok so this is the function calculating the histogram:
def image_histogram():
from PIL import Image
import numpy as np
import glob
im = Image.open('/Users/Adnan/Desktop/Archive/0.jpg')
im_vals1 = np.zeros(256)
im_vals2 = np.zeros(256)
im_vals3 = np.zeros(256)
r,g,b = im.split()
pixels_r = list(r.getdata())
pixels_g = list(g.getdata())
pixels_b = list(b.getdata())
pix_r = np.array(pixels_r)
pix_g = np.array(pixels_g)
pix_b = np.array(pixels_b)
for idx in range (0, len(pix_r)):
im_vals1[pix_r[idx]] += 1
im_vals2[pix_g[idx]] += 1
im_vals3[pix_b[idx]] += 1
histogram = list(im_vals1) + list(im_vals2) + list(im_vals3)
return histogram
print(image_histogram())
def euclidean_distance():
from scipy.spatial import distance
a = image_histogram()
b = image_histogram()
dist = distance.euclidean(a,b)
print(euclidean_distance())
ok so this function now calculates the histogram of 1 image (0.jpg). I would like to know how could i run this same function multiple times with diff images and store each images histogram as a list to then be used by the euclidean distance function. Im guessing some sort of recursion should do the trick but do not know how to go about this since I can not seem to find it anywhere.
Upvotes: 0
Views: 3583
Reputation: 470
From my computer vision course, I remember that one of the best metrics for calculating the distance between two histograms is the chi-squared distance.
https://stats.stackexchange.com/questions/184101/comparing-two-histograms-using-chi-square-distance
In python, it can be a custom metric for sklearn Nearest Neighbor function:
def chiSquared(p,q):
return 0.5*np.sum((p-q)**2/(p+q+1e-6))
Upvotes: 4