好的,这是计算直方图的函数:
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())
好的,所以这个函数现在计算1个图像的直方图(0.jpg)。我想知道如何用diff图像多次运行这个相同的函数,并将每个图像直方图存储为列表,然后由欧几里德距离函数使用。我猜测某种递归应该做的伎俩,但不知道如何去做,因为我似乎无法在任何地方找到它。
答案 0 :(得分:3)
从我的计算机视觉课程中,我记得计算两个直方图之间距离的最佳指标之一是卡方距离。
https://stats.stackexchange.com/questions/184101/comparing-two-histograms-using-chi-square-distance
在python中,它可以是sklearn最近邻函数的自定义指标:
def chiSquared(p,q):
return 0.5*np.sum((p-q)**2/(p+q+1e-6))