我是图像比较和skimage用法的新手。 我正在尝试使用compare_ssim获得多个图像之间的结构相似性差异。我使用pairwise_distances返回“ n”个图像之间的差异数组。 但是我收到以下错误:
Traceback (most recent call last):
File "<ipython-input-40-8451bdf0c9e6>", line 1, in <module>
m = pairwise_distances(X, X, metric=computeDiff)
File "C:\Anaconda\lib\site-packages\sklearn\metrics\pairwise.py", line 1432, in pairwise_distances
return _parallel_pairwise(X, Y, func, n_jobs, **kwds)
File "C:\Anaconda\lib\site-packages\sklearn\metrics\pairwise.py", line 1067, in _parallel_pairwise
return func(X, Y, **kwds)
File "C:\Anaconda\lib\site-packages\sklearn\metrics\pairwise.py", line 1088, in _pairwise_callable
out[i, j] = metric(X[i], Y[j], **kwds)
ValueError: setting an array element with a sequence.
我尝试打印数组大小,并比较(compare_ssim)返回的nd数组中两个列表的列表大小,以检查它们是否具有相同的大小。
from skimage.measure import compare_ssim
import imutils
import cv2
from sklearn.metrics.pairwise import pairwise_distances
import cvutils
import numpy as np
def computeDiff(x,y):
print(train_images[int(x)])
print(train_images[int(y)])
img1 = cv2.imread(train_images[int(x)])
img2 = cv2.imread(train_images[int(y)])
grayA = cv2.cvtColor(img1, cv2.COLOR_BGR2GRAY)
grayB = cv2.cvtColor(img2, cv2.COLOR_BGR2GRAY)
(score, diff) = compare_ssim(grayA, grayB, full=True)
diff = (diff * 255).astype("uint8")
print("SSIM: {}".format(score))
return diff
images_path = "ImagePath"
train_images = []
train_images = cvutils.imlist(images_path)
train_images = list(set(train_images))
X = np.arange(len(train_images)).reshape(-1, 1)
m = pairwise_distances(X, X, metric=computeDiff)
我希望有人可以帮助指出此代码出了什么问题,并在可能的情况下帮助进行修复。
谢谢