我正在尝试运行以下代码片段:
from scipy.stats import wasserstein_distance
from imageio import imread
import numpy as np
def get_histogram(img):
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("./IMG_4835.jpg")
b = imread("./IMG_4836.jpg")
a_hist = get_histogram(a)
b_hist = get_histogram(b)
dist = wasserstein_distance(a_hist, b_hist)
print(dist)但我在以下位置得到一个错误:
h, w = img.shape
b = imread('b.jpg', mode='L')ValueError: too many values to unpack (expected 2)使用的原始代码:
from scipy.ndimage import imread来读取图像文件,但是因为我无法导入它,所以我使用了来自另一个库的imread。这可能与错误有关吗?
发布于 2021-04-10 22:15:28
h,w = img.shape[:2]应该可以解决这个问题。
发布于 2021-07-27 10:16:22
RGB图像有3个通道,您的代码只能使用2个通道。您可以将您的图像转换为灰色:
from skimage.color import rgb2gray
from skimage import img_as_ubyte
img = img_as_ubyte(rgb2gray(img))或者,如果您不关心正确的RGB2灰色:(参见https://e2eml.school/convert_rgb_to_grayscale.html)
img = np.mean(img, axis=2).astype(np.uint8)但看起来你是在重新发明一个轮子。要使用直方图,请执行以下操作:
a_hist, _ = np.histogram(a, bins=256)
b_hist, _ = np.histogram(b, bins=256)https://stackoverflow.com/questions/67035263
复制相似问题