考虑以下两个图像之间的自定义度量(注意:我知道这很糟糕,这是一个例子)。对于图像上方的每个3*3内核,它会查看匹配的像素数并计算该内核的比率。最后,它输出1 - max(ratio)
。
def custom_metric(a, b):
w, h = a.shape
ratio = []
for i in range(w // ksize):
for j in range(h // ksize):
kernel_diff = a[i*ksize:(i+1)*ksize, j*ksize:(j+1)*ksize] - b[i*ksize:(i+1)*ksize, j*ksize:(j+1)*ksize]
ratio.append( np.mean(kernel_diff == 0) )
return 1 - max(ratio)
a = np.array([
[1,0,0,0,0,1],
[0,0,1,0,0,0],
[0,0,0,0,0,0],
[0,0,0,0,1,0],
[0,1,0,0,0,1],
[0,0,0,0,0,0]
])
b = np.array([
[0,1,0,0,0,0],
[0,0,1,0,0,1],
[1,0,0,0,1,0],
[0,0,0,0,1,0],
[0,0,0,1,0,1],
[0,0,0,1,1,1]
])
print(custom_metric(a, b)) # outputs 1/9 because of the bottom left window
字符串
我见过的scipy / sklearn中计算距离矩阵的所有函数都将shape(n_samples_X,n_features)数组作为输入,比如sklearn的pairwise_distances
。是否有一个函数允许更高维的数组,例如shape(n_samples_X,width,height),我可以调用我的度量?
1条答案
按热度按时间mcdcgff01#
尝试将特征展平到单个轴上。而不是将数据整形为
(samples, height * width * channels * ...)
的2D数组。这将适用于任意数量的维度,然后您可以将其提供给sklearn
。在本例中,尝试
a_reshaped = a.reshape([len(a), -1])
。这将通过将高度轴和宽度轴移动到相同的维度上,给予您一个形状为(samples, height * width)
的2D数组。演示了如何将高度和宽度扁平化为一维,同时保持每个补丁的连续性。还演示了如何在扁平化的数组上运行OP的度量。
字符串
使用方法:
型
上面的代码不是必需的,但我使用了一些额外的测试代码来检查整形结果:
型