我想计算两个 numpy 直方图之间的距离
i want to compute the distance between two numpy histogram
我正在创建一个图像处理程序,我想测量两个 numpy 直方图之间的 wasserstein 距离。
这两个直方图是用函数 numpy.histogram
创建的
我像这样尝试了 scipy.stats 包中的 wasserstein_distance
from scipy.stats import wasserstein_distance
wasserstein_distance(histogram1,histogram2)
但它给了我那个错误
ValueError: setting an array element with a sequence.
完整代码:
首先计算距离的函数:
def f_dist( histogram1 ,histogram2):
return wasserstein_distance(histogram1,histogram2)
比计算直方图创建掩码的函数:
def prepare_mask(polygon, image,value):
"""Returns binary mask based on input polygon presented as list of coordinates of vertices
Params:
polygon (list) - coordinates of polygon's vertices. Ex: [(x1,y1),(x2,y2),...] or [x1,y1,x2,y2,...]
image (numpy array) - original image. Will be used to create mask of the same size. Shape (H, W, C).
Output:
mask (numpy array) - boolean mask. Shape (H, W).
"""
# create an "empty" pre-mask with the same size as original image
width = image.shape[1]
height = image.shape[0]
mask = Image.new('L', (width, height),value )
# Draw your mask based on polygon
ImageDraw.Draw(mask).polygon(polygon, outline=1, fill=abs(value-1))
# Covert to np array
mask = np.array(mask).astype(bool)
return mask
比创建直方图的函数
def compute_histogram(mask, image):
"""Returns histogram for image region defined by mask for each channel
Params:
image (numpy array) - original image. Shape (H, W, C).
mask (numpy array) - boolean mask. Shape (H, W).
Output:
list of tuples, each tuple (each channel) contains 2 arrays: first - computed histogram, the second - bins.
"""
# Apply binary mask to your array, you will get array with shape (N, C)
region = image[mask]
hist = np.histogram(region.ravel(), bins=256, range=[0, 255])
return hist
现在主要功能:
points=[(633, 312), (630, 351), (623, 389), (611, 426), (594, 462), (573, 495), (548, 525), (519, 552), (488, 575), (453, 594), (417, 608), (379, 618), (340, 623), (301, 623), (262, 618), (224, 608), (188, 594), (153, 575), (122, 552), (93, 525), (68, 495), (47, 462), (30, 426), (18, 389), (11, 351), (9, 311), (11, 272), (18, 234), (30, 197), (47, 161), (68, 128), (93, 98), (122, 71), (153, 48), (188, 29), (224, 15), (262, 5), (301, 0), (340, 0), (379, 5), (417, 15), (453, 29), (488, 48), (519, 71), (548, 98), (573, 128), (594, 161), (611, 197), (623, 234), (630, 272)]
mask1 = prepare_mask(points, image_gray, 0)
mask2 = prepare_mask(points, image_gray, 1)
histogram1 = compute_histogram(mask1, image_gray)
histogram2 = compute_histogram(mask2, image_gray)
dist=f_dist(histogram1,histogram2)
感谢 SpghttCd,解决方案很简单......我只需要更换
wasserstein_distance(histogram1, histogram2)
和
wasserstein_distance(histogram1[0], histogram2[0])
我正在创建一个图像处理程序,我想测量两个 numpy 直方图之间的 wasserstein 距离。 这两个直方图是用函数 numpy.histogram
创建的我像这样尝试了 scipy.stats 包中的 wasserstein_distance
from scipy.stats import wasserstein_distance
wasserstein_distance(histogram1,histogram2)
但它给了我那个错误
ValueError: setting an array element with a sequence.
完整代码:
首先计算距离的函数:
def f_dist( histogram1 ,histogram2):
return wasserstein_distance(histogram1,histogram2)
比计算直方图创建掩码的函数:
def prepare_mask(polygon, image,value):
"""Returns binary mask based on input polygon presented as list of coordinates of vertices
Params:
polygon (list) - coordinates of polygon's vertices. Ex: [(x1,y1),(x2,y2),...] or [x1,y1,x2,y2,...]
image (numpy array) - original image. Will be used to create mask of the same size. Shape (H, W, C).
Output:
mask (numpy array) - boolean mask. Shape (H, W).
"""
# create an "empty" pre-mask with the same size as original image
width = image.shape[1]
height = image.shape[0]
mask = Image.new('L', (width, height),value )
# Draw your mask based on polygon
ImageDraw.Draw(mask).polygon(polygon, outline=1, fill=abs(value-1))
# Covert to np array
mask = np.array(mask).astype(bool)
return mask
比创建直方图的函数
def compute_histogram(mask, image):
"""Returns histogram for image region defined by mask for each channel
Params:
image (numpy array) - original image. Shape (H, W, C).
mask (numpy array) - boolean mask. Shape (H, W).
Output:
list of tuples, each tuple (each channel) contains 2 arrays: first - computed histogram, the second - bins.
"""
# Apply binary mask to your array, you will get array with shape (N, C)
region = image[mask]
hist = np.histogram(region.ravel(), bins=256, range=[0, 255])
return hist
现在主要功能:
points=[(633, 312), (630, 351), (623, 389), (611, 426), (594, 462), (573, 495), (548, 525), (519, 552), (488, 575), (453, 594), (417, 608), (379, 618), (340, 623), (301, 623), (262, 618), (224, 608), (188, 594), (153, 575), (122, 552), (93, 525), (68, 495), (47, 462), (30, 426), (18, 389), (11, 351), (9, 311), (11, 272), (18, 234), (30, 197), (47, 161), (68, 128), (93, 98), (122, 71), (153, 48), (188, 29), (224, 15), (262, 5), (301, 0), (340, 0), (379, 5), (417, 15), (453, 29), (488, 48), (519, 71), (548, 98), (573, 128), (594, 161), (611, 197), (623, 234), (630, 272)]
mask1 = prepare_mask(points, image_gray, 0)
mask2 = prepare_mask(points, image_gray, 1)
histogram1 = compute_histogram(mask1, image_gray)
histogram2 = compute_histogram(mask2, image_gray)
dist=f_dist(histogram1,histogram2)
感谢 SpghttCd,解决方案很简单......我只需要更换
wasserstein_distance(histogram1, histogram2)
和
wasserstein_distance(histogram1[0], histogram2[0])